/
models.py
279 lines (254 loc) · 9.18 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from autoslug import AutoSlugField
from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from django_ckeditor_5.fields import CKEditor5Field
from jsonfield.fields import JSONField
from parler.models import TranslatableModel
from parler.models import TranslatedFields
from adhocracy4 import transforms
from adhocracy4.images import fields as images_fields
from adhocracy4.projects.models import Project
from apps.projects import query
class Organisation(TranslatableModel):
slug = AutoSlugField(populate_from="name", unique=True)
name = models.CharField(max_length=512)
initiators = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
title = models.CharField(
verbose_name=_("Title of your organisation"),
max_length=100,
help_text=_(
"The title of your organisation will be shown "
"on the landing page. max. 100 characters"
),
blank=True,
)
translations = TranslatedFields(
description=models.CharField(
max_length=800,
verbose_name=_("Short description of " "your organisation"),
help_text=_(
"The description will be "
"displayed on the landing "
"page. max. 800 characters"
),
blank=True,
),
slogan=models.CharField(
max_length=200,
verbose_name=_("Slogan"),
help_text=_(
"The slogan will be shown below "
"the title of your organisation "
"on the landing page. The slogan "
"can provide context or "
"additional information to the "
"title. max. 200 characters"
),
blank=True,
),
information=CKEditor5Field(
config_name="collapsible-image-editor",
verbose_name=_("Information about your organisation"),
help_text=_(
"You can provide general information about your "
"participation platform to your visitors. "
"It’s also helpful to name a general person "
"of contact for inquiries. The information "
'will be shown on a separate "About" page that '
"can be reached via the main menu."
),
blank=True,
),
)
logo = images_fields.ConfiguredImageField(
"logo",
verbose_name=_("Logo"),
help_text=_(
"The Logo representing your organisation."
" The image must be square and it "
"should be min. 200 pixels wide and 200 "
"pixels tall and max. 800 pixels wide and "
"800 pixels tall. Allowed file formats are "
"png, jpeg, gif. The file size "
"should be max. 5 MB."
),
upload_to="organisations/logos",
blank=True,
)
url = models.URLField(
blank=True,
verbose_name="Organisation website",
help_text=_(
"Please enter " "a full url which " "starts with https:// " "or http://"
),
)
image = images_fields.ConfiguredImageField(
"heroimage",
verbose_name=_("Header image"),
help_prefix=_("The image will be shown as a decorative background image."),
upload_to="organisations/backgrounds",
blank=True,
)
image_copyright = models.CharField(
max_length=200,
verbose_name=_("Header image copyright"),
blank=True,
help_text=_("Author, which is displayed in the header image."),
)
twitter_handle = models.CharField(
max_length=100,
blank=True,
verbose_name="Twitter handle",
)
facebook_handle = models.CharField(
max_length=100,
blank=True,
verbose_name="Facebook handle",
)
instagram_handle = models.CharField(
max_length=100,
blank=True,
verbose_name="Instagram handle",
)
imprint = CKEditor5Field(
verbose_name=_("Imprint"),
help_text=_(
"Please provide all the legally "
"required information of your imprint. "
"The imprint will be shown on a separate page."
),
blank=True,
)
terms_of_use = CKEditor5Field(
verbose_name=_("Terms of use"),
help_text=_(
"Please provide all the legally "
"required information of your terms of use. "
"The terms of use will be shown on a separate page."
),
blank=True,
)
data_protection = CKEditor5Field(
verbose_name=_("Data protection policy"),
help_text=_(
"Please provide all the legally "
"required information of your data protection. "
"The data protection policy will be shown on a "
"separate page."
),
blank=True,
)
netiquette = CKEditor5Field(
verbose_name=_("Netiquette"),
help_text=_(
"Please provide a netiquette for the participants. "
"The netiquette helps improving the climate of "
"online discussions and supports the moderation."
),
blank=True,
)
is_supporting = models.BooleanField(
default=False,
verbose_name=_("is a supporting organisation"),
help_text=_(
"For supporting organisations, the banner asking "
"for donations is not displayed on their pages."
),
)
language = models.CharField(
verbose_name=_("Default language for e-mails"),
choices=settings.LANGUAGES,
default=settings.DEFAULT_USER_LANGUAGE_CODE,
max_length=4,
help_text=_(
"All e-mails to unregistered users (invites) will be sent "
"in this language."
),
)
site = models.ForeignKey(Site, on_delete=models.SET_NULL, blank=True, null=True)
def __str__(self):
return self.name
@cached_property
def projects(self):
return Project.objects.filter(
organisation=self, is_archived=False, is_draft=False
)
def get_projects_list(self, user):
projects = query.filter_viewable(self.projects, user)
now = timezone.now()
min_module_start = models.Min(
"module__phase__start_date", filter=models.Q(module__is_draft=False)
)
max_module_end = models.Max(
"module__phase__end_date", filter=models.Q(module__is_draft=False)
)
sorted_active_projects = (
projects.annotate(project_start=min_module_start)
.annotate(project_end=max_module_end)
.filter(project_start__lte=now, project_end__gt=now)
.order_by("project_end")
)
sorted_future_projects = (
projects.annotate(project_start=min_module_start)
.filter(models.Q(project_start__gt=now) | models.Q(project_start=None))
.order_by("project_start")
)
sorted_past_projects = (
projects.annotate(project_start=min_module_start)
.annotate(project_end=max_module_end)
.filter(project_end__lt=now)
.order_by("project_start")
)
return sorted_active_projects, sorted_future_projects, sorted_past_projects
def has_initiator(self, user):
return self.initiators.filter(id=user.id).exists()
def has_org_member(self, user):
return Member.objects.filter(
member__id=user.id, organisation__id=self.id
).exists()
def get_absolute_url(self):
return reverse("organisation", kwargs={"organisation_slug": self.slug})
def has_social_share(self):
return self.twitter_handle or self.facebook_handle or self.instagram_handle
def save(self, update_fields=None, *args, **kwargs):
self.imprint = transforms.clean_html_field(self.imprint)
if update_fields:
update_fields = {"imprint"}.union(update_fields)
super().save(update_fields=update_fields, *args, **kwargs)
class Member(models.Model):
member = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
organisation = models.ForeignKey(
settings.A4_ORGANISATIONS_MODEL, on_delete=models.CASCADE
)
member_number = models.CharField(
max_length=50,
blank=True,
)
additional_info = JSONField(blank=True)
class Meta:
unique_together = [("member", "organisation")]
def __str__(self):
return "{}_{}".format(self.organisation, self.member)
class OrganisationTermsOfUse(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
editable=False,
)
organisation = models.ForeignKey(
settings.A4_ORGANISATIONS_MODEL,
on_delete=models.CASCADE,
editable=False,
)
has_agreed = models.BooleanField(
default=False,
)
class Meta:
unique_together = [("user", "organisation")]
def __str__(self):
return "{}_{}".format(self.organisation, self.user)