コード例 #1
0
ファイル: views.py プロジェクト: jwhite007/iuvo
def create_event_view(request, user_id):
    if int(user_id) != request.user.pk:
        raise Http404
    if request.method == 'POST':
        form = EventForm(request.user, data=request.POST)
        if form.is_valid():
            event = form.save(commit=False)
            event.owner = request.user
            event.title = form.cleaned_data.get('title')
            event.location = form.cleaned_data.get('location')
            event.message = form.cleaned_data.get('message')
            event.start_day = form.cleaned_data.get('start_day')
            event.start_time = form.cleaned_data.get('start_time')
            event.end_day = form.cleaned_data.get('end_day')
            event.end_time = form.cleaned_data.get('end_time')
            event.notify_day = form.cleaned_data.get('notify_day')
            event.notify_time = form.cleaned_data.get('notify_time')
            tz = form.cleaned_data.get('timezone')

            start_date = get_date(
                form.cleaned_data.get('start_day'),
                form.cleaned_data.get('start_time'),
                tz)
            end_date = get_date(
                form.cleaned_data.get('end_day'),
                form.cleaned_data.get('end_time'),
                tz)
            notify_date = get_date(
                form.cleaned_data.get('notify_day'),
                form.cleaned_data.get('notify_time'),
                tz)

            now = timezone.now()
            if (  # Checking for appropriate dates.
                    start_date < now or
                    end_date < start_date or
                    notify_date < end_date):
                # raise ValueError  # Change this to a better response
                messages.add_message(request, messages.WARNING, "Please make sure that your times are correct. End time must be later than Start time and Notify time must be later than End time")
                return redirect(create_event_view, request.user.pk)

            event.start_date = start_date
            event.end_date = end_date
            event.notify_date = notify_date

            event.status = 0

            event.save()
            event.contacts = form.cleaned_data.get('contacts')
            event.save()
            return redirect(view_event_view, request.user.pk, event.pk)
        else:
            messages.add_message(request, messages.WARNING, "Your form didn't validate. Please check it and try again.")
            return redirect(create_event_view, request.user.pk)  # Add message
    else:
        context = {'event_form': EventForm(current_user=request.user)}
        return render(request, 'iuvo_app/create_event.html', context)
コード例 #2
0
ファイル: views.py プロジェクト: jwhite007/iuvo
def edit_event_view(request, user_id, event_id):
    if int(user_id) != request.user.pk:
        raise Http404
    try:
        event = Event.objects.get(pk=event_id)
    except Event.DoesNotExist:
        raise Http404
    if request.method == 'POST':
        form = EventForm(request.POST, instance=event)
        if form.is_valid():
            # event.title = form.cleaned_data.get('title')
            # event.location = form.cleaned_data.get('location')
            # event.message = form.cleaned_data.get('message')
            # event.start_day = form.cleaned_data.get('start_day')
            # event.start_time = form.cleaned_data.get('start_time')
            # event.end_day = form.cleaned_data.get('end_day')
            # event.end_time = form.cleaned_data.get('end_time')
            # event.notify_day = form.cleaned_data.get('notify_day')
            # event.notify_time = form.cleaned_data.get('notify_time')
            event = form.save(commit=False)
            tz = form.cleaned_data.get('timezone')
            start_date = get_date(
                form.cleaned_data.get('start_day'),
                form.cleaned_data.get('start_time'), tz)
            end_date = get_date(
                form.cleaned_data.get('end_day'),
                form.cleaned_data.get('end_time'), tz)
            notify_date = get_date(
                form.cleaned_data.get('notify_day'),
                form.cleaned_data.get('notify_time'), tz)


            now = timezone.now()
            if (  # Checking for appropriate dates.
                    end_date < now or
                    notify_date < end_date):
                # raise ValueError  # Change this to a better response
                messages.add_message(request, messages.WARNING, "Please make sure that your times are correct. End time must be later than Start time and Notify time must be later than End time")
                return redirect(create_event_view, request.user.pk)

            event.start_date = start_date
            event.end_date = end_date
            event.notify_date = notify_date

            event.save()
            form.save_m2m()
            # event.contacts = form.cleaned_data.get('contacts')
            # event.save()
            return redirect(events_list_view, request.user.pk)
        else:
            messages.add_message(request, messages.INFO, "Your form didn't validate. Please check it and try again.")
            return redirect(edit_event_view, request.user.pk, event.pk)
            # Add error message/handle this better.
    else:
        context = {'event_form': EventForm(instance=event), 'event': event}
        return render(request, 'iuvo_app/edit_event.html', context)