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

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

# Create your models here.

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

    class Meta:
        verbose_name = _('Launderette')

    def is_owned_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
        if user.is_in_group(settings.SITH_GROUPS['launderette-admin']['name']):
            return True
        return False

28 29 30 31
    def can_be_edited_by(self, user):
        sub = get_subscriber(request.user)
        return sub in self.sellers.all()

Skia's avatar
Skia committed
32 33 34 35 36 37 38 39 40 41 42 43
    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')

class Machine(models.Model):
    name = models.CharField(_('name'), max_length=30)
    launderette = models.ForeignKey(Launderette, related_name='machines', verbose_name=_('launderette'))
44
    type = models.CharField(_('type'), max_length=10, choices=[('WASHING', _('Washing')), ('DRYING', _('Drying'))])
Skia's avatar
Skia committed
45 46 47 48 49 50 51 52 53 54 55 56 57
    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
        """
        if user.is_in_group(settings.SITH_GROUPS['launderette-admin']['name']):
            return True
        return False

Skia's avatar
Skia committed
58
    def __str__(self):
59 60 61 62
        return "%s %s" % (self._meta.verbose_name, self.name)

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

Skia's avatar
Skia committed
64
class Token(models.Model):
65
    name = models.CharField(_('name'), max_length=5)
Skia's avatar
Skia committed
66 67
    launderette = models.ForeignKey(Launderette, related_name='tokens', verbose_name=_('launderette'))
    type = models.CharField(_('type'), max_length=10, choices=[('WASHING', _('Washing')), ('DRYING', _('Drying'))])
68 69
    borrow_date = models.DateTimeField(_('borrow date'), null=True)
    user = models.ForeignKey(Subscriber, related_name='tokens', verbose_name=_('user'))
Skia's avatar
Skia committed
70 71 72

    class Meta:
        verbose_name = _('Token')
73
        unique_together = ('name', 'launderette', 'type')
Skia's avatar
Skia committed
74 75 76 77 78 79 80 81 82 83 84 85 86

    def is_owned_by(self, user):
        """
        Method to see if that object can be edited by the given user
        """
        if user.is_in_group(settings.SITH_GROUPS['launderette-admin']['name']):
            return True
        return False

class Slot(models.Model):
    start_date = models.DateTimeField(_('start date'))
    type = models.CharField(_('type'), max_length=10, choices=[('WASHING', _('Washing')), ('DRYING', _('Drying'))])
    machine = models.ForeignKey(Machine, related_name='slots', verbose_name=_('machine'))
Skia's avatar
Skia committed
87
    token = models.ForeignKey(Token, related_name='slots', verbose_name=_('token'), blank=True, null=True)
Skia's avatar
Skia committed
88 89
    user = models.ForeignKey(Subscriber, related_name='slots', verbose_name=_('user'))

Skia's avatar
Skia committed
90 91
    class Meta:
        verbose_name = _('Slot')
92
        ordering = ['start_date']
Skia's avatar
Skia committed
93

Skia's avatar
Skia committed
94 95 96
    def full_clean(self):
        return super(Slot, self).full_clean()

Skia's avatar
Skia committed
97
    def __str__(self):
98 99
        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
100 101