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