From 566bd8b82f716c9c2a5216a308891869e6de7fbb Mon Sep 17 00:00:00 2001 From: Eldo Greshard Date: Wed, 9 Sep 2020 13:43:32 +0700 Subject: [PATCH 1/3] Create abstract class model for calendars and events --- schedule/models/calendars.py | 17 +++++++++++++--- schedule/models/events.py | 39 +++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/schedule/models/calendars.py b/schedule/models/calendars.py index 81f8a7de..543dd9ce 100644 --- a/schedule/models/calendars.py +++ b/schedule/models/calendars.py @@ -103,7 +103,7 @@ def get_calendars_for_object(self, obj, distinction=""): ) -class Calendar(models.Model): +class CalendarAbstract(models.Model): """ This is for grouping events so that batch relations can be made to all events. An example would be a project calendar. @@ -143,6 +143,7 @@ class Calendar(models.Model): objects = CalendarManager() class Meta: + abstract = True verbose_name = _("calendar") verbose_name_plural = _("calendars") @@ -160,7 +161,8 @@ def create_relation(self, obj, distinction="", inheritable=True): if Inheritable is set to true this relation will cascade to all events related to this calendar. """ - CalendarRelation.objects.create_relation(self, obj, distinction, inheritable) + CalendarRelation.objects.create_relation( + self, obj, distinction, inheritable) def get_recent(self, amount=5): """ @@ -181,6 +183,10 @@ def get_absolute_url(self): return reverse("calendar_home", kwargs={"calendar_slug": self.slug}) +class Calendar(CalendarAbstract): + pass + + class CalendarRelationManager(models.Manager): def create_relation( self, calendar, content_object, distinction="", inheritable=True @@ -194,7 +200,7 @@ def create_relation( ) -class CalendarRelation(models.Model): +class CalendarRelationAbstract(models.Model): """ This is for relating data to a Calendar, and possible all of the events for that calendar, there is also a distinction, so that the same type or kind of @@ -229,9 +235,14 @@ class CalendarRelation(models.Model): objects = CalendarRelationManager() class Meta: + abstract = True verbose_name = _("calendar relation") verbose_name_plural = _("calendar relations") index_together = [("content_type", "object_id")] def __str__(self): return "{} - {}".format(self.calendar, self.content_object) + + +class CalendarRelation(CalendarRelationAbstract): + pass diff --git a/schedule/models/events.py b/schedule/models/events.py index f1a8c04a..0a7af88b 100644 --- a/schedule/models/events.py +++ b/schedule/models/events.py @@ -43,7 +43,7 @@ def get_for_object(self, content_object, distinction="", inherit=True): ) -class Event(models.Model): +class EventAbstract(models.Model): """ This model stores meta data for a date. You can relate this data to many other models. @@ -89,6 +89,7 @@ class Event(models.Model): objects = EventManager() class Meta: + abstract = True verbose_name = _("event") verbose_name_plural = _("events") index_together = (("start", "end"),) @@ -175,7 +176,8 @@ def get_occurrences(self, start, end, clear_prefetch=True): final_occurrences.append(occ) # then add persisted occurrences which originated outside of this period but now # fall within it - final_occurrences += occ_replacer.get_additional_occurrences(start, end) + final_occurrences += occ_replacer.get_additional_occurrences( + start, end) return final_occurrences def get_rrule_object(self, tzinfo): @@ -226,7 +228,8 @@ def get_occurrence(self, date): return Occurrence.objects.get(event=self, original_start=date) except Occurrence.DoesNotExist: if use_naive: - next_occurrence = timezone.make_naive(next_occurrence, tzinfo) + next_occurrence = timezone.make_naive( + next_occurrence, tzinfo) return self._create_occurrence(next_occurrence) def _get_occurrence_list(self, start, end): @@ -374,7 +377,8 @@ def _event_params(self): ): sp = start_params[param] if sp == rule_params[param] or ( - hasattr(rule_params[param], "__iter__") and sp in rule_params[param] + hasattr(rule_params[param], + "__iter__") and sp in rule_params[param] ): event_params[param] = [sp] else: @@ -424,6 +428,10 @@ def effective_end(self): return None +class Event(EventAbstract): + pass + + class EventRelationManager(models.Manager): """ >>> import datetime @@ -542,7 +550,7 @@ def create_relation(self, event, content_object, distinction=""): ) -class EventRelation(models.Model): +class EventRelationAbstract(models.Model): """ This is for relating data to an Event, there is also a distinction, so that data can be related in different ways. A good example would be, if you have @@ -561,7 +569,8 @@ class EventRelation(models.Model): may not scale well. If you use this keep that in mind. """ - event = models.ForeignKey(Event, on_delete=models.CASCADE, verbose_name=_("event")) + event = models.ForeignKey( + Event, on_delete=models.CASCADE, verbose_name=_("event")) content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.IntegerField(db_index=True) content_object = fields.GenericForeignKey("content_type", "object_id") @@ -570,6 +579,7 @@ class EventRelation(models.Model): objects = EventRelationManager() class Meta: + abstract = True verbose_name = _("event relation") verbose_name_plural = _("event relations") index_together = [("content_type", "object_id")] @@ -580,8 +590,13 @@ def __str__(self): ) -class Occurrence(models.Model): - event = models.ForeignKey(Event, on_delete=models.CASCADE, verbose_name=_("event")) +class EventRelation(EventRelationAbstract): + pass + + +class OccurrenceAbstract(models.Model): + event = models.ForeignKey( + Event, on_delete=models.CASCADE, verbose_name=_("event")) title = models.CharField(_("title"), max_length=255, blank=True) description = models.TextField(_("description"), blank=True) start = models.DateTimeField(_("start"), db_index=True) @@ -593,6 +608,7 @@ class Occurrence(models.Model): updated_on = models.DateTimeField(_("updated on"), auto_now=True) class Meta: + abstract = True verbose_name = _("occurrence") verbose_name_plural = _("occurrences") index_together = (("start", "end"),) @@ -702,7 +718,8 @@ def __lt__(self, other): def __hash__(self): if not self.pk: - raise TypeError("Model instances without primary key value are unhashable") + raise TypeError( + "Model instances without primary key value are unhashable") return hash(self.pk) def __eq__(self, other): @@ -711,3 +728,7 @@ def __eq__(self, other): and self.original_start == other.original_start and self.original_end == other.original_end ) + + +class Occurrence(OccurrenceAbstract): + pass From d82089bde057f5dde66ee70378d4327b14d7629c Mon Sep 17 00:00:00 2001 From: Eldo Greshard Date: Thu, 10 Sep 2020 20:03:27 +0700 Subject: [PATCH 2/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a7f95f21..602ef27e 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Information * [Documentation](https://django-scheduler.readthedocs.io/) * [Wiki](https://github.com/llazzaro/django-scheduler/wiki) * [Sample Project](https://github.com/llazzaro/django-scheduler-sample) +* [Sample Project using django + vue] (https://github.com/karmael/django-scheduler-vue-sample) Installation From 0b54897e914f66c2d07df78911f05e729128418e Mon Sep 17 00:00:00 2001 From: Eldo Greshard Date: Thu, 10 Sep 2020 20:04:22 +0700 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 602ef27e..f538cca1 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Information * [Documentation](https://django-scheduler.readthedocs.io/) * [Wiki](https://github.com/llazzaro/django-scheduler/wiki) * [Sample Project](https://github.com/llazzaro/django-scheduler-sample) -* [Sample Project using django + vue] (https://github.com/karmael/django-scheduler-vue-sample) +* [Sample Project using django + vue](https://github.com/karmael/django-scheduler-vue-sample) Installation