metadata mod in project src
[auf_rh_dae.git] / src / auf.django.metadata / auf / django / metadata / admin.py
CommitLineData
13ce8dd5
OL
1# -*- encoding: utf-8 -*-
2
3import datetime
4from django.contrib import admin
5from django.contrib import messages
6from django.shortcuts import redirect
7from models import AUFMetadata
8
9AUF_METADATA_READONLY_FIELDS = ('supprime',
10 'date_creation',
11 'user_creation',
12 'date_modification',
13 'user_modification',
14 'date_activation',
15 'user_activation',
16 'date_desactivation',
17 'user_desactivation', )
18
19AUF_METADATA_FIELDSET_FIELDS = ('actif', 'date_creation', 'user_creation',
20 'date_modification', 'user_modification',
21 'date_activation', 'user_activation',
22 'date_desactivation', 'user_desactivation',)
23
24class AUFMetadataInlineAdminMixin(object):
25 exclude = AUF_METADATA_READONLY_FIELDS + ('actif', )
26
27
28class AUFMetadataAdminMixin(object):
29 """
30 Surcharge l'admin de base, pour setter automatiquement les metadata.
31 """
32 actions = ['desactiver', ]
33
34 readonly_fields = AUF_METADATA_READONLY_FIELDS
35 fieldsets = (
36 ('Méta-données', {
37 'classes': ('collapse',),
38 'fields': AUF_METADATA_FIELDSET_FIELDS,
39 }),
40 )
41
42 def desactiver(modeladmin, request, queryset):
43 """
44 Passe à actif = False tous les objets du QS
45 """
46 selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
47 queryset.update(actif=False)
48 messages.add_message(request, messages.INFO, u'Les %s ont été désactivés.' % queryset.model._meta.verbose_name_plural)
49 info = queryset.model._meta.app_label, queryset.model._meta.module_name
50 return redirect('admin:%s_%s_changelist' % info)
51 desactiver.short_description = u"Désactiver les éléments sélectionnés"
52
53 def save_model(self, request, obj, form, change):
54 if obj.user_creation is None:
55 obj.user_creation = request.user
56 obj.date_creation = datetime.datetime.now()
57 obj.user_modification = request.user
58 obj.date_modification = datetime.datetime.now()
59 initial = getattr(form, 'initial', None)
60 if initial:
61 if form.initial['actif'] is True and obj.actif is False:
62 obj.user_desactivation = request.user
63 obj.date_desactivation = datetime.datetime.now()
64 if form.initial['actif'] is False and obj.actif is True:
65 obj.user_activation = request.user
66 obj.date_activation = datetime.datetime.now()
67 obj.save()
68
69 def has_metadata(self, instance):
70 """
71 Test si une instance de modele hérite bien de AUFMetadata.
72 """
73 test = getattr(instance, 'supprime', None)
74 return test is not None
75
76 def save_formset(self, request, form, formset, change):
77 """
78 Mettre les metadata si l'objet est créé ou modifié par inline.
79 """
80 instances = formset.save(commit=False)
81 for instance in instances:
82 if self.has_metadata(instance):
83 self.save_model(request, instance, formset.form, change)
84 else:
85 instance.save()
86 formset.save_m2m()
87
88
89 def changelist_view(self, request, extra_context=None):
90 """
91 Sans filtre sélectionné, on redirige vers les éléments actifs
92 """
93 url = request.get_full_path()
94 has_filter = "?" in url
95 if has_filter is False:
96 q = request.GET.copy()
97 q['actif__exact'] = 1
98 return redirect("%s?%s" % (url, q.urlencode()))
99 return super(AUFMetadataAdminMixin, self).changelist_view(request, extra_context=extra_context)
100