closed #444 ajout traitement par lot de disciplines, thematiques, pays, regions
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / admin.py
index 05a0a7c..ea18bfa 100644 (file)
@@ -1,6 +1,12 @@
 # -*- encoding: utf-8 -*-
+import re
+
 from django.contrib import admin
-from models import SourceActualite, Actualite, Discipline, Evenement, Record
+from django.utils.safestring import mark_safe
+from django.http import HttpResponseRedirect
+
+from models import SourceActualite, Actualite, Discipline, Evenement, Record, ListSet, HarvestLog
+from savoirs.globals import META
 from savoirs.lib.backend import Backend
 
 admin.site.register(Actualite)
@@ -8,9 +14,83 @@ admin.site.register(SourceActualite)
 admin.site.register(Discipline)
 admin.site.register(Evenement)
 
+# Ces deux classes permettent d'implémenter la possibilité d'avoir un champs readonly_fields
+# dans l.administration.
+# Ce champs est devenu natif à partir de la version 1.2
+# http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields
+from django import forms
+class ReadOnlyWidget(forms.Widget):
+    def __init__(self, original_value, display_value):
+        self.original_value = original_value
+        self.display_value = display_value
 
-class RecordAdmin(admin.ModelAdmin):
-    list_filter = ('server',)
+        super(ReadOnlyWidget, self).__init__()
+
+    def render(self, name, value, attrs=None):
+        if self.display_value is not None:
+            output = self.display_value
+        else:
+            output = unicode(self.original_value)
+
+        # pour les relations
+        try:
+            output = ", ".join([ls.name for ls in self.original_value.get_query_set()])
+        except:
+            pass
+
+        is_url = re.match('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', output)
+        if is_url:
+            output = "<a target='_blank' href='%s'>%s</a>" % (output, output)
+
+        return mark_safe(output)
+
+    def value_from_datadict(self, data, files, name):
+        return self.original_value
+
+class ReadOnlyAdminFields(object):
+    def get_form(self, request, obj=None):
+        form = super(ReadOnlyAdminFields, self).get_form(request, obj)
+
+        if hasattr(self, 'readonly_fields'):
+            for field_name in self.readonly_fields:
+                if field_name in form.base_fields:
+
+                    if hasattr(obj, 'get_%s_display' % field_name):
+                        display_value = getattr(obj, 'get_%s_display' % field_name)()
+                    else:
+                        display_value = None
+
+                    form.base_fields[field_name].widget = ReadOnlyWidget(getattr(obj, field_name, ''), display_value)
+                    form.base_fields[field_name].required = False
+        return form
+
+
+class RecordAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
+    fields = [
+        'server',
+        'title',
+        'creator',
+        'description',
+        'modified',
+        'identifier',
+        'uri',
+        'source',
+        'contributor',
+        'publisher',
+        'type',
+        'format',
+        'language',
+        'disciplines',
+        'thematiques',
+        'pays',
+        'regions',
+        'validated',
+        ]
+
+    search_fields = []
+    readonly_fields = []
+
+    list_filter = ('server', 'validated', 'pays', 'regions')
     list_display = (
       #OAI et extra AUF
       'title',
@@ -22,9 +102,9 @@ class RecordAdmin(admin.ModelAdmin):
       #'source',
       'modified',
       'creator',
-      'contributor',
-      'language',
-      'publisher',
+      #'contributor',
+      #'language',
+      #'publisher',
       'format',
       'type',
     
@@ -35,8 +115,22 @@ class RecordAdmin(admin.ModelAdmin):
        #'issued',
        #'isbn',
        #'orig_lang',
+       'validated',
     )
+    actions = ['assigner_pays',
+               'assigner_regions',
+               'assigner_disciplines',
+               'assigner_thematiques']
 
+    # fonctions pour présenter l'information
+    def __init__(self, *args, **kwargs):
+        """Surcharge l'initialisation pour définir les champs de recherche dynamiquement,
+        et les champs en lecture seule uniquement."""
+        self.search_fields = META.keys()
+        self.readonly_fields = META.keys()
+        self.readonly_fields.append('listsets')
+        super(RecordAdmin, self).__init__(*args, **kwargs) 
+    
     def _uri(self, obj):
         """ """
         return "<a target='_blank' href='%s'>%s</a>" % (obj.uri, obj.uri)
@@ -44,7 +138,45 @@ class RecordAdmin(admin.ModelAdmin):
 
     def _description(self, obj):
         """ """
-        return "%s..." % obj.description[:140]
+        max = 140
+        if obj.description is not None and len(obj.description) > max:       
+            return "%s..." % obj.description[:max]
+        else:
+            return obj.description
+
+    # actions
+    def assigner_pays(self, request, queryset):
+        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
+        return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('pays', ",".join(selected)))
+
+    def assigner_regions(self, request, queryset):
+        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
+        return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('regions', ",".join(selected)))
+
+    def assigner_thematiques(self, request, queryset):
+        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
+        return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('thematiques', ",".join(selected)))
+
+    def assigner_disciplines(self, request, queryset):
+        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
+        return HttpResponseRedirect("/admin/assigner_%s?ids=%s" % ('disciplines', ",".join(selected)))
 
 admin.site.register(Record, RecordAdmin)
 
+class ListSetAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
+    fields = ['spec', 'name', 'server', 'validated' ]
+    list_display = fields
+    readonly_fields = ['spec', 'name', 'server',]
+    list_filter = ('server',)
+
+admin.site.register(ListSet, ListSetAdmin)
+
+class HarvestLogAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
+    fields = ['context', 'name', 'added', 'updated', 'processed', 'record']
+    list_display = fields + ['date']
+    admin_order_fields = ['date']
+    search_fields = fields
+    readonly_fields = fields
+    list_filter = ('context',)
+
+admin.site.register(HarvestLog, HarvestLogAdmin)