ajustement nouveaux logs de moissons et fix pour ajuster a la page d'infos
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / admin.py
CommitLineData
6ef8ead4 1# -*- encoding: utf-8 -*-
23b5b3d5 2import re
6ef8ead4 3from django.contrib import admin
23b5b3d5 4from django.utils.safestring import mark_safe
d972b61d 5from models import SourceActualite, Actualite, Discipline, Evenement, Record, ListSet, HarvestLog
23b5b3d5 6from savoirs.globals import META
da9020f3 7from savoirs.lib.backend import Backend
6ef8ead4 8
92c7413b 9admin.site.register(Actualite)
79c398f6 10admin.site.register(SourceActualite)
92c7413b
CR
11admin.site.register(Discipline)
12admin.site.register(Evenement)
6ef8ead4 13
23b5b3d5 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
18from django import forms
19class 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
d972b61d 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
23b5b3d5 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)
d972b61d 41
23b5b3d5 42 return mark_safe(output)
43
44 def value_from_datadict(self, data, files, name):
45 return self.original_value
46
47class 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
65class 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',
c88d78dc 82 'validated',
23b5b3d5 83 ]
84
85 search_fields = []
86 readonly_fields = []
da9020f3 87
c88d78dc 88 list_filter = ('server', 'validated')
da9020f3 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',
23b5b3d5 100 #'contributor',
101 #'language',
102 #'publisher',
da9020f3 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',
c88d78dc 113 'validated',
da9020f3 114 )
115
23b5b3d5 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()
d972b61d 121 self.readonly_fields.append('listsets')
23b5b3d5 122 super(RecordAdmin, self).__init__(*args, **kwargs)
123
da9020f3 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 """ """
23b5b3d5 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
da9020f3 136
137admin.site.register(Record, RecordAdmin)
138
d972b61d 139class ListSetAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
9eda5d6c 140 fields = ['spec', 'name', 'server', 'validated' ]
d972b61d 141 list_display = fields
142 readonly_fields = ['spec', 'name', 'server',]
143 list_filter = ('server',)
144
145admin.site.register(ListSet, ListSetAdmin)
146
23b5b3d5 147class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
a85ba76e 148 fields = ['context', 'name', 'added', 'updated', 'processed', 'record']
23b5b3d5 149 list_display = fields + ['date']
150 admin_order_fields = ['date']
151 search_fields = fields
152 readonly_fields = fields
153 list_filter = ('context',)
154
155admin.site.register(HarvestLog, HarvestLogAdmin)