Sith/launderette/models.py

109 lines
4.0 KiB
Python
Raw Normal View History

2016-08-06 13:20:38 +00:00
from django.db import models, DataError
2016-07-28 18:05:56 +00:00
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.core.urlresolvers import reverse
from core.models import User
from counter.models import Counter, Product
2016-07-28 18:05:56 +00:00
from subscription.models import Subscriber
from subscription.views import get_subscriber
2016-07-28 18:05:56 +00:00
# Create your models here.
class Launderette(models.Model):
name = models.CharField(_('name'), max_length=30)
counter = models.OneToOneField(Counter, verbose_name=_('counter'), related_name='launderette')
2016-07-28 18:05:56 +00:00
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
def can_be_edited_by(self, user):
sub = get_subscriber(user)
return sub in self.counter.sellers.all()
2016-07-28 18:05:56 +00:00
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'))
2016-08-06 13:20:38 +00:00
type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)
2016-07-28 18:05:56 +00:00
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
2016-07-28 23:38:46 +00:00
def __str__(self):
return "%s %s" % (self._meta.verbose_name, self.name)
def get_absolute_url(self):
2016-08-06 13:20:38 +00:00
return reverse('launderette:launderette_admin', kwargs={"launderette_id": self.launderette.id})
2016-07-28 23:38:46 +00:00
2016-07-28 18:05:56 +00:00
class Token(models.Model):
name = models.CharField(_('name'), max_length=5)
2016-07-28 18:05:56 +00:00
launderette = models.ForeignKey(Launderette, related_name='tokens', verbose_name=_('launderette'))
2016-08-06 13:20:38 +00:00
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(Subscriber, related_name='tokens', verbose_name=_('user'), null=True, blank=True)
2016-07-28 18:05:56 +00:00
class Meta:
verbose_name = _('Token')
unique_together = ('name', 'launderette', 'type')
2016-08-06 13:20:38 +00:00
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)
2016-07-28 18:05:56 +00:00
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
2016-08-01 22:32:55 +00:00
def __str__(self):
return self.__class__._meta.verbose_name + " " + self.get_type_display() + " #" + self.name + " (" + self.launderette.name + ")"
2016-07-28 18:05:56 +00:00
class Slot(models.Model):
start_date = models.DateTimeField(_('start date'))
2016-08-06 13:20:38 +00:00
type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)
2016-07-28 18:05:56 +00:00
machine = models.ForeignKey(Machine, related_name='slots', verbose_name=_('machine'))
2016-07-28 23:38:46 +00:00
token = models.ForeignKey(Token, related_name='slots', verbose_name=_('token'), blank=True, null=True)
2016-07-28 18:05:56 +00:00
user = models.ForeignKey(Subscriber, related_name='slots', verbose_name=_('user'))
2016-07-28 23:38:46 +00:00
class Meta:
verbose_name = _('Slot')
ordering = ['start_date']
2016-07-28 23:38:46 +00:00
def __str__(self):
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)
2016-07-28 23:38:46 +00:00