Routeur: Ajout temporaire des modules workflow et dae en écriture
[auf_rh_dae.git] / project / rh / management / commands / rh.py
1 # -*- encoding: utf-8 -*-
2
3 import datetime
4 from django import forms
5 from django.core.management.base import BaseCommand
6 from project.rh.models import Poste, Employe, Dossier
7
8
9 def flag_vacant():
10 """
11 Mettre a jour le booleen de poste pour savoir si le poste
12 est vacant.
13 """
14 today = datetime.date.today()
15
16 for p in Poste.objects.all():
17 test = p.vacant
18 employes = p.occupe_par()
19 if len(employes) > 0:
20 vacant = False
21 else:
22 vacant = True
23
24 if p.date_fin is not None and p.date_fin < today:
25 vacant = None
26
27 if test != vacant:
28 p.vacant = vacant
29 p.save()
30
31 def flag_nb_postes():
32 """
33 """
34 today = datetime.date.today()
35 for e in Employe.objects.all():
36 total = 0
37 test = e.nb_postes
38 for d in e.rh_dossiers.all():
39 if d.date_fin is None or d.date_fin >= today:
40 total +=1
41 if test != total:
42 e.nb_postes = total
43 e.save()
44
45 def check_employes():
46
47 for e in Employe.objects.all():
48 dossiers = e.rh_dossiers.all()
49 for d in dossiers:
50 try:
51 d.full_clean()
52 except Exception, e:
53 print "DOSSIER %s: %s" % (d.id, d)
54 for f, msgs in e.message_dict.items():
55 for m in msgs:
56 print "* %s: %s" % (f, m)
57 print ""
58
59 try:
60 d.poste.full_clean()
61 except Exception, e:
62 print "POSTE %s: %s" % (d.poste.id, d.poste)
63 for f, msgs in e.message_dict.items():
64 for m in msgs:
65 print "* %s: %s" % (f, m)
66 print ""
67
68 def doc_permissions():
69 from project.rh import tests
70 test_classes = [klass for k, klass in tests.__dict__.items() if
71 k.endswith('Test')]
72 for klass in test_classes:
73 print klass.__name__
74 print "="*len(klass.__name__)
75 methods = [m for m in klass.__dict__ if m.startswith('test_')]
76 for m in methods:
77 fct = getattr(klass, m)
78 info = "* %s : %s" % (m, fct.__doc__.strip())
79 print info
80 print ""
81
82 class Command(BaseCommand):
83
84 def handle(self, *args, **options):
85 if args[0] == "doc_permissions":
86 doc_permissions()
87
88 if args[0] == "check_data":
89 check_employes()
90
91 if args[0] == "cron":
92 flag_vacant()
93 flag_nb_postes()
94
95 if args[0] == "vacant":
96 flag_vacant()
97 self.stdout.write("Flag vacant mis à jour\n")
98
99 if args[0] == "nb_postes":
100 flag_nb_postes()
101 self.stdout.write("Flag nb_postes mis à jour\n")