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

This commit is contained in:
Michael Hall 2018-04-15 11:31:12 -04:00
parent c63eaa6436
commit 8c43ab394e
5 changed files with 54 additions and 2 deletions

View file

@ -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

View file

@ -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)

View file

@ -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))))

View file

@ -1,7 +1,13 @@
{% extends "get_together/base.html" %}
{% load static %}
{% block extra_google_analytics %}
{% block meta %}
<script type="text/javascript" src="{% static 'recurrence/js/recurrence.js' %}"></script>
<script type="text/javascript" src="{% static 'recurrence/js/recurrence-widget.js' %}"></script>
<link href="{% static 'recurrence/css/recurrence.css' %}" rel="stylesheet">
{% endblock %}
{% block extra_google_analytics %}
gtag('config', '{{settings.GOOGLE_ANALYTICS_ID}}', {'page_path': '/team/+create-event/'});
{% endblock %}

View file

@ -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 = {