date range filter
[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
f614ca5c 19AUF_METADATA_FIELDSET_FIELDS = ('date_creation', 'user_creation',
13ce8dd5
OL
20 'date_modification', 'user_modification',
21 'date_activation', 'user_activation',
22 'date_desactivation', 'user_desactivation',)
23
24class AUFMetadataInlineAdminMixin(object):
f614ca5c 25 exclude = AUF_METADATA_READONLY_FIELDS
13ce8dd5
OL
26
27
28class AUFMetadataAdminMixin(object):
29 """
30 Surcharge l'admin de base, pour setter automatiquement les metadata.
31 """
13ce8dd5
OL
32 readonly_fields = AUF_METADATA_READONLY_FIELDS
33 fieldsets = (
34 ('Méta-données', {
35 'classes': ('collapse',),
36 'fields': AUF_METADATA_FIELDSET_FIELDS,
37 }),
38 )
39
13ce8dd5
OL
40 def save_model(self, request, obj, form, change):
41 if obj.user_creation is None:
42 obj.user_creation = request.user
43 obj.date_creation = datetime.datetime.now()
44 obj.user_modification = request.user
45 obj.date_modification = datetime.datetime.now()
13ce8dd5
OL
46 obj.save()
47
48 def has_metadata(self, instance):
49 """
50 Test si une instance de modele hérite bien de AUFMetadata.
51 """
52 test = getattr(instance, 'supprime', None)
53 return test is not None
54
55 def save_formset(self, request, form, formset, change):
56 """
57 Mettre les metadata si l'objet est créé ou modifié par inline.
58 """
59 instances = formset.save(commit=False)
60 for instance in instances:
61 if self.has_metadata(instance):
62 self.save_model(request, instance, formset.form, change)
63 else:
64 instance.save()
65 formset.save_m2m()