Sith/counter/models.py

51 lines
2.0 KiB
Python
Raw Normal View History

2016-03-28 12:54:35 +00:00
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
2016-03-28 12:54:35 +00:00
from django.conf import settings
2016-03-29 08:30:24 +00:00
from django.core.urlresolvers import reverse
2016-03-28 12:54:35 +00:00
from datetime import timedelta
2016-03-28 12:54:35 +00:00
from club.models import Club
from accounting.models import Product
from core.models import Group
from subscription.models import Subscriber
2016-03-28 12:54:35 +00:00
class Counter(models.Model):
name = models.CharField(_('name'), max_length=30)
club = models.ForeignKey(Club, related_name="counters")
products = models.ManyToManyField(Product, related_name="counters", blank=True)
2016-03-29 08:30:24 +00:00
type = models.CharField(_('subscription type'),
max_length=255,
2016-03-31 08:36:00 +00:00
choices=[('BAR',_('Bar')), ('OFFICE',_('Office'))])
2016-03-28 12:54:35 +00:00
edit_groups = models.ManyToManyField(Group, related_name="editable_counters", blank=True)
view_groups = models.ManyToManyField(Group, related_name="viewable_counters", blank=True)
2016-04-12 08:00:47 +00:00
barmen_session = {}
2016-03-28 12:54:35 +00:00
def __getattribute__(self, name):
if name == "owner_group":
2016-03-31 08:36:00 +00:00
return Group(name=self.club.unix_name+settings.SITH_BOARD_SUFFIX)
2016-03-28 12:54:35 +00:00
return object.__getattribute__(self, name)
def __str__(self):
return self.name
2016-03-29 08:30:24 +00:00
def get_absolute_url(self):
return reverse('counter:details', kwargs={'counter_id': self.id})
def can_be_viewed_by(self, user):
2016-03-31 08:36:00 +00:00
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)
def get_barmen_list(counter_id):
bl = []
counter_id = str(counter_id)
if counter_id in list(Counter.barmen_session.keys()):
if (timezone.now() - Counter.barmen_session[counter_id]['time']) < timedelta(minutes=settings.SITH_BARMAN_TIMEOUT):
for b in Counter.barmen_session[counter_id]['users']:
bl.append(Subscriber.objects.filter(id=b).first())
Counter.barmen_session[counter_id]['time'] = timezone.now()
else:
Counter.barmen_session[counter_id]['users'] = set()
return bl