models.py 5.01 KB
Newer Older
Skia's avatar
Skia committed
1
from django.db import models, DataError
Skia's avatar
Skia committed
2
3
4
5
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.core.urlresolvers import reverse

6
from counter.models import Counter, Product
7
from core.models import User
Skia's avatar
Skia committed
8
from subscription.models import Subscriber
9
from subscription.views import get_subscriber
10
from club.models import Club
Skia's avatar
Skia committed
11
12
13
14
15

# Create your models here.

class Launderette(models.Model):
    name = models.CharField(_('name'), max_length=30)
16
    counter = models.OneToOneField(Counter, verbose_name=_('counter'), related_name='launderette')
Skia's avatar
Skia committed
17
18
19
20
21
22
23
24

    class Meta:
        verbose_name = _('Launderette')

    def is_owned_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
25
26
27
        launderette_club = Club.objects.filter(unix_name=settings.SITH_LAUNDERETTE_MANAGER['unix_name']).first()
        m = launderette_club.get_membership_for(user)
        if m and m.role >= 9:
Skia's avatar
Skia committed
28
29
30
            return True
        return False

31
    def can_be_edited_by(self, user):
32
33
34
35
36
        launderette_club = Club.objects.filter(unix_name=settings.SITH_LAUNDERETTE_MANAGER['unix_name']).first()
        m = launderette_club.get_membership_for(user)
        if m and m.role >= 2:
            return True
        return False
37

Skia's avatar
Skia committed
38
39
40
41
42
43
44
45
46
    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):
        return reverse('launderette:launderette_list')

Sli's avatar
Sli committed
47
48
49
50
51
52
53
54
55
56
57
58
    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()]

Skia's avatar
Skia committed
59
60
61
class Machine(models.Model):
    name = models.CharField(_('name'), max_length=30)
    launderette = models.ForeignKey(Launderette, related_name='machines', verbose_name=_('launderette'))
Skia's avatar
Skia committed
62
    type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)
Skia's avatar
Skia committed
63
64
65
66
67
68
69
70
71
    is_working = models.BooleanField(_('is working'), default=True)

    class Meta:
        verbose_name = _('Machine')

    def is_owned_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
72
73
74
        launderette_club = Club.objects.filter(unix_name=settings.SITH_LAUNDERETTE_MANAGER['unix_name']).first()
        m = launderette_club.get_membership_for(user)
        if m and m.role >= 9:
Skia's avatar
Skia committed
75
76
77
            return True
        return False

Skia's avatar
Skia committed
78
    def __str__(self):
79
80
81
        return "%s %s" % (self._meta.verbose_name, self.name)

    def get_absolute_url(self):
Skia's avatar
Skia committed
82
        return reverse('launderette:launderette_admin', kwargs={"launderette_id": self.launderette.id})
Skia's avatar
Skia committed
83

Skia's avatar
Skia committed
84
class Token(models.Model):
85
    name = models.CharField(_('name'), max_length=5)
Skia's avatar
Skia committed
86
    launderette = models.ForeignKey(Launderette, related_name='tokens', verbose_name=_('launderette'))
Skia's avatar
Skia committed
87
    type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)
88
89
    borrow_date = models.DateTimeField(_('borrow date'), null=True, blank=True)
    user = models.ForeignKey(Subscriber, related_name='tokens', verbose_name=_('user'), null=True, blank=True)
Skia's avatar
Skia committed
90
91
92

    class Meta:
        verbose_name = _('Token')
93
        unique_together = ('name', 'launderette', 'type')
Skia's avatar
Skia committed
94
95
96
97
98
99
100
        ordering = ['type', 'name']

    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
101
102
103
104
105

    def is_owned_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
106
107
108
        launderette_club = Club.objects.filter(unix_name=settings.SITH_LAUNDERETTE_MANAGER['unix_name']).first()
        m = launderette_club.get_membership_for(user)
        if m and m.role >= 9:
Skia's avatar
Skia committed
109
110
111
            return True
        return False

Skia's avatar
Skia committed
112
113
114
    def __str__(self):
        return self.__class__._meta.verbose_name + " " + self.get_type_display() + " #" + self.name + " (" + self.launderette.name + ")"

Sli's avatar
Sli committed
115
116
117
118
119
120
    def is_avaliable(self):
        if not self.borrow_date and not self.user:
            return True
        else:
            return False

Skia's avatar
Skia committed
121
122
class Slot(models.Model):
    start_date = models.DateTimeField(_('start date'))
Skia's avatar
Skia committed
123
    type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)
Skia's avatar
Skia committed
124
    machine = models.ForeignKey(Machine, related_name='slots', verbose_name=_('machine'))
Skia's avatar
Skia committed
125
    token = models.ForeignKey(Token, related_name='slots', verbose_name=_('token'), blank=True, null=True)
Skia's avatar
Skia committed
126
127
    user = models.ForeignKey(Subscriber, related_name='slots', verbose_name=_('user'))

Skia's avatar
Skia committed
128
129
    class Meta:
        verbose_name = _('Slot')
130
        ordering = ['start_date']
Skia's avatar
Skia committed
131
132

    def __str__(self):
133
134
        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)
Skia's avatar
Skia committed
135
136