models.py 4.77 KB
Newer Older
1
from django.core.validators import MinValueValidator
2
3
from django.db import models
from django.utils import timezone
Tom van Bussel's avatar
Tom van Bussel committed
4
from django.utils.translation import ugettext_lazy as _
Joost Rijneveld's avatar
Joost Rijneveld committed
5

Tom van Bussel's avatar
Tom van Bussel committed
6
from utils.translation import ModelTranslateMeta, MultilingualField
Joost Rijneveld's avatar
Joost Rijneveld committed
7
8
9
from utils.validators import validate_file_extension


Tom van Bussel's avatar
Tom van Bussel committed
10
11
12
13
14
15
16
17
18
19
class Document(models.Model, metaclass=ModelTranslateMeta):
    class Meta:
        verbose_name = _('Document')
        verbose_name_plural = _('Documents')

    DOCUMENT_CATEGORIES = (
        ('annual', _('Annual document')),
        ('association', _('Association document')),
        ('minutes', _('Minutes')),
        ('misc', _('Miscellaneous document')),
Joost Rijneveld's avatar
Joost Rijneveld committed
20
    )
Tom van Bussel's avatar
Tom van Bussel committed
21
22
23
24
25

    name = MultilingualField(
        models.CharField,
        verbose_name=_('name'),
        max_length=200
Joost Rijneveld's avatar
Joost Rijneveld committed
26
    )
Tom van Bussel's avatar
Tom van Bussel committed
27
28
29
30

    created = models.DateTimeField(
        verbose_name=_('created'),
        auto_now_add=True,
Joost Rijneveld's avatar
Joost Rijneveld committed
31
32
    )

Tom van Bussel's avatar
Tom van Bussel committed
33
34
35
36
    last_updated = models.DateTimeField(
        verbose_name=_('last updated'),
        auto_now=True
    )
37

Tom van Bussel's avatar
Tom van Bussel committed
38
39
40
41
42
43
    category = models.CharField(
        max_length=40,
        choices=DOCUMENT_CATEGORIES,
        verbose_name=_('category'),
        default='misc',
    )
Joost Rijneveld's avatar
Joost Rijneveld committed
44

Tom van Bussel's avatar
Tom van Bussel committed
45
46
47
48
    file = MultilingualField(
        models.FileField,
        verbose_name=_('file'),
        upload_to='documents/',
Joost Rijneveld's avatar
Joost Rijneveld committed
49
50
        validators=[validate_file_extension],
    )
Tom van Bussel's avatar
Tom van Bussel committed
51
52
53
54
55

    members_only = models.BooleanField(
        verbose_name=_('members only'),
        default=False
    )
Joost Rijneveld's avatar
Joost Rijneveld committed
56
57

    def __str__(self):
Tom van Bussel's avatar
Tom van Bussel committed
58
        return '%s (%s)' % (self.name, str(self.created.date()))
Joost Rijneveld's avatar
Joost Rijneveld committed
59

Thom Wiggers's avatar
Thom Wiggers committed
60

Tom van Bussel's avatar
Tom van Bussel committed
61
62
63
64
65
66
67
68
69
70
71
class AnnualDocument(Document):
    class Meta:
        verbose_name = _('Annual document')
        verbose_name_plural = _('Annual documents')
        unique_together = ('subcategory', 'year')

    SUBCATEGORIES = (
        ('report', _('Annual report')),
        ('financial', _('Financial report')),
        ('policy', _('Policy document')),
    )
Joost Rijneveld's avatar
Joost Rijneveld committed
72

Tom van Bussel's avatar
Tom van Bussel committed
73
74
75
76
77
    subcategory = models.CharField(
        max_length=40,
        choices=SUBCATEGORIES,
        verbose_name=_('category'),
        default='report',
Joost Rijneveld's avatar
Joost Rijneveld committed
78
79
    )

Tom van Bussel's avatar
Tom van Bussel committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    year = models.IntegerField(
        verbose_name=_('year'),
        validators=[MinValueValidator(1990)],
    )

    def save(self, *args, **kwargs):
        self.category = 'annual'
        if self.subcategory == 'report':
            self.name_en = 'Annual report %d' % self.year
            self.name_nl = 'Jaarverslag %d' % self.year
        elif self.subcategory == 'financial':
            self.name_en = 'Financial report %d' % self.year
            self.name_nl = 'Financieel jaarverslag %d' % self.year
        else:
            self.name_en = 'Policy document %d' % self.year
            self.name_nl = 'Beleidsdocument %d' % self.year
        super().save(*args, **kwargs)
97

Tom van Bussel's avatar
Tom van Bussel committed
98
99
100
101
102
103
104

class AssociationDocumentManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(category='association')


class AssociationDocument(Document):
105
    class Meta:
Tom van Bussel's avatar
Tom van Bussel committed
106
107
108
        verbose_name = _('Miscellaneous association document')
        verbose_name_plural = _('Miscellaneous association documents')
        proxy = True
109

Tom van Bussel's avatar
Tom van Bussel committed
110
    objects = AssociationDocumentManager()
Joost Rijneveld's avatar
Joost Rijneveld committed
111

Tom van Bussel's avatar
Tom van Bussel committed
112
113
114
    def save(self, *args, **kwargs):
        self.category = 'association'
        super().save(*args, **kwargs)
115
116


Tom van Bussel's avatar
Tom van Bussel committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class MiscellaneousDocumentManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(category='misc')


class MiscellaneousDocument(Document):
    class Meta:
        verbose_name = _('Miscellaneous document')
        verbose_name_plural = _('Miscellaneous documents')
        proxy = True

    objects = MiscellaneousDocumentManager()

    def save(self, *args, **kwargs):
        self.category = 'misc'
        super().save(*args, **kwargs)


class GeneralMeeting(models.Model, metaclass=ModelTranslateMeta):
    class Meta:
        verbose_name = _('General meeting')
        verbose_name_plural = _('General meetings')
        ordering = ['datetime']

    documents = models.ManyToManyField(
        Document,
Tom van Bussel's avatar
Tom van Bussel committed
143
144
        verbose_name=_('documents'),
        blank=True,
Tom van Bussel's avatar
Tom van Bussel committed
145
146
147
148
149
150
151
152
153
154
    )

    datetime = models.DateTimeField(
        verbose_name=_('datetime'),
    )

    location = MultilingualField(
        models.CharField,
        verbose_name=_('location'),
        max_length=200
Joost Rijneveld's avatar
Joost Rijneveld committed
155
156
157
    )

    def __str__(self):
Tom van Bussel's avatar
Tom van Bussel committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        return timezone.localtime(self.datetime).strftime('%Y-%m-%d')


class Minutes(Document):
    class Meta:
        verbose_name = _('Minutes')
        verbose_name_plural = _('Minutes')

    meeting = models.OneToOneField(
        GeneralMeeting,
        blank=True,
        null=True,
        on_delete=models.CASCADE
    )

    def save(self, *args, **kwargs):
        self.category = 'minutes'
        self.name_en = 'Minutes %s' % str(self.meeting.datetime.date())
        self.name_nl = 'Notulen %s' % str(self.meeting.datetime.date())
        super().save(*args, **kwargs)