Routeur: Ajout temporaire des modules workflow et dae en écriture
[auf_rh_dae.git] / src / auf.django.metadata / auf / django / metadata / admin.py
CommitLineData
13ce8dd5
OL
1# -*- encoding: utf-8 -*-
2
3import datetime
13ce8dd5
OL
4
5AUF_METADATA_READONLY_FIELDS = ('supprime',
6 'date_creation',
7 'user_creation',
8 'date_modification',
9 'user_modification',
33c8ffa1 10 )
13ce8dd5 11
f614ca5c 12AUF_METADATA_FIELDSET_FIELDS = ('date_creation', 'user_creation',
13ce8dd5 13 'date_modification', 'user_modification',
33c8ffa1 14 )
13ce8dd5
OL
15
16class AUFMetadataInlineAdminMixin(object):
f614ca5c 17 exclude = AUF_METADATA_READONLY_FIELDS
13ce8dd5
OL
18
19
20class AUFMetadataAdminMixin(object):
21 """
22 Surcharge l'admin de base, pour setter automatiquement les metadata.
23 """
13ce8dd5
OL
24 readonly_fields = AUF_METADATA_READONLY_FIELDS
25 fieldsets = (
26 ('Méta-données', {
27 'classes': ('collapse',),
28 'fields': AUF_METADATA_FIELDSET_FIELDS,
29 }),
30 )
31
13ce8dd5
OL
32 def save_model(self, request, obj, form, change):
33 if obj.user_creation is None:
34 obj.user_creation = request.user
35 obj.date_creation = datetime.datetime.now()
36 obj.user_modification = request.user
37 obj.date_modification = datetime.datetime.now()
13ce8dd5
OL
38 obj.save()
39
40 def has_metadata(self, instance):
41 """
42 Test si une instance de modele hérite bien de AUFMetadata.
43 """
44 test = getattr(instance, 'supprime', None)
45 return test is not None
46
47 def save_formset(self, request, form, formset, change):
48 """
49 Mettre les metadata si l'objet est créé ou modifié par inline.
50 """
51 instances = formset.save(commit=False)
52 for instance in instances:
53 if self.has_metadata(instance):
54 self.save_model(request, instance, formset.form, change)
55 else:
56 instance.save()
57 formset.save_m2m()