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.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 'listsets',
85 'pays',
86 'regions',
87 'validated',
88 ]
89
90 search_fields = []
91 readonly_fields = []
92
93 list_filter = (
94 'validated',
95 'server',
96 'pays',
97 'regions',
98 'disciplines',
99 'thematiques',
100 )
101 list_display = (
102 #OAI et extra AUF
103 'title',
104 'subject',
105 '_description',
106 '_uri',
107 #'server',
108 'identifier',
109 #'source',
110 'modified',
111 'creator',
112 #'contributor',
113 #'language',
114 #'publisher',
115 'format',
116 'type',
117
118 #SEP 2 (aucune données récoltées)
119 #'alt_title',
120 #'abstract',
121 #'creation',
122 #'issued',
123 #'isbn',
124 #'orig_lang',
125 'validated',
126 )
127 actions = ['assigner_pays',
128 'assigner_regions',
129 'assigner_disciplines',
130 'assigner_thematiques']
131
132 # fonctions pour présenter l'information
133 def __init__(self, *args, **kwargs):
134 """Surcharge l'initialisation pour définir les champs de recherche dynamiquement,
135 et les champs en lecture seule uniquement."""
136 self.search_fields = META.keys()
137 self.readonly_fields = META.keys()
138 self.readonly_fields.append('listsets')
139 super(RecordAdmin, self).__init__(*args, **kwargs)
140
141 def _uri(self, obj):
142 """ """
143 return "<a target='_blank' href='%s'>%s</a>" % (obj.uri, obj.uri)
144 _uri.allow_tags = True
145
146 def _description(self, obj):
147 """ """
148 max = 140
149 if obj.description is not None and len(obj.description) > max:
150 return "%s..." % obj.description[:max]
151 else:
152 return obj.description
153
154 # actions
155 def assigner_pays(self, request, queryset):
156 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
157 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('pays', ",".join(selected)))
158
159 def assigner_regions(self, request, queryset):
160 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
161 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('regions', ",".join(selected)))
162
163 def assigner_thematiques(self, request, queryset):
164 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
165 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('thematiques', ",".join(selected)))
166
167 def assigner_disciplines(self, request, queryset):
168 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
169 return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('disciplines', ",".join(selected)))
170
171 admin.site.register(Record, RecordAdmin)
172
173 class ListSetAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
174 fields = ['spec', 'name', 'server', 'validated' ]
175 list_display = fields
176 readonly_fields = ['spec', 'name', 'server',]
177 list_filter = ('server',)
178
179 admin.site.register(ListSet, ListSetAdmin)
180
181 class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
182 fields = ['context', 'name', 'added', 'updated', 'processed', 'record']
183 list_display = fields + ['date']
184 admin_order_fields = ['date']
185 search_fields = fields
186 readonly_fields = fields
187 list_filter = ('context',)
188
189 admin.site.register(HarvestLog, HarvestLogAdmin)