Project

General

Profile

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

calebasse / calebasse / personnes / models.py @ 1a43d77a

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

    
3
from datetime import datetime, date
4

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

    
10
import reversion
11

    
12
from calebasse.ressources.models import Service
13
from calebasse.models import WeekdayField, BaseModelMixin
14

    
15
from interval import Interval
16

    
17
from model_utils import Choices
18
from model_utils.managers import PassThroughManager
19

    
20
class People(BaseModelMixin, models.Model):
21
    GENDERS = Choices('', 'homme', 'femme')
22
    last_name = models.CharField(max_length=128, verbose_name=u'Nom')
23
    first_name = models.CharField(max_length=128, verbose_name=u'Prénom(s)')
24
    display_name = models.CharField(max_length=256,
25
            verbose_name=u'Nom complet', editable=False)
26
    gender = models.IntegerField(verbose_name=u"Genre", choices=GENDERS,
27
            max_length=2, default=0)
28

    
29
    def save(self, **kwargs):
30
        self.display_name = self.first_name + ' ' + self.last_name
31
        super(People, self).save(**kwargs)
32

    
33
    def __unicode__(self):
34
        return self.display_name
35

    
36
    class Meta:
37
        ordering = ['last_name', 'first_name']
38

    
39
class WorkerQuerySet(query.QuerySet):
40
    def for_service(self, service, type=None):
41
        if type:
42
            return self.filter(services__in=[service]).filter(type=type)
43
        else:
44
            return self.filter(services__in=[service])
45

    
46
class Worker(People):
47
    objects = PassThroughManager.for_queryset_class(WorkerQuerySet)()
48
    # TODO : use manytomany here ?
49
    type = models.ForeignKey('ressources.WorkerType',
50
            verbose_name=u'Type de personnel')
51
    services = models.ManyToManyField('ressources.Service')
52

    
53
    def is_away(self):
54
        day = WeekdayField.WEEKDAYS[date.today().weekday()]
55
        if self.timetable_set.filter(weekday=day).exists():
56
            return False
57
        return True
58

    
59
    @models.permalink
60
    def get_absolute_url(self):
61
        return ('worker_update', (), {
62
            'service': self.services.all()[0].name.lower(),
63
            'pk': self.pk })
64

    
65
    class Meta:
66
        verbose_name = u'Personnel'
67
        verbose_name_plural = u'Personnels'
68

    
69
reversion.register(Worker, follow=['people_ptr'])
70
reversion.register(User)
71

    
72
class UserWorker(BaseModelMixin, User):
73
    worker = models.ForeignKey('Worker',
74
            verbose_name=u'Personnel')
75

    
76
    def __unicode__(self):
77
        return u'Lien entre la personne %s et l\'utilisateur %s' % (
78
                self.people, super(UserWorker, self).__unicode__())
79

    
80
reversion.register(UserWorker, follow=['user_ptr'])
81

    
82
class SchoolTeacher(People):
83
    schools = models.ManyToManyField('ressources.School')
84
    role = models.ForeignKey('ressources.SchoolTeacherRole')
85

    
86
reversion.register(SchoolTeacher, follow=['user_ptr'])
87

    
88
class TimeTableQuerySet(query.QuerySet):
89
    def current(self):
90
        today = date.today()
91
        return self.filter(start_date__lte=today, end_date__gte=today)
92

    
93
class TimeTable(BaseModelMixin, models.Model):
94
    objects = PassThroughManager.for_queryset_class(TimeTableQuerySet)()
95
    worker = models.ForeignKey(Worker,
96
            verbose_name=u'Intervenant')
97
    services = models.ManyToManyField('ressources.Service')
98
    weekday = WeekdayField(
99
            verbose_name=u'Jour')
100
    start_time = models.TimeField(
101
        verbose_name=u'Heure de début')
102
    end_time = models.TimeField(
103
        verbose_name=u'Heure de fin')
104
    start_date = models.DateField(
105
        verbose_name=u'Début',
106
        help_text=u'format: jj/mm/aaaa')
107
    end_date = models.DateField(
108
        verbose_name=u'Fin', blank=True, null=True,
109
        help_text=u'format: jj/mm/aaaa')
110

    
111
    def __unicode__(self):
112
        s = u'%s pour au %s le %s de %s à %s' % \
113
                (self.worker, ', '.join(map(unicode, self.services.all())), self.weekday, self.start_time,
114
                        self.end_time)
115
        if self.end_time:
116
            s += u' à partir du %s' % self.start_date
117
        else:
118
            s += u' du %s au %s' % (self.start_data, self.end_date)
119
        return s
120

    
121
    class Meta:
122
        verbose_name = u'Emploi du temps'
123
        verbose_name_plural = u'Emplois du temps'
124

    
125
    def to_interval(self, date):
126
        return Interval(datetime.combine(date, self.start_time),
127
                datetime.combine(date, self.end_time))
128

    
129
class HolidayQuerySet(query.QuerySet):
130
    def for_worker(self, worker):
131
        filter_query = models.Q(worker=worker) \
132
              | models.Q(worker__isnull=True,
133
                           service=worker.services.all())
134
        return self.filter(filter_query)
135

    
136
    def for_service(self, service):
137
        return self.filter(worker__isnull=True) \
138
                   .filter(models.Q(service=service)
139
                          |models.Q(service__isnull=True)) \
140

    
141
    def for_service_workers(self, service):
142
        return self.filter(worker__services=service)
143

    
144
    def future(self):
145
        return self.filter(end_date__gte=date.today())
146

    
147
    def today(self):
148
        today = date.today()
149
        return self.filter(start_date__lte=today,
150
                end_date__gte=today)
151

    
152
    def for_period(self, start_date, end_date):
153
        return self.filter(start_date__lte=end_date, end_date__gte=start_date)
154

    
155
def time2french(time):
156
    if time.minute:
157
        return '{0}h{1}'.format(time.hour, time.minute)
158
    return '{0}h'.format(time.hour)
159

    
160
class Holiday(BaseModelMixin, models.Model):
161
    objects = PassThroughManager().for_queryset_class(HolidayQuerySet)()
162

    
163
    worker = models.ForeignKey(Worker, blank=True, null=True,
164
            verbose_name=u"Personnel")
165
    service = models.ForeignKey(Service, blank=True, null=True,
166
            verbose_name=u"Service")
167
    start_date = models.DateField(verbose_name=u"Date de début",
168
        help_text=u'format: jj/mm/aaaa')
169
    end_date = models.DateField(verbose_name=u"Date de fin",
170
        help_text=u'format: jj/mm/aaaa')
171
    start_time = models.TimeField(verbose_name=u"Horaire de début", blank=True,
172
            null=True)
173
    end_time = models.TimeField(verbose_name=u"Horaire de fin", blank=True,
174
            null=True)
175
    comment = models.TextField(verbose_name=u'Commentaire', blank=True)
176

    
177
    class Meta:
178
        verbose_name = u'Congé'
179
        verbose_name_plural = u'Congés'
180
        ordering = ('start_date', 'start_time')
181

    
182
    def is_current(self):
183
        return self.start_date <= date.today() <= self.end_date
184

    
185
    def __unicode__(self):
186
        ret = ''
187
        if self.start_date == self.end_date:
188
            ret = u'le {0}'.format(date_filter(self.start_date, 'j F Y'))
189
            if self.start_time and self.end_time:
190
                ret += u', de {0} à {1}'.format(time2french(self.start_time),
191
                time2french(self.end_time))
192
        else:
193
            ret = u'du {0} au {1}'.format(
194
                    date_filter(self.start_date, 'j F'),
195
                    date_filter(self.end_date, 'j F Y'))
196
        return ret
197

    
(4-4/7)