a3f880461e6e2f80033379cc6e01bd457b919a13
[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, 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 is_url = re.match('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', output)
33 if is_url:
34 output = "<a target='_blank' href='%s'>%s</a>" % (output, output)
35 return mark_safe(output)
36
37 def value_from_datadict(self, data, files, name):
38 return self.original_value
39
40 class ReadOnlyAdminFields(object):
41 def get_form(self, request, obj=None):
42 form = super(ReadOnlyAdminFields, self).get_form(request, obj)
43
44 if hasattr(self, 'readonly_fields'):
45 for field_name in self.readonly_fields:
46 if field_name in form.base_fields:
47
48 if hasattr(obj, 'get_%s_display' % field_name):
49 display_value = getattr(obj, 'get_%s_display' % field_name)()
50 else:
51 display_value = None
52
53 form.base_fields[field_name].widget = ReadOnlyWidget(getattr(obj, field_name, ''), display_value)
54 form.base_fields[field_name].required = False
55 return form
56
57
58 class RecordAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
59 fields = [
60 'server',
61 'title',
62 'creator',
63 'description',
64 'modified',
65 'identifier',
66 'uri',
67 'source',
68 'contributor',
69 'publisher',
70 'type',
71 'format',
72 'language',
73 'disciplines',
74 'thematiques',
75 ]
76
77 search_fields = []
78 readonly_fields = []
79
80 list_filter = ('server',)
81 list_display = (
82 #OAI et extra AUF
83 'title',
84 'subject',
85 '_description',
86 '_uri',
87 #'server',
88 'identifier',
89 #'source',
90 'modified',
91 'creator',
92 #'contributor',
93 #'language',
94 #'publisher',
95 'format',
96 'type',
97
98 #SEP 2 (aucune données récoltées)
99 #'alt_title',
100 #'abstract',
101 #'creation',
102 #'issued',
103 #'isbn',
104 #'orig_lang',
105 )
106
107 def __init__(self, *args, **kwargs):
108 """Surcharge l'initialisation pour définir les champs de recherche dynamiquement,
109 et les champs en lecture seule uniquement."""
110 self.search_fields = META.keys()
111 self.readonly_fields = META.keys()
112 super(RecordAdmin, self).__init__(*args, **kwargs)
113
114 def _uri(self, obj):
115 """ """
116 return "<a target='_blank' href='%s'>%s</a>" % (obj.uri, obj.uri)
117 _uri.allow_tags = True
118
119 def _description(self, obj):
120 """ """
121 max = 140
122 if obj.description is not None and len(obj.description) > max:
123 return "%s..." % obj.description[:max]
124 else:
125 return obj.description
126
127 admin.site.register(Record, RecordAdmin)
128
129 class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
130 fields = ['context', 'name', 'added', 'updated', 'record']
131 list_display = fields + ['date']
132 admin_order_fields = ['date']
133 search_fields = fields
134 readonly_fields = fields
135 list_filter = ('context',)
136
137 admin.site.register(HarvestLog, HarvestLogAdmin)