Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Abstract for Calendar and Event model class #488

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions schedule/models/calendars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -143,6 +143,7 @@ class Calendar(models.Model):
objects = CalendarManager()

class Meta:
abstract = True
verbose_name = _("calendar")
verbose_name_plural = _("calendars")

Expand All @@ -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):
"""
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
39 changes: 30 additions & 9 deletions schedule/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"),)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -424,6 +428,10 @@ def effective_end(self):
return None


class Event(EventAbstract):
pass


class EventRelationManager(models.Manager):
"""
>>> import datetime
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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")]
Expand All @@ -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)
Expand All @@ -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"),)
Expand Down Expand Up @@ -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):
Expand All @@ -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