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

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


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

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

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

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

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

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

Tom van Bussel's avatar
Tom van Bussel committed
47
48
49
50
    file = MultilingualField(
        models.FileField,
        verbose_name=_('file'),
        upload_to='documents/',
51
52
        validators=[FileExtensionValidator(
            ['.txt', '.pdf', '.jpg', '.jpeg', '.png'])],
Joost Rijneveld's avatar
Joost Rijneveld committed
53
    )
Tom van Bussel's avatar
Tom van Bussel committed
54
55
56
57
58

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

60
61
62
    def get_absolute_url(self):
        return reverse('documents:document', kwargs={'pk': self.pk})

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

Thom Wiggers's avatar
Thom Wiggers committed
66

Tom van Bussel's avatar
Tom van Bussel committed
67
class AnnualDocument(Document):
68
    """Describes an annual document"""
Tom van Bussel's avatar
Tom van Bussel committed
69
70
71
72
73
74
75
76
77
78
    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
79

Tom van Bussel's avatar
Tom van Bussel committed
80
81
82
83
84
    subcategory = models.CharField(
        max_length=40,
        choices=SUBCATEGORIES,
        verbose_name=_('category'),
        default='report',
Joost Rijneveld's avatar
Joost Rijneveld committed
85
86
    )

Tom van Bussel's avatar
Tom van Bussel committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    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)
104

Tom van Bussel's avatar
Tom van Bussel committed
105
106

class AssociationDocumentManager(models.Manager):
107
    """Custom manager to filter for association documents"""
Tom van Bussel's avatar
Tom van Bussel committed
108
109
110
111
112
    def get_queryset(self):
        return super().get_queryset().filter(category='association')


class AssociationDocument(Document):
113
    """Describes an association document"""
114
    class Meta:
Tom van Bussel's avatar
Tom van Bussel committed
115
116
117
        verbose_name = _('Miscellaneous association document')
        verbose_name_plural = _('Miscellaneous association documents')
        proxy = True
118

Tom van Bussel's avatar
Tom van Bussel committed
119
    objects = AssociationDocumentManager()
Joost Rijneveld's avatar
Joost Rijneveld committed
120

Tom van Bussel's avatar
Tom van Bussel committed
121
122
123
    def save(self, *args, **kwargs):
        self.category = 'association'
        super().save(*args, **kwargs)
124
125


126
127
128
129
130
class EventDocument(Document):
    """Describes a document for events"""
    class Meta:
        verbose_name = _('event document')
        verbose_name_plural = _('event documents')
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
131
132
133
        permissions = (
            ("override_owner", "Can access event document as if owner"),
        )
134
135
136
137
138
139
140
141
142
143
144
145

    owner = models.ForeignKey(
        'activemembers.MemberGroup',
        verbose_name=_('owner'),
        on_delete=models.CASCADE,
    )

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


Tom van Bussel's avatar
Tom van Bussel committed
146
class MiscellaneousDocumentManager(models.Manager):
147
    """Custom manager to filter for misc documents"""
Tom van Bussel's avatar
Tom van Bussel committed
148
149
150
151
152
    def get_queryset(self):
        return super().get_queryset().filter(category='misc')


class MiscellaneousDocument(Document):
153
    """Describes a miscellaneous document"""
Tom van Bussel's avatar
Tom van Bussel committed
154
155
156
157
158
159
160
161
162
163
164
165
166
    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):
167
    """Describes a general meeting"""
Tom van Bussel's avatar
Tom van Bussel committed
168
169
170
171
172
173
174
    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
175
176
        verbose_name=_('documents'),
        blank=True,
Tom van Bussel's avatar
Tom van Bussel committed
177
178
179
180
181
182
183
184
185
186
    )

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

    location = MultilingualField(
        models.CharField,
        verbose_name=_('location'),
        max_length=200
Joost Rijneveld's avatar
Joost Rijneveld committed
187
188
189
    )

    def __str__(self):
Tom van Bussel's avatar
Tom van Bussel committed
190
191
192
193
        return timezone.localtime(self.datetime).strftime('%Y-%m-%d')


class Minutes(Document):
194
    """Describes a minutes document"""
Tom van Bussel's avatar
Tom van Bussel committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    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)