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