38 lines
1.6 KiB
Python
38 lines
1.6 KiB
Python
from datetime import timedelta
|
|
|
|
import humanize
|
|
from django.db import models
|
|
|
|
from davinci.caldav.models import CalDAVCalendar
|
|
from davinci.icalendar import ical_utils
|
|
|
|
|
|
class ICalSync(models.Model):
|
|
name = models.CharField(max_length=191, verbose_name="Sync Name")
|
|
ical_url = models.URLField(verbose_name="iCal URL")
|
|
target = models.ForeignKey(to=CalDAVCalendar, on_delete=models.CASCADE, verbose_name="Target calendar")
|
|
purge = models.BooleanField(verbose_name="Purge calendar (remove old events)", default=False,
|
|
help_text="Do not use if you are importing multiple ICS files into the same calendar.")
|
|
sync_interval = models.DurationField(verbose_name="Sync interval", default=timedelta(hours=1))
|
|
last_sync = models.DateTimeField(verbose_name="Last synchronised", blank=True, null=True)
|
|
active = models.BooleanField(verbose_name="Sync active", default=True)
|
|
|
|
class Meta:
|
|
verbose_name = "iCal Sync"
|
|
verbose_name_plural = "iCal Syncs"
|
|
|
|
def __str__(self):
|
|
return f'{self.name} to {self.target} every {humanize.precisedelta(self.sync_interval)}'
|
|
|
|
@property
|
|
def humanized_sync_interval(self):
|
|
return humanize.precisedelta(self.sync_interval)
|
|
|
|
def get_events(self):
|
|
data = ical_utils.get_ical_from_url(self.ical_url)
|
|
events = ical_utils.split_events(data)
|
|
# We have to pad the uids with some string related to the target calendar,
|
|
# So that if someone wants to sync the same .ics to two calendars, the UIDs are different.
|
|
fixed_events = ical_utils.fix_ical_uids(events, self.target)
|
|
return fixed_events
|