587e3cdeba4d0376402a7b485283c1d0490e7ab6
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / admin.py
1 # -*- encoding: utf-8 -*-
2 import re
3
4 from django.contrib import admin
5 from django.utils.safestring import mark_safe
6 from django.http import HttpResponseRedirect
7
8 from models import SourceActualite, Actualite, Discipline, Evenement, Record, ListSet, HarvestLog
9 from savoirs.globals import META
10
11 admin.site.register(Actualite)
12 admin.site.register(SourceActualite)
13 admin.site.register(Discipline)
14 admin.site.register(Evenement)
15
16 # Ces deux classes permettent d'implémenter la possibilité d'avoir un champs readonly_fields
17 # dans l.administration.
18 # Ce champs est devenu natif à partir de la version 1.2
19 # http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields
20 from django import forms
21 class ReadOnlyWidget(forms.Widget):
22 def __init__(self, original_value, display_value):
23 self.original_value = original_value
24 self.display_value = display_value
25
26 super(ReadOnlyWidget, self).__init__()
27
28 def render(self, name, value, attrs=None):
29 if self.display_value is not None:
30 output = self.display_value
31 else:
32 output = unicode(self.original_value)
33
34 # pour les relations
35 try:
36 output = ", ".join([ls.name for ls in self.original_value.get_query_set()])
37 except:
38 pass
39
40 is_url = re.match('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', output)
41 if is_url:
42 output = "<a target='_blank' href='%s'>%s</a>" % (output, output)
43
44 return mark_safe(output)
45
46 def value_from_datadict(self, data, files, name):
47 return self.original_value
48
49 class ReadOnlyAdminFields(object):
50 def get_form(self, request, obj=None):
51 form = super(ReadOnlyAdminFields, self).get_form(request, obj)
52
53 if hasattr(self, 'readonly_fields'):
54 for field_name in self.readonly_fields:
55 if field_name in form.base_fields:
56
57 if hasattr(obj, 'get_%s_display' % field_name):
58 display_value = getattr(obj, 'get_%s_display' % field_name)()
59 else:
60 display_value = None
61
62 form.base_fields[field_name].widget = ReadOnlyWidget(getattr(obj, field_name, ''), display_value)
63 form.base_fields[field_name].required = False
64 return form
65
66
67 class RecordAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
68 fields = [
69 'server',
70 'title',
71 'creator',
72 'description',
73 'modified',
74 'identifier',
75 'uri',
76 'source',
77 'contributor',
78 'publisher',
79 'type',
80 'format',
81 'language',
82 'disciplines',
83 'thematiques',
84 'pays',
85 'regions',
86 'validated',
87 ]
88
89 search_fields = []
90 readonly_fields = []
91
92 list_filter = (
93 'server',
94 'validated',
95 'pays',
96 'regions',
97 'disciplines',
98 'thematiques',
99 )
100 list_display = (
101 #OAI et extra AUF
102 'title',
103 'subject',
104 '_description',
105 '_uri',
106 #'server',
107 'identifier',
108 #'source',
109 'modified',
110 'creator',
111 #'contributor',
112 #'language',
113 #'publisher',
114 'format',
115 'type',
116
117 #SEP 2 (aucune données récoltées)
118 #'alt_title',
119 #'abstract',
120 #'creation',
121 #'issued',
122 #'isbn',
123 #'orig_lang',
124 'validated',
125 )
126 actions = ['assigner_pays',
127 'assigner_regions',
128 'assigner_disciplines',
129 'assigner_thematiques']
130
131 # fonctions pour présenter l'information
132 def __init__(self, *args, **kwargs):
133 """Surcharge l'initialisation pour définir les champs de recherche dynamiquement,
134 et les champs en lecture seule uniquement."""
135 self.search_fields = META.keys()
136 self.readonly_fields = META.keys()
137 self.readonly_fields.append('listsets')
138 super(RecordAdmin, self).__init__(*args, **kwargs)
139
140 def _uri(self, obj):
141 """ """
142 return "<a target='_blank' href='%s'>%s</a>" % (obj.uri, obj.uri)
143 _uri.allow_tags = True
144
145 def _description(self, obj):
146 """ """
147 max = 140
148 if obj.description is not None and len(obj.description) > max:
149 return "%s..." % obj.description[:max]
150 else:
151 return obj.description
152
153 # actions
154 def assigner_pays(self, request, queryset):
155 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
156 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('pays', ",".join(selected)))
157
158 def assigner_regions(self, request, queryset):
159 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
160 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('regions', ",".join(selected)))
161
162 def assigner_thematiques(self, request, queryset):
163 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
164 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('thematiques', ",".join(selected)))
165
166 def assigner_disciplines(self, request, queryset):
167 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
168 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('disciplines', ",".join(selected)))
169
170 admin.site.register(Record, RecordAdmin)
171
172 class ListSetAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
173 fields = ['spec', 'name', 'server', 'validated' ]
174 list_display = fields
175 readonly_fields = ['spec', 'name', 'server',]
176 list_filter = ('server',)
177
178 admin.site.register(ListSet, ListSetAdmin)
179
180 class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
181 fields = ['context', 'name', 'added', 'updated', 'processed', 'record']
182 list_display = fields + ['date']
183 admin_order_fields = ['date']
184 search_fields = fields
185 readonly_fields = fields
186 list_filter = ('context',)
187
188 admin.site.register(HarvestLog, HarvestLogAdmin)