retrait import inutile
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / admin.py
CommitLineData
6ef8ead4 1# -*- encoding: utf-8 -*-
23b5b3d5 2import re
e3c3296e 3
6ef8ead4 4from django.contrib import admin
23b5b3d5 5from django.utils.safestring import mark_safe
e3c3296e 6from django.http import HttpResponseRedirect
7
d972b61d 8from models import SourceActualite, Actualite, Discipline, Evenement, Record, ListSet, HarvestLog
23b5b3d5 9from savoirs.globals import META
6ef8ead4 10
92c7413b 11admin.site.register(Actualite)
79c398f6 12admin.site.register(SourceActualite)
92c7413b
CR
13admin.site.register(Discipline)
14admin.site.register(Evenement)
6ef8ead4 15
23b5b3d5 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
20from django import forms
21class 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
d972b61d 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
23b5b3d5 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)
d972b61d 43
23b5b3d5 44 return mark_safe(output)
45
46 def value_from_datadict(self, data, files, name):
47 return self.original_value
48
49class 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
67class 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',
e3c3296e 84 'pays',
85 'regions',
c88d78dc 86 'validated',
23b5b3d5 87 ]
88
89 search_fields = []
90 readonly_fields = []
da9020f3 91
9692b801 92 list_filter = (
93 'server',
94 'validated',
95 'pays',
96 'regions',
97 'disciplines',
98 'thematiques',
99 )
da9020f3 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',
23b5b3d5 111 #'contributor',
112 #'language',
113 #'publisher',
da9020f3 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',
c88d78dc 124 'validated',
da9020f3 125 )
e3c3296e 126 actions = ['assigner_pays',
127 'assigner_regions',
128 'assigner_disciplines',
129 'assigner_thematiques']
da9020f3 130
e3c3296e 131 # fonctions pour présenter l'information
23b5b3d5 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()
d972b61d 137 self.readonly_fields.append('listsets')
23b5b3d5 138 super(RecordAdmin, self).__init__(*args, **kwargs)
139
da9020f3 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 """ """
23b5b3d5 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
da9020f3 152
e3c3296e 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
da9020f3 170admin.site.register(Record, RecordAdmin)
171
d972b61d 172class ListSetAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
9eda5d6c 173 fields = ['spec', 'name', 'server', 'validated' ]
d972b61d 174 list_display = fields
175 readonly_fields = ['spec', 'name', 'server',]
176 list_filter = ('server',)
177
178admin.site.register(ListSet, ListSetAdmin)
179
23b5b3d5 180class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
a85ba76e 181 fields = ['context', 'name', 'added', 'updated', 'processed', 'record']
23b5b3d5 182 list_display = fields + ['date']
183 admin_order_fields = ['date']
184 search_fields = fields
185 readonly_fields = fields
186 list_filter = ('context',)
187
188admin.site.register(HarvestLog, HarvestLogAdmin)