Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 26bdaff

Browse files
committed
#288 Potentially reduce tight coupling of events models
Make changes to events models that *might* help make it feasible to swap out the fundamental model types like `EventType` and `Occurrence` with custom versions in a project. This is just a first step and the feasibility of actually making swappable models needs to be tested, proved, and documented. - use string references for `ForeignKey` relationships so the actual implementation can be overridden with custom versions in a project (perhaps) - create abstract base classes for `EventType`, `EventRepeatsGenerator`, `Occurrence` with the core implementation, with the idea these models can be subclassed and customised in a project.
1 parent 8743fc3 commit 26bdaff

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

icekit_events/models.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ class RecurrenceRule(AbstractBaseModel):
123123
def __str__(self):
124124
return self.description
125125

126-
class EventType(PluralTitleSlugMixin):
126+
127+
class AbstractEventType(PluralTitleSlugMixin):
127128
is_public = models.BooleanField(
128129
"Show to public?",
129130
default=True,
@@ -142,12 +143,16 @@ def swatch(self, color_only=False):
142143
""").render(Context({'o': self, 'color_only': color_only}))
143144

144145
class Meta:
146+
abstract = True
145147
# changing the verbose name rather than renaming because model rename
146148
# migrations are sloooooow
147149
verbose_name = "Event category"
148150
verbose_name_plural = "Event categories"
149151

150152

153+
class EventType(AbstractEventType):
154+
pass
155+
151156

152157
@encoding.python_2_unicode_compatible
153158
class EventBase(PolymorphicModel, AbstractBaseModel, ICEkitContentsMixin,
@@ -168,7 +173,8 @@ class EventBase(PolymorphicModel, AbstractBaseModel, ICEkitContentsMixin,
168173
objects = EventManager()
169174

170175
primary_type = models.ForeignKey(
171-
EventType, blank=True, null=True,
176+
'icekit_events.EventType',
177+
blank=True, null=True,
172178
verbose_name="Primary category",
173179
help_text="The primary category of this event: Talk, workshop, etc. Only "
174180
"'public' event categories can be primary.",
@@ -614,7 +620,7 @@ class GeneratorException(Exception):
614620

615621

616622
@encoding.python_2_unicode_compatible
617-
class EventRepeatsGenerator(AbstractBaseModel):
623+
class AbstractEventRepeatsGenerator(AbstractBaseModel):
618624
"""
619625
A model storing the information and features required to generate a set
620626
of repeating datetimes for a given repeat rule.
@@ -633,7 +639,7 @@ class EventRepeatsGenerator(AbstractBaseModel):
633639
explicitly is most likely the easiest way to ensure this.
634640
"""
635641
event = models.ForeignKey(
636-
EventBase,
642+
'icekit_events.EventBase',
637643
db_index=True,
638644
editable=False,
639645
related_name='repeat_generators',
@@ -661,6 +667,7 @@ class EventRepeatsGenerator(AbstractBaseModel):
661667
)
662668

663669
class Meta:
670+
abstract = True
664671
ordering = ['pk'] # Order by PK, essentially in creation order
665672

666673
def __str__(self):
@@ -782,7 +789,7 @@ def save(self, *args, **kwargs):
782789
self.end = zero_datetime(self.end) \
783790
+ timedelta(days=1, microseconds=-1)
784791

785-
super(EventRepeatsGenerator, self).save(*args, **kwargs)
792+
super(AbstractEventRepeatsGenerator, self).save(*args, **kwargs)
786793

787794
@property
788795
def duration(self):
@@ -792,23 +799,27 @@ def duration(self):
792799
return self.end - self.start
793800

794801

802+
class EventRepeatsGenerator(AbstractEventRepeatsGenerator):
803+
pass
804+
805+
795806
@encoding.python_2_unicode_compatible
796-
class Occurrence(AbstractBaseModel):
807+
class AbstractOccurrence(AbstractBaseModel):
797808
"""
798809
A specific occurrence of an Event with start and end date times, and
799810
a reference back to the owner event that contains all the other data.
800811
"""
801812
objects = OccurrenceManager()
802813

803814
event = models.ForeignKey(
804-
EventBase,
815+
'icekit_events.EventBase',
805816
db_index=True,
806817
editable=False,
807818
related_name='occurrences',
808819
on_delete=models.CASCADE
809820
)
810821
generator = models.ForeignKey(
811-
EventRepeatsGenerator,
822+
'icekit_events.EventRepeatsGenerator',
812823
blank=True, null=True,
813824
on_delete=models.SET_NULL
814825
)
@@ -850,6 +861,7 @@ class Occurrence(AbstractBaseModel):
850861
blank=True, null=True, editable=False)
851862

852863
class Meta:
864+
abstract = True
853865
ordering = ['start', '-is_all_day', 'event', 'pk']
854866

855867
def time_range_string(self):
@@ -911,7 +923,7 @@ def save(self, *args, **kwargs):
911923
self.original_start = self.start
912924
if not self.original_end:
913925
self.original_end = self.end
914-
super(Occurrence, self).save(*args, **kwargs)
926+
super(AbstractOccurrence, self).save(*args, **kwargs)
915927

916928
# TODO Return __str__ as title for now, improve it later
917929
def title(self):
@@ -927,6 +939,10 @@ def get_absolute_url(self):
927939
return self.event.get_occurrence_url(self)
928940

929941

942+
class Occurrence(AbstractOccurrence):
943+
pass
944+
945+
930946
def get_occurrence_times_for_event(event):
931947
"""
932948
Return a tuple with two sets containing the (start, end) *naive* datetimes

0 commit comments

Comments
 (0)