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

7
8
from datetime import timedelta

Skia's avatar
Skia committed
9
10
11
from club.models import Club
from accounting.models import Product
from core.models import Group
12
from subscription.models import Subscriber
Skia's avatar
Skia committed
13
14
15
16
17

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)
Skia's avatar
Skia committed
18
19
    type = models.CharField(_('subscription type'),
            max_length=255,
Skia's avatar
Skia committed
20
            choices=[('BAR',_('Bar')), ('OFFICE',_('Office'))])
Skia's avatar
Skia committed
21
22
    edit_groups = models.ManyToManyField(Group, related_name="editable_counters", blank=True)
    view_groups = models.ManyToManyField(Group, related_name="viewable_counters", blank=True)
Skia's avatar
Skia committed
23
    barmen_session = {}
Skia's avatar
Skia committed
24
25
26

    def __getattribute__(self, name):
        if name == "owner_group":
Skia's avatar
Skia committed
27
            return Group(name=self.club.unix_name+settings.SITH_BOARD_SUFFIX)
Skia's avatar
Skia committed
28
29
30
31
        return object.__getattribute__(self, name)

    def __str__(self):
        return self.name
Skia's avatar
Skia committed
32
33
34
35
36

    def get_absolute_url(self):
        return reverse('counter:details', kwargs={'counter_id': self.id})

    def can_be_viewed_by(self, user):
Skia's avatar
Skia committed
37
        return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)
38
39
40
41
42
43
44
45
46
47
48
49
50

    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