예제 #1
0
def edit_event(request, event_id):
    event = Event.objects.get(id=event_id)

    if not request.user.profile.can_edit_event(event):
        messages.add_message(
            request,
            messages.WARNING,
            message=_('You can not make changes to this event.'))
        return redirect(event.get_absolute_url())

    if request.method == 'GET':
        form = TeamEventForm(instance=event)

        context = {
            'team': event.team,
            'event': event,
            'event_form': form,
        }
        return render(request, 'get_together/events/edit_event.html', context)
    elif request.method == 'POST':
        form = TeamEventForm(request.POST, instance=event)
        if form.is_valid:
            new_event = form.save()
            return redirect(new_event.get_absolute_url())
        else:
            context = {
                'team': event.team,
                'event': event,
                'event_form': form,
            }
            return render(request, 'get_together/events/edit_event.html',
                          context)
    else:
        return redirect('home')
예제 #2
0
def edit_event(request, event_id):
    event = get_object_or_404(Event, id=event_id)

    if not request.user.profile.can_edit_event(event):
        messages.add_message(
            request,
            messages.WARNING,
            message=_("You can not make changes to this event."),
        )
        return redirect(event.get_absolute_url())

    if request.method == "GET":
        form = TeamEventForm(instance=event)
        if event.series is not None:
            form.initial["recurrences"] = event.series.recurrences

        context = {"team": event.team, "event": event, "event_form": form}
        return render(request, "get_together/events/edit_event.html", context)
    elif request.method == "POST":
        form = TeamEventForm(request.POST, instance=event)
        if form.is_valid:
            new_event = form.save()

            if form.cleaned_data.get("recurrences", None):
                if event.series is not None:
                    event.series.recurrences = form.cleaned_data["recurrences"]
                    event.series.save()
                else:
                    new_series = EventSeries.from_event(
                        new_event,
                        recurrences=form.cleaned_data["recurrences"])
                    new_series.save()
                    new_event.series = new_series
                    new_event.save()
            else:
                if event.series is not None:
                    old_series = event.series
                    event.series = None
                    event.save()
                    if old_series.instances.count() < 1:
                        old_series.delete()
                else:
                    event.save()

            return redirect(new_event.get_absolute_url())
        else:
            context = {"team": event.team, "event": event, "event_form": form}
            return render(request, "get_together/events/edit_event.html",
                          context)
    else:
        return redirect("home")
예제 #3
0
def edit_event(request, event_id):
    event = get_object_or_404(Event, id=event_id)

    if not request.user.profile.can_edit_event(event):
        messages.add_message(
            request,
            messages.WARNING,
            message=_('You can not make changes to this event.'))
        return redirect(event.get_absolute_url())

    if request.method == 'GET':
        form = TeamEventForm(instance=event)
        if event.series is not None:
            form.initial['recurrences'] = event.series.recurrences

        context = {
            'team': event.team,
            'event': event,
            'event_form': form,
        }
        return render(request, 'get_together/events/edit_event.html', context)
    elif request.method == 'POST':
        form = TeamEventForm(request.POST, instance=event)
        if form.is_valid:
            new_event = form.save()

            if form.cleaned_data.get('recurrences', None):
                if event.series is not None:
                    event.series.recurrences = form.cleaned_data['recurrences']
                    event.series.save()
                else:
                    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(new_event.get_absolute_url())
        else:
            context = {
                'team': event.team,
                'event': event,
                'event_form': form,
            }
            return render(request, 'get_together/events/edit_event.html',
                          context)
    else:
        return redirect('home')