1622: Trop de requêtes lors de l'ajout d'une offre d'emploi
[auf_rh_dae.git] / project / recrutement / models.py
1 # -=- encoding: utf-8 -=-
2
3 import datetime
4 from django.contrib.auth.models import User
5 from django.core.files.storage import FileSystemStorage
6 from django.db import models
7 import settings
8 #from private_files import PrivateFileField
9
10 import datamaster_modeles.models as ref
11 from project.rh import models as rh
12
13 ### CONSTANTES
14 #NOTES
15 NOTE_MIN = 1
16 NOTE_RANGE = 1
17 NOTE_MAX = 10
18 NOTES = [(i, i) for i in range(NOTE_MIN, NOTE_MAX, NOTE_RANGE)]
19
20 # Abstracts
21 class Metadata(models.Model):
22 """Méta-données AUF.
23 Metadata.actif = flag remplaçant la suppression.
24 actif == False : objet réputé supprimé.
25 """
26 actif = models.BooleanField(default=True)
27 date_creation = models.DateField(auto_now_add=True)
28
29 class Meta:
30 abstract = True
31
32 class OffreEmploiManager(models.Manager):
33 def get_query_set(self):
34 fkeys = ('poste', 'region',)
35 return super(OffreEmploiManager, self).get_query_set().select_related(*fkeys).all()
36
37 class ProxyPoste(rh.Poste):
38 class Meta:
39 proxy = True
40
41 def __unicode__(self):
42 return '%s [%s]' % (self.nom, self.id)
43
44 class OffreEmploi(Metadata):
45 objects = OffreEmploiManager()
46 nom = models.CharField(max_length=255)
47 resume = models.TextField(verbose_name="Résumé")
48 description = models.TextField()
49 poste = models.ForeignKey(ProxyPoste, )#db_column='poste')
50 date_limite = models.DateField(verbose_name="Date limite")
51 region = models.ForeignKey(ref.Region, db_column='region',
52 verbose_name="Région")
53 bureau = models.ForeignKey(ref.Bureau, db_column='bureau', )
54
55 class Meta:
56 verbose_name_plural = "offres d'emploi"
57
58 def __unicode__(self):
59 return '%s [%s]' % (self.nom, self.id)
60
61
62 ### CANDIDAT
63
64 GENRE_CHOICES = (
65 ('M', 'Homme'),
66 ('F', 'Femme'),
67 )
68 SITUATION_CHOICES = (
69 ('C', 'Célibataire'),
70 ('F', 'Fiancé'),
71 ('M', 'Marié'),
72 ('D', 'Divorcé'),
73 )
74 STATUT_CHOICES = (
75 ('NOUV', 'Nouveau'),
76 ('REF', 'Refusé'),
77 ('SEL', 'Sélectionné'),
78 ('ACC', 'Accepté'),
79 ('REC', 'Recevable'), # Trouver une lettre plus appropriée?
80 )
81
82 class Candidat(Metadata):
83 statut = models.CharField(max_length=4, choices=STATUT_CHOICES,
84 default='NOUV')
85 offre_emploi = models.ForeignKey('OffreEmploi', db_column='offre_emploi',
86 related_name='+')
87 prenom = models.CharField(max_length=255)
88 nom = models.CharField(max_length=255)
89 genre = models.CharField(max_length=1, choices=GENRE_CHOICES)
90 nationalite = models.ForeignKey(ref.Pays,
91 db_column='nationalite',
92 related_name='+')
93 date_naissance = models.DateField(verbose_name="Date de naissance")
94 situation_famille = models.CharField(max_length=1,
95 choices=SITUATION_CHOICES)
96 nombre_dependant = models.IntegerField(verbose_name="Nombre de dépendant")
97 niveau_diplome = models.CharField(max_length=255,
98 verbose_name="Niveau du diplôme")
99 employeur_actuel = models.CharField(max_length=255)
100 poste_actuel = models.CharField(max_length=255)
101 domaine_professionnel = models.CharField(max_length=255)
102
103 # Adresse
104 adresse = models.CharField(max_length=255)
105 ville = models.CharField(max_length=255)
106 etat_province = models.CharField(max_length=255,
107 verbose_name="État/Province")
108 pays = models.ForeignKey(ref.Pays, db_column='pays',
109 related_name='+')
110
111 def __unicode__(self):
112 return '%s %s [%s]' % (self.prenom, self.nom, self.id)
113
114 ### PIECE CANDIDAT
115 # Upload de fichiers
116 storage_prive = FileSystemStorage(settings.PRIVE_MEDIA_ROOT,
117 base_url=settings.PRIVE_MEDIA_URL)
118
119 def candidat_piece_dispatch(instance, filename):
120 path = "offre_emploi/%s_%s/%s/%s_%s" % (instance.candidat.nom,
121 instance.candidat.prenom, instance.nom, instance.candidat.id,
122 filename)
123 return path
124
125 class CandidatPiece(models.Model):
126 candidat = models.ForeignKey(Candidat, db_column='candidat',
127 related_name='candidat_piece')
128 nom = models.CharField(max_length=255)
129 #path = PrivateFileField("file", upload_to=candidat_piece_dispatch)
130 path = models.FileField(verbose_name="Fichier",
131 upload_to=candidat_piece_dispatch,
132 storage=storage_prive, )
133
134 class Meta:
135 verbose_name = "pièce jointe du candidats"
136 verbose_name_plural = "pièces jointes des candidats"
137
138 def __unicode__(self):
139 return '%s' % (self.nom)
140
141 class UserProfile(models.Model):
142 user = models.ForeignKey("auth.User", unique=True,
143 verbose_name="Permission")
144 candidats = models.ManyToManyField(Candidat, verbose_name='Candidats',
145 blank=True, null=True,related_name="evaluateurs")
146 regions = models.ManyToManyField(ref.Region,
147 verbose_name="Régions", )
148
149 class Meta:
150 verbose_name = "permission"
151
152 def __unicode__(self):
153 return '%s %s' % (self.user.first_name, self.user.last_name)
154
155 UserProfile.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
156
157 class CandidatEvaluation(models.Model):
158 candidat = models.ForeignKey(Candidat, db_column='candidat',
159 related_name='+')
160 evaluateur = models.ForeignKey(UserProfile, db_column='evaluateur',
161 related_name='+', verbose_name="Évaluateur")
162 note = models.IntegerField(choices=NOTES, blank=True, null=True)
163 commentaire = models.TextField(null=True, blank=True)
164 date = models.DateField(auto_now_add=True)
165
166 class Meta:
167 verbose_name = "évaluation du candidat"
168 verbose_name_plural = "évaluations des candidats"