check data fct
[auf_rh_dae.git] / project / rh / management / commands / rh.py
CommitLineData
4c53dda4
OL
1# -*- encoding: utf-8 -*-
2
e4cec79e 3import datetime
692d25e9
OL
4from django import forms
5from django.core.management.base import BaseCommand
6from project.rh.models import Poste, Employe, Dossier
4c53dda4
OL
7
8
9def flag_vacant():
10 """
11 Mettre a jour le booleen de poste pour savoir si le poste
12 est vacant.
13 """
e4cec79e
OL
14 today = datetime.date.today()
15
4c53dda4
OL
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
e4cec79e 24 if p.date_fin is not None and p.date_fin < today:
bdc6d9ff
OL
25 vacant = None
26
4c53dda4
OL
27 if test != vacant:
28 p.vacant = vacant
29 p.save()
30
a45e414b
OL
31def flag_nb_postes():
32 """
33 """
657337fb 34 today = datetime.date.today()
a45e414b
OL
35 for e in Employe.objects.all():
36 total = 0
37 test = e.nb_postes
38 for d in e.rh_dossiers.all():
657337fb 39 if d.date_fin is None or d.date_fin >= today:
a45e414b
OL
40 total +=1
41 if test != total:
42 e.nb_postes = total
43 e.save()
44
692d25e9
OL
45def 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
4c53dda4
OL
68
69class Command(BaseCommand):
70
71 def handle(self, *args, **options):
a45e414b 72
692d25e9
OL
73 if args[0] == "check_data":
74 check_employes()
75
31a784bf
OL
76 if args[0] == "cron":
77 flag_vacant()
78 flag_nb_postes()
79
4c53dda4
OL
80 if args[0] == "vacant":
81 flag_vacant()
82 self.stdout.write("Flag vacant mis à jour\n")
a45e414b
OL
83
84 if args[0] == "nb_postes":
85 flag_nb_postes()
86 self.stdout.write("Flag nb_postes mis à jour\n")