Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
molgen
/
mpicms
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
20
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security
Insights
Files
e523b92
config
docs
locale
mpicms
base
contrib
events
migrations
templatetags
__init__.py
apps.py
mixins.py
models.py
translation.py
views.py
news
personal
publications
static
templates
users
__init__.py
conftest.py
requirements
.coveragerc
.gitignore
.travis.yml
README.md
manage.py
pytest.ini
setup.cfg
Breadcrumbs
mpicms
/
mpicms
/
events
/
models.py
Blame
Blame
Latest commit
History
History
147 lines (119 loc) · 4.65 KB
Breadcrumbs
mpicms
/
mpicms
/
events
/
models.py
Top
File metadata and controls
Code
Blame
147 lines (119 loc) · 4.65 KB
Raw
import json from datetime import datetime from ics import Calendar, Event as ICSEvent from django.db import models from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel from wagtail.images.edit_handlers import ImageChooserPanel from wagtail.core.models import Page from wagtail.api import APIField from mpicms.base.mixins import BasePage, BodyMixin class Event(BodyMixin, BasePage): start_date = models.DateField(_('start date')) end_date = models.DateField(_('end date'), blank=True, null=True) start_time = models.TimeField(_('start time'), blank=True, null=True) end_time = models.TimeField(_('end time'), blank=True, null=True) room = models.CharField(_('Room'), max_length=10, blank=True) header_image = models.ForeignKey( 'wagtailimages.Image', null=True, blank=True, on_delete=models.SET_NULL, related_name='+' ) show_in_menus_default = False parent_page_types = ['events.EventIndex'] subpage_types = [] content_panels = Page.content_panels + BodyMixin.content_panels + [ ImageChooserPanel('header_image'), MultiFieldPanel( [ FieldPanel('start_date'), FieldPanel('start_time'), FieldPanel('end_date'), FieldPanel('end_time'), ], heading=_('event dates') ), FieldPanel('room') ] promote_panels = Page.promote_panels + BodyMixin.promote_panels api_fields = BodyMixin.api_fields + [ APIField('start_date'), APIField('end_date'), APIField('start_time'), APIField('end_time'), APIField('room') ] @property def start(self): if self.start_time: return datetime.combine(self.start_date, self.start_time) return self.start_date @property def end(self): if self.end_time: date = self.end_date or self.start_date return datetime.combine(date, self.end_time) return self.end_date def get_dict(self, request=None): return { 'title': self.title, 'start': self.start.isoformat(), 'end': self.end.isoformat() if self.end else None, 'url': self.get_url(request=request), 'color': '#006c66' } def clean(self): """Clean the model fields, if end_date is before start_date raise a ValidationError.""" super().clean() if self.end_date and self.end_date < self.start_date: raise ValidationError({'end_date': 'The end date cannot be before the start date.'}) if self.end_time and not self.start_time: raise ValidationError({'end_time': 'The end time cannot be set without a start time.'}) if self.end and self.end < self.start: raise ValidationError({'end_time': 'The end time cannot be before the start time.'}) def get_context(self, request, *args, **kwargs): context = super().get_context(request, *args, **kwargs) events = [] for child in self.get_parent().get_children().type(Event).live().specific(): events.append(child.get_dict(request)) context["events"] = json.dumps(events) return context class Meta(object): # noqa ordering = ['start_date'] verbose_name = _('event') verbose_name_plural = _('events') class EventIndex(BasePage): parent_page_types = ['base.RootPage'] subpage_types = ['events.Event'] @property def events(self): if self.depth <= 3: return Event.objects.live().specific() return self.get_children().type(Event).live().specific() def get_json_events(self, request=None): event_dicts = [event.get_dict(request) for event in self.events] return json.dumps(event_dicts) def clean(self): # Prevent more than one event index model = self.__class__ if (model.objects.count() > 0 and self.pk != model.objects.get().id): raise ValidationError("Can only create 1 %s instance" % model.__name__) @property def ics(self): c = Calendar() for event in self.events: e = ICSEvent( name=event.title, begin=event.start, end=event.end, description=event.search_description, url=event.full_url, location=event.room ) c.events.add(e) return '\n'.join(c) class Meta: # noqa verbose_name = _('event index') verbose_name_plural = _('event indexes')
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
You can’t perform that action at this time.