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
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
da9020f3 10from savoirs.lib.backend import Backend
6ef8ead4 11
92c7413b 12admin.site.register(Actualite)
79c398f6 13admin.site.register(SourceActualite)
92c7413b
CR
14admin.site.register(Discipline)
15admin.site.register(Evenement)
6ef8ead4 16
23b5b3d5 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
d972b61d 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
23b5b3d5 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)
d972b61d 44
23b5b3d5 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',
e3c3296e 85 'pays',
86 'regions',
c88d78dc 87 'validated',
23b5b3d5 88 ]
89
90 search_fields = []
91 readonly_fields = []
da9020f3 92
e3c3296e 93 list_filter = ('server', 'validated', 'pays', 'regions')
da9020f3 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',
23b5b3d5 105 #'contributor',
106 #'language',
107 #'publisher',
da9020f3 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',
c88d78dc 118 'validated',
da9020f3 119 )
e3c3296e 120 actions = ['assigner_pays',
121 'assigner_regions',
122 'assigner_disciplines',
123 'assigner_thematiques']
da9020f3 124
e3c3296e 125 # fonctions pour présenter l'information
23b5b3d5 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()
d972b61d 131 self.readonly_fields.append('listsets')
23b5b3d5 132 super(RecordAdmin, self).__init__(*args, **kwargs)
133
da9020f3 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 """ """
23b5b3d5 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
da9020f3 146
e3c3296e 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
da9020f3 164admin.site.register(Record, RecordAdmin)
165
d972b61d 166class ListSetAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
9eda5d6c 167 fields = ['spec', 'name', 'server', 'validated' ]
d972b61d 168 list_display = fields
169 readonly_fields = ['spec', 'name', 'server',]
170 list_filter = ('server',)
171
172admin.site.register(ListSet, ListSetAdmin)
173
23b5b3d5 174class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
a85ba76e 175 fields = ['context', 'name', 'added', 'updated', 'processed', 'record']
23b5b3d5 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)