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