Project

General

Profile

Download (5.31 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / personnes / models.py @ 706c6be7

1
# -*- coding: utf-8 -*-
2

    
3
from datetime import datetime, date
4

    
5
from django.db import models
6
from django.contrib.auth.models import User
7
from django.core.exceptions import ValidationError
8
from django.template.defaultfilters import date as date_filter
9

    
10
from calebasse.ressources.models import WorkerType, Service
11
from calebasse.models import WeekdayField, BaseModelMixin
12

    
13
from interval import Interval
14

    
15
class People(BaseModelMixin, models.Model):
16
    last_name = models.CharField(max_length=128, verbose_name=u'Nom')
17
    first_name = models.CharField(max_length=128, verbose_name=u'Prénom(s)')
18
    display_name = models.CharField(max_length=256,
19
            verbose_name=u'Nom complet', editable=False)
20

    
21
    def save(self, **kwargs):
22
        self.display_name = self.first_name + ' ' + self.last_name
23
        super(People, self).save(**kwargs)
24

    
25
    def __unicode__(self):
26
        return self.display_name
27

    
28
    class Meta:
29
        ordering = ['last_name', 'first_name']
30

    
31
class WorkerManager(models.Manager):
32

    
33
    def for_service(self, service, type=None):
34
        if type:
35
            return self.filter(services__in=[service]).filter(type=type)
36
        else:
37
            return self.filter(services__in=[service])
38

    
39
class Worker(People):
40
    objects = WorkerManager()
41
    # TODO : use manytomany here ?
42
    type = models.ForeignKey('ressources.WorkerType',
43
            verbose_name=u'Type de personnel')
44
    services = models.ManyToManyField('ressources.Service')
45

    
46
    def is_away(self):
47
        day = WeekdayField.WEEKDAYS[date.today().weekday()]
48
        if self.timetable_set.filter(weekday=day).exists():
49
            return False
50
        return True
51

    
52
    class Meta:
53
        verbose_name = u'Personnel'
54
        verbose_name_plural = u'Personnels'
55

    
56

    
57
class UserWorker(BaseModelMixin, User):
58
    worker = models.ForeignKey('Worker',
59
            verbose_name=u'Personnel')
60

    
61
    def __unicode__(self):
62
        return u'Lien entre la personne %s et l\'utilisateur %s' % (
63
                self.people, super(UserWorker, self).__unicode__())
64

    
65
class SchoolTeacher(People):
66
    schools = models.ManyToManyField('ressources.School')
67
    role = models.ForeignKey('ressources.SchoolTeacherRole')
68

    
69
class TimeTableManager(models.Manager):
70
    def current(self):
71
        today = date.today()
72
        return self.filter(start_date__lte=today, end_date__gte=today)
73

    
74
class TimeTable(BaseModelMixin, models.Model):
75
    objects = TimeTableManager()
76
    worker = models.ForeignKey(Worker,
77
            verbose_name=u'Intervenant')
78
    service = models.ForeignKey('ressources.Service')
79
    weekday = WeekdayField(
80
            verbose_name=u'Jour')
81
    start_time = models.TimeField(
82
        verbose_name=u'Heure de début')
83
    end_time = models.TimeField(
84
        verbose_name=u'Heure de fin')
85
    start_date = models.DateField(
86
        verbose_name=u'Début')
87
    end_date = models.DateField(
88
        verbose_name=u'Fin', blank=True, null=True)
89

    
90
    def __unicode__(self):
91
        s = u'%s pour le %s le %s de %s à %s' % \
92
                (self.worker, self.service, self.weekday, self.start_time,
93
                        self.end_time)
94
        if self.end_time:
95
            s += u' à partir du %s' % self.start_date
96
        else:
97
            s += u' du %s au %s' % (self.start_data, self.end_date)
98
        return s
99

    
100
    class Meta:
101
        verbose_name = u'Emploi du temps'
102
        verbose_name_plural = u'Emplois du temps'
103

    
104
    def to_interval(self, date):
105
        return Interval(datetime.combine(date, self.start_time),
106
                datetime.combine(date, self.end_time))
107

    
108
class HolidayManager(models.Manager):
109
    def for_worker(self, worker):
110
        query = models.Q(worker=worker) \
111
              | models.Q(worker__isnull=True,
112
                           service=worker.services.all())
113
        return self.filter(query) \
114
                   .filter(end_date__gte=date.today())
115

    
116
    def for_service(self, service):
117
        return self.filter(worker__isnull=True, service=service) \
118
                   .filter(end_date__gte=date.today())
119

    
120

    
121
def time2french(time):
122
    if time.minute:
123
        return '{0}h{1}'.format(time.hour, time.minute)
124
    return '{0}h'.format(time.hour)
125

    
126
class Holiday(BaseModelMixin, models.Model):
127
    objects = HolidayManager()
128

    
129
    worker = models.ForeignKey(Worker, blank=True, null=True,
130
            verbose_name=u"Personnel")
131
    service = models.ForeignKey(Service, blank=True, null=True,
132
            verbose_name=u"Service")
133
    start_date = models.DateField(verbose_name=u"Date de début")
134
    end_date = models.DateField(verbose_name=u"Date de fin")
135

    
136
    class Meta:
137
        verbose_name = u'Congé'
138
        verbose_name_plural = u'Congés'
139

    
140
    def is_current(self):
141
        return self.start_date <= date.today() <= self.end_date
142

    
143
    def clean(self):
144
        if not self.worker and not self.service:
145
            raise ValidationError('at least one of worker or service must be set')
146
    def __unicode__(self):
147
        ret = ''
148
        if self.start_date == self.end_date:
149
            ret = u'le {0}'.format(date_filter(self.start_date, 'j F Y'))
150
            if self.start_time and self.end_time:
151
                ret += u', de {0} à {1}'.format(time2french(self.start_time),
152
                time2french(self.end_time))
153
        else:
154
            ret = u'du {0} au {1}'.format(
155
                    date_filter(self.start_date, 'j F'),
156
                    date_filter(self.end_date, 'j F Y'))
157
        return ret
158

    
(4-4/7)