CRUD evenement + admin
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / admin.py
1 # -*- encoding: utf-8 -*-
2 import re
3
4 from django.db import models
5 from django.contrib import admin
6 from django.contrib.auth.admin import UserAdmin
7 from django.contrib.auth.models import User
8 from django.contrib.admin.filterspecs import RelatedFilterSpec, FilterSpec
9 from django.utils.safestring import mark_safe
10 from django.utils.translation import ugettext as _
11 from django.utils.encoding import smart_unicode, iri_to_uri
12 from django.http import HttpResponseRedirect
13
14 from models import SourceActualite, Actualite, Discipline, Evenement, Record, ListSet, HarvestLog, Profile
15 from savoirs.globals import META
16
17 admin.site.register(SourceActualite)
18
19 class ListSetFilterSpec(RelatedFilterSpec):
20 """
21 Filtre custom automatiquement lié à un field nommé 'listsets'. Il a pour but de s'afficher
22 lorsqu'un server a déjà été présélectionné. Dans ce cas, il affiche une liste qui contient les
23 listsets de ce server.
24 """
25 def __init__(self, f, request, params, model, model_admin):
26 super(ListSetFilterSpec, self).__init__(f, request, params, model, model_admin)
27 self.server_name = request.GET.get('server', None)
28
29 def has_output(self):
30 return self.server_name is not None
31
32 FilterSpec.filter_specs.insert(0, (lambda f: f.name == 'listsets', ListSetFilterSpec))
33
34 # Ces deux classes permettent d'implémenter la possibilité d'avoir un champs readonly_fields
35 # dans l.administration.
36 # Ce champs est devenu natif à partir de la version 1.2
37 # http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields
38 from django import forms
39 class ReadOnlyWidget(forms.Widget):
40 def __init__(self, original_value, display_value):
41 self.original_value = original_value
42 self.display_value = display_value
43
44 super(ReadOnlyWidget, self).__init__()
45
46 def render(self, name, value, attrs=None):
47 if self.display_value is not None:
48 output = self.display_value
49 else:
50 output = unicode(self.original_value)
51
52 # pour les relations
53 try:
54 output = ", ".join([ls.name for ls in self.original_value.get_query_set()])
55 except:
56 pass
57
58 is_url = re.match('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', output)
59 if is_url:
60 output = "<a target='_blank' href='%s'>%s</a>" % (output, output)
61
62 return mark_safe(output)
63
64 def value_from_datadict(self, data, files, name):
65 return self.original_value
66
67 class ReadOnlyAdminFields(object):
68 def get_form(self, request, obj=None):
69 form = super(ReadOnlyAdminFields, self).get_form(request, obj)
70
71 if hasattr(self, 'readonly_fields'):
72 for field_name in self.readonly_fields:
73 if field_name in form.base_fields:
74
75 if hasattr(obj, 'get_%s_display' % field_name):
76 display_value = getattr(obj, 'get_%s_display' % field_name)()
77 else:
78 display_value = None
79
80 form.base_fields[field_name].widget = ReadOnlyWidget(getattr(obj, field_name, ''), display_value)
81 form.base_fields[field_name].required = False
82 return form
83
84
85 class RecordAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
86 fields = [
87 'server',
88 'title',
89 'creator',
90 'description',
91 'modified',
92 'identifier',
93 'uri',
94 'source',
95 'contributor',
96 'publisher',
97 'type',
98 'format',
99 'language',
100 'disciplines',
101 'thematiques',
102 'pays',
103 'regions',
104 'validated',
105 ]
106
107 search_fields = []
108 readonly_fields = []
109
110 list_filter = (
111 'validated',
112 'server',
113 'listsets',
114 'pays',
115 'regions',
116 'disciplines',
117 'thematiques',
118 )
119 list_display = (
120 #OAI et extra AUF
121 'title',
122 'subject',
123 '_description',
124 '_uri',
125 #'server',
126 'identifier',
127 #'source',
128 'modified',
129 'creator',
130 #'contributor',
131 #'language',
132 #'publisher',
133 'format',
134 'type',
135
136 #SEP 2 (aucune données récoltées)
137 #'alt_title',
138 #'abstract',
139 #'creation',
140 #'issued',
141 #'isbn',
142 #'orig_lang',
143 'est_complet',
144 'validated',
145 )
146 actions = ['valider_references',
147 'invalider_references',
148 'assigner_pays',
149 'assigner_regions',
150 'assigner_disciplines',
151 'assigner_thematiques']
152
153 # fonctions pour présenter l'information
154 def __init__(self, *args, **kwargs):
155 """Surcharge l'initialisation pour définir les champs de recherche dynamiquement,
156 et les champs en lecture seule uniquement."""
157 self.search_fields = META.keys()
158 self.readonly_fields = META.keys()
159 self.readonly_fields.append('listsets')
160 super(RecordAdmin, self).__init__(*args, **kwargs)
161
162 def est_complet(self, obj):
163 """ """
164 v = obj.est_complet()
165 return '<img src="/admin_media/img/admin/icon-%s.gif" alt="%d"/>' % (('no','yes')[v], v)
166 est_complet.allow_tags = True
167
168 def _uri(self, obj):
169 """ """
170 return "<a target='_blank' href='%s'>%s</a>" % (obj.uri, obj.uri)
171 _uri.allow_tags = True
172
173 def _description(self, obj):
174 """ """
175 max = 140
176 if obj.description is not None and len(obj.description) > max:
177 return "%s..." % obj.description[:max]
178 else:
179 return obj.description
180
181 # actions
182 def valider_references(self, request, queryset):
183 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
184 return HttpResponseRedirect("/admin/confirmation/%s/%s?ids=%s" % ('record', 'valider', ",".join(selected)))
185
186 def invalider_references(self, request, queryset):
187 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
188 return HttpResponseRedirect("/admin/confirmation/%s/%s/?ids=%s" % ('record', 'invalider', ",".join(selected)))
189
190 def assigner_pays(self, request, queryset):
191 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
192 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('pays', ",".join(selected)))
193
194 def assigner_regions(self, request, queryset):
195 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
196 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('regions', ",".join(selected)))
197
198 def assigner_thematiques(self, request, queryset):
199 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
200 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('thematiques', ",".join(selected)))
201
202 def assigner_disciplines(self, request, queryset):
203 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
204 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('disciplines', ",".join(selected)))
205
206 admin.site.register(Record, RecordAdmin)
207
208 class ListSetAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
209 fields = ['spec', 'name', 'server', 'validated' ]
210 list_display = fields
211 readonly_fields = ['spec', 'name', 'server',]
212 list_filter = ('server',)
213
214 admin.site.register(ListSet, ListSetAdmin)
215
216 class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
217 fields = ['context', 'name', 'added', 'updated', 'processed', 'record']
218 list_display = fields + ['date']
219 admin_order_fields = ['date']
220 search_fields = fields
221 readonly_fields = fields
222 list_filter = ('context',)
223
224 admin.site.register(HarvestLog, HarvestLogAdmin)
225
226 class ProfileInline(admin.TabularInline):
227 model = Profile
228 fk_name = 'user'
229 max_num = 1
230
231 class UserProfileAdmin(UserAdmin):
232 inlines = [ProfileInline, ]
233
234 admin.site.unregister(User)
235 admin.site.register(User, UserProfileAdmin)
236
237 class ActualiteAdmin(admin.ModelAdmin):
238 list_filter = ('visible',)
239 list_display = ('titre', 'source', 'date', 'visible')
240 actions = ['rendre_visible', 'rendre_invisible']
241
242 # actions
243 def rendre_visible(self, request, queryset):
244 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
245 return HttpResponseRedirect("/admin/confirmation/%s/%s?ids=%s" % ('actualite', 'visible', ",".join(selected)))
246
247 def rendre_invisible(self, request, queryset):
248 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
249 return HttpResponseRedirect("/admin/confirmation/%s/%s?ids=%s" % ('actualite', 'invisible', ",".join(selected)))
250
251 admin.site.register(Actualite, ActualiteAdmin)
252
253
254 class EvenementAdmin(admin.ModelAdmin):
255 list_filter = ('approuve',)
256 list_display = ('titre', 'debut', 'fin', 'lieu', 'approuve')
257 fields = ['titre',
258 'discipline',
259 'discipline_secondaire',
260 'type',
261 'fuseau',
262 'debut',
263 'fin',
264 'lieu',
265 'description',
266 'contact',
267 'url',
268 'approuve']
269
270 admin.site.register(Evenement, EvenementAdmin)
271
272
273