/
models.py
65 lines (53 loc) · 2.31 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
from datetime import timedelta
from autoslug import AutoSlugField
from django.db import models
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 adhocracy4 import transforms
from adhocracy4.images.validators import ImageAltTextValidator
from adhocracy4.models.base import UserGeneratedContentModel
from adhocracy4.projects import models as project_models
class OfflineEventsQuerySet(models.QuerySet):
def starts_within(self, hours=72):
"""All offlineevents starting within the given time."""
now = timezone.now()
return self.filter(date__gt=now, date__lt=(now + timedelta(hours=hours)))
class OfflineEvent(UserGeneratedContentModel):
slug = AutoSlugField(populate_from="name", unique=True)
name = models.CharField(max_length=120, verbose_name=_("Title"))
event_type = models.CharField(
max_length=30,
verbose_name=_("Event type"),
)
date = models.DateTimeField(verbose_name=_("Date"))
description = CKEditor5Field(
config_name="collapsible-image-editor",
verbose_name=_("Description"),
validators=[ImageAltTextValidator()],
)
project = models.ForeignKey(project_models.Project, on_delete=models.CASCADE)
objects = OfflineEventsQuerySet.as_manager()
class Meta:
ordering = ["-date"]
def __str__(self):
return self.name
def save(self, update_fields=None, *args, **kwargs):
self.description = transforms.clean_html_field(self.description, "image-editor")
if update_fields:
update_fields = {"description"}.union(update_fields)
super().save(update_fields=update_fields, *args, **kwargs)
@cached_property
def get_timeline_index(self):
if self.project.display_timeline:
for count, cluster in enumerate(self.project.participation_dates):
if "event_type" in cluster and self.slug == cluster["slug"]:
return count
return 0
def get_absolute_url(self):
if self.project.display_timeline:
return "{}?initialSlide={}".format(
self.project.get_absolute_url(), self.get_timeline_index
)
return self.project.get_absolute_url()