From 8c43ab394ed9ef4bbe0359798fa24002956bf867 Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Sun, 15 Apr 2018 11:31:12 -0400 Subject: [PATCH] Allow creating an EventSeries when created an Event by adding a RecurrenceField to the new Event form and generating an EventSeries if it's filled in. Add a management command to auto-create the next event in a Series if the previously created Event's original start_time has passed. Fixes #40 --- events/forms.py | 4 +++- .../commands/create_next_in_series.py | 20 +++++++++++++++++++ events/models/events.py | 18 +++++++++++++++++ .../get_together/events/create_event.html | 8 +++++++- get_together/views/events.py | 6 ++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 events/management/commands/create_next_in_series.py diff --git a/events/forms.py b/events/forms.py index 832adaf..b278144 100644 --- a/events/forms.py +++ b/events/forms.py @@ -8,6 +8,7 @@ from django.contrib.auth.models import User from .models.locale import Country, SPR, City from .models.profiles import Team, UserProfile from .models.events import Event, EventComment ,CommonEvent, EventSeries, Place, EventPhoto +import recurrence import pytz from datetime import time @@ -189,9 +190,10 @@ class TeamEventForm(forms.ModelForm): return cleaned_data class NewTeamEventForm(forms.ModelForm): + recurrences = recurrence.forms.RecurrenceField(label="Repeat", required=False) class Meta: model = Event - fields = ['name', 'start_time', 'end_time', 'summary'] + fields = ['name', 'start_time', 'end_time', 'recurrences', 'summary'] widgets = { 'start_time': DateTimeWidget, 'end_time': DateTimeWidget diff --git a/events/management/commands/create_next_in_series.py b/events/management/commands/create_next_in_series.py new file mode 100644 index 0000000..86164b5 --- /dev/null +++ b/events/management/commands/create_next_in_series.py @@ -0,0 +1,20 @@ +from django.core.management.base import BaseCommand, CommandError +from django.conf import settings +from django.contrib.sites.models import Site +from django.utils import timezone + +from events.models import Event, EventSeries + +import time +import datetime + +class Command(BaseCommand): + help = "Generates the next event for any series that needs one" + + def handle(self, *args, **options): + needs_update = EventSeries.objects.filter(last_time__lte=timezone.now()) + + for series in needs_update: + next_event = series.create_next_in_series() + if next_event is not None: + print("Created new event: %s" % next_event) diff --git a/events/models/events.py b/events/models/events.py index 9761d72..f856eef 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -310,8 +310,26 @@ class EventSeries(models.Model): tags = models.CharField(verbose_name=_("Keyword Tags"), blank=True, null=True, max_length=128) + @classmethod + def from_event(klass, event, recurrences): + new_series = EventSeries( + team=event.team, + parent=event.parent, + name=event.name, + start_time=event.local_start_time.time(), + end_time=event.local_end_time.time(), + last_time=event.start_time, + summary=event.summary, + place=event.place, + created_by=event.created_by, + recurrences=recurrences, + ) + return new_series + def create_next_in_series(self): next_date = self.recurrences.after(self.last_time, dtstart=self.last_time) + if next_date is None: + return None event_tz = pytz.timezone(self.tz) next_start = pytz.utc.localize(timezone.make_naive(event_tz.localize(datetime.datetime.combine(next_date.date(), self.start_time)))) diff --git a/get_together/templates/get_together/events/create_event.html b/get_together/templates/get_together/events/create_event.html index 801a5f7..ac7043d 100644 --- a/get_together/templates/get_together/events/create_event.html +++ b/get_together/templates/get_together/events/create_event.html @@ -1,7 +1,13 @@ {% extends "get_together/base.html" %} {% load static %} -{% block extra_google_analytics %} +{% block meta %} + + + +{% endblock %} + + {% block extra_google_analytics %} gtag('config', '{{settings.GOOGLE_ANALYTICS_ID}}', {'page_path': '/team/+create-event/'}); {% endblock %} diff --git a/get_together/views/events.py b/get_together/views/events.py index 20ee8fe..55e42cc 100644 --- a/get_together/views/events.py +++ b/get_together/views/events.py @@ -105,6 +105,12 @@ def create_event(request, team_id): if form.is_valid: new_event = form.save() Attendee.objects.create(event=new_event, user=request.user.profile, role=Attendee.HOST, status=Attendee.YES) + + if form.cleaned_data.get('recurrences', None): + new_series = EventSeries.from_event(new_event, recurrences=form.cleaned_data['recurrences']) + new_series.save() + new_event.series = new_series + new_event.save() return redirect('add-place', new_event.id) else: context = {