models.py 6.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#

Skia's avatar
Skia committed
25
from django.db import models, DataError
Skia's avatar
Skia committed
26
27
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
28
from django.urls import reverse
Skia's avatar
Skia committed
29

Krophil's avatar
Krophil committed
30
from counter.models import Counter
31
32
from core.models import User
from club.models import Club
Skia's avatar
Skia committed
33
34
35

# Create your models here.

Krophil's avatar
Krophil committed
36

Skia's avatar
Skia committed
37
class Launderette(models.Model):
Sli's avatar
Sli committed
38
39
    name = models.CharField(_("name"), max_length=30)
    counter = models.OneToOneField(
40
41
42
43
        Counter,
        verbose_name=_("counter"),
        related_name="launderette",
        on_delete=models.CASCADE,
Sli's avatar
Sli committed
44
    )
Skia's avatar
Skia committed
45
46

    class Meta:
Sli's avatar
Sli committed
47
        verbose_name = _("Launderette")
Skia's avatar
Skia committed
48
49
50
51
52

    def is_owned_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
Sli's avatar
Sli committed
53
54
55
        launderette_club = Club.objects.filter(
            unix_name=settings.SITH_LAUNDERETTE_MANAGER["unix_name"]
        ).first()
56
57
        m = launderette_club.get_membership_for(user)
        if m and m.role >= 9:
Skia's avatar
Skia committed
58
59
60
            return True
        return False

61
    def can_be_edited_by(self, user):
Sli's avatar
Sli committed
62
63
64
        launderette_club = Club.objects.filter(
            unix_name=settings.SITH_LAUNDERETTE_MANAGER["unix_name"]
        ).first()
65
66
67
68
        m = launderette_club.get_membership_for(user)
        if m and m.role >= 2:
            return True
        return False
69

Skia's avatar
Skia committed
70
71
72
73
74
75
76
    def can_be_viewed_by(self, user):
        return user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
Sli's avatar
Sli committed
77
        return reverse("launderette:launderette_list")
Skia's avatar
Skia committed
78

Sli's avatar
Sli committed
79
80
81
82
83
84
85
86
87
88
89
90
    def get_machine_list(self):
        return Machine.objects.filter(launderette_id=self.id)

    def machine_list(self):
        return [m.id for m in self.get_machine_list()]

    def get_token_list(self):
        return Token.objects.filter(launderette_id=self.id)

    def token_list(self):
        return [t.id for t in self.get_token_list()]

Krophil's avatar
Krophil committed
91

Skia's avatar
Skia committed
92
class Machine(models.Model):
Sli's avatar
Sli committed
93
94
    name = models.CharField(_("name"), max_length=30)
    launderette = models.ForeignKey(
95
96
97
98
        Launderette,
        related_name="machines",
        verbose_name=_("launderette"),
        on_delete=models.CASCADE,
Sli's avatar
Sli committed
99
100
101
102
103
    )
    type = models.CharField(
        _("type"), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES
    )
    is_working = models.BooleanField(_("is working"), default=True)
Skia's avatar
Skia committed
104
105

    class Meta:
Sli's avatar
Sli committed
106
        verbose_name = _("Machine")
Skia's avatar
Skia committed
107
108
109
110
111

    def is_owned_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
Sli's avatar
Sli committed
112
113
114
        launderette_club = Club.objects.filter(
            unix_name=settings.SITH_LAUNDERETTE_MANAGER["unix_name"]
        ).first()
115
116
        m = launderette_club.get_membership_for(user)
        if m and m.role >= 9:
Skia's avatar
Skia committed
117
118
119
            return True
        return False

Skia's avatar
Skia committed
120
    def __str__(self):
121
122
123
        return "%s %s" % (self._meta.verbose_name, self.name)

    def get_absolute_url(self):
Sli's avatar
Sli committed
124
125
126
127
        return reverse(
            "launderette:launderette_admin",
            kwargs={"launderette_id": self.launderette.id},
        )
Skia's avatar
Skia committed
128

Krophil's avatar
Krophil committed
129

Skia's avatar
Skia committed
130
class Token(models.Model):
Sli's avatar
Sli committed
131
132
    name = models.CharField(_("name"), max_length=5)
    launderette = models.ForeignKey(
133
134
135
136
        Launderette,
        related_name="tokens",
        verbose_name=_("launderette"),
        on_delete=models.CASCADE,
Sli's avatar
Sli committed
137
138
139
140
141
142
    )
    type = models.CharField(
        _("type"), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES
    )
    borrow_date = models.DateTimeField(_("borrow date"), null=True, blank=True)
    user = models.ForeignKey(
143
144
145
146
147
148
        User,
        related_name="tokens",
        verbose_name=_("user"),
        null=True,
        blank=True,
        on_delete=models.CASCADE,
Sli's avatar
Sli committed
149
    )
Skia's avatar
Skia committed
150
151

    class Meta:
Sli's avatar
Sli committed
152
153
154
        verbose_name = _("Token")
        unique_together = ("name", "launderette", "type")
        ordering = ["type", "name"]
Skia's avatar
Skia committed
155
156
157
158
159
160

    def save(self, *args, **kwargs):
        if self.name == "":
            raise DataError(_("Token name can not be blank"))
        else:
            super(Token, self).save(*args, **kwargs)
Skia's avatar
Skia committed
161
162
163
164
165

    def is_owned_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
Sli's avatar
Sli committed
166
167
168
        launderette_club = Club.objects.filter(
            unix_name=settings.SITH_LAUNDERETTE_MANAGER["unix_name"]
        ).first()
169
170
        m = launderette_club.get_membership_for(user)
        if m and m.role >= 9:
Skia's avatar
Skia committed
171
172
173
            return True
        return False

Skia's avatar
Skia committed
174
    def __str__(self):
Sli's avatar
Sli committed
175
176
177
178
179
180
181
182
183
184
        return (
            self.__class__._meta.verbose_name
            + " "
            + self.get_type_display()
            + " #"
            + self.name
            + " ("
            + self.launderette.name
            + ")"
        )
Skia's avatar
Skia committed
185

Sli's avatar
Sli committed
186
187
188
189
190
191
    def is_avaliable(self):
        if not self.borrow_date and not self.user:
            return True
        else:
            return False

Krophil's avatar
Krophil committed
192

Skia's avatar
Skia committed
193
class Slot(models.Model):
Sli's avatar
Sli committed
194
195
196
197
198
    start_date = models.DateTimeField(_("start date"))
    type = models.CharField(
        _("type"), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES
    )
    machine = models.ForeignKey(
199
200
201
202
        Machine,
        related_name="slots",
        verbose_name=_("machine"),
        on_delete=models.CASCADE,
Sli's avatar
Sli committed
203
204
    )
    token = models.ForeignKey(
205
206
207
208
209
210
211
212
213
        Token,
        related_name="slots",
        verbose_name=_("token"),
        blank=True,
        null=True,
        on_delete=models.CASCADE,
    )
    user = models.ForeignKey(
        User, related_name="slots", verbose_name=_("user"), on_delete=models.CASCADE
Sli's avatar
Sli committed
214
    )
Skia's avatar
Skia committed
215

Skia's avatar
Skia committed
216
    class Meta:
Sli's avatar
Sli committed
217
218
        verbose_name = _("Slot")
        ordering = ["start_date"]
Skia's avatar
Skia committed
219

Skia's avatar
Skia committed
220
221
222
    def is_owned_by(self, user):
        return user == self.user

Skia's avatar
Skia committed
223
    def __str__(self):
Sli's avatar
Sli committed
224
225
226
227
228
229
230
        return "User: %s - Date: %s - Type: %s - Machine: %s - Token: %s" % (
            self.user,
            self.start_date,
            self.get_type_display(),
            self.machine.name,
            self.token,
        )