ajout de methode str pour listset
[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',
c65edab8 84 'listsets',
e3c3296e 85 'pays',
86 'regions',
c88d78dc 87 'validated',
23b5b3d5 88 ]
89
90 search_fields = []
91 readonly_fields = []
da9020f3 92
9692b801 93 list_filter = (
9692b801 94 'validated',
c65edab8 95 'server',
9692b801 96 'pays',
97 'regions',
98 'disciplines',
99 'thematiques',
100 )
da9020f3 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',
23b5b3d5 112 #'contributor',
113 #'language',
114 #'publisher',
da9020f3 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',
c88d78dc 125 'validated',
da9020f3 126 )
e3c3296e 127 actions = ['assigner_pays',
128 'assigner_regions',
129 'assigner_disciplines',
130 'assigner_thematiques']
da9020f3 131
e3c3296e 132 # fonctions pour présenter l'information
23b5b3d5 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()
d972b61d 138 self.readonly_fields.append('listsets')
23b5b3d5 139 super(RecordAdmin, self).__init__(*args, **kwargs)
140
da9020f3 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 """ """
23b5b3d5 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
da9020f3 153
e3c3296e 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
da9020f3 171admin.site.register(Record, RecordAdmin)
172
d972b61d 173class ListSetAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
9eda5d6c 174 fields = ['spec', 'name', 'server', 'validated' ]
d972b61d 175 list_display = fields
176 readonly_fields = ['spec', 'name', 'server',]
177 list_filter = ('server',)
178
179admin.site.register(ListSet, ListSetAdmin)
180
23b5b3d5 181class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
a85ba76e 182 fields = ['context', 'name', 'added', 'updated', 'processed', 'record']
23b5b3d5 183 list_display = fields + ['date']
184 admin_order_fields = ['date']
185 search_fields = fields
186 readonly_fields = fields
187 list_filter = ('context',)
188
189admin.site.register(HarvestLog, HarvestLogAdmin)