models.py 2.98 KB
Newer Older
1
from django.contrib.auth.models import User
2
from django.core.exceptions import ValidationError
3
4
5
6
7
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

import events
8
from utils.translation import ModelTranslateMeta, MultilingualField
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


class PizzaEvent(models.Model):
    start = models.DateTimeField(_("Order from"))
    end = models.DateTimeField(_("Order until"))
    event = models.OneToOneField(events.models.Event, on_delete=models.CASCADE)

    @property
    def title(self):
        return self.event.title

    @property
    def in_the_future(self):
        return self.start > timezone.now()

24
25
26
27
    @property
    def has_ended(self):
        return self.end < timezone.now()

28
29
    @property
    def just_ended(self):
30
31
        return (self.has_ended and
                self.end + timezone.timedelta(hours=8) > timezone.now())
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

    @classmethod
    def current(cls):
        try:
            return PizzaEvent.objects.get(
                end__gt=timezone.now() - timezone.timedelta(hours=8)
            )
        except PizzaEvent.DoesNotExist:
            return None

    def clean(self):
        for other in PizzaEvent.objects.all():
            if self.start <= other.end and other.start >= self.end:
                raise ValidationError({
                    'start': _('This event cannot overlap with ') + str(other),
Jim Driessen's avatar
Jim Driessen committed
47
                    'end': _('This event cannot overlap with ') + str(other),
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
                })

    def __str__(self):
        return 'Pizzas for ' + str(self.event)


class Product(models.Model, metaclass=ModelTranslateMeta):
    name = models.CharField(max_length=50)
    description = MultilingualField(models.TextField)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    available = models.BooleanField(default=True)

    def __str__(self):
        return self.name

63
64
65
    class Meta:
        ordering = ('name', )

66
67
68

class Order(models.Model):
    member = models.ForeignKey(
69
        User,
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )

    paid = models.BooleanField(default=False)

    name = models.CharField(
        max_length=50,
        help_text=_('Use this for non-members'),
        null=True,
        blank=True,
    )

    product = models.ForeignKey(Product, on_delete=models.PROTECT)
    pizza_event = models.ForeignKey(PizzaEvent, on_delete=models.CASCADE)

    def clean(self):
        if ((self.member is None and not self.name) or
                (self.member and self.name)):
            raise ValidationError({
                'member': _('Either specify a member or a name'),
                'name': _('Either specify a member or a name'),
            })

    @property
    def member_name(self):
        if self.member is not None:
            return self.member.get_full_name()
        return self.name

101
102
103
104
    @property
    def can_be_changed(self):
        return not self.paid and not self.pizza_event.has_ended

105
    class Meta:
106
        unique_together = ('pizza_event', 'member',)