A list view of upcoming events.

def list_events_view(request):
    ''' A list view of upcoming events. '''
    page_name = "Upcoming Events"
    profile = UserProfile.objects.get(user=request.user)
    event_form = EventForm(
        request.POST if 'post_event' in request.POST else None,
        profile=profile,
    )
    if event_form.is_valid():
        event_form.save()
        return HttpResponseRedirect(reverse('events:list'))

    # a pseudo-dictionary, actually a list with items of form (event, ongoing,
    # rsvpd, rsvp_form), where ongoing is a boolean of whether the event is
    # currently ongoing, rsvpd is a boolean of whether the user has rsvp'd to
    # the event
    events_dict = list()
    for event in Event.objects.filter(end_time__gte=now()):
        rsvp_form = RsvpForm(
            request.POST if "rsvp-{0}".format(event.pk) in request.POST else None,
            instance=event,
            profile=profile,
        )
        if rsvp_form.is_valid():
            rsvpd = rsvp_form.save()
            if rsvpd:
                message = MESSAGES['RSVP_ADD'].format(event=event.title)
            else:
                message = MESSAGES['RSVP_REMOVE'].format(event=event.title)
            messages.add_message(request, messages.SUCCESS, message)
            return HttpResponseRedirect(reverse('events:list'))

        ongoing = ((event.start_time <= now()) and (event.end_time >= now()))
        rsvpd = (profile in event.rsvps.all())
        events_dict.append((event, ongoing, rsvpd, rsvp_form))

    if request.method == "POST":
        messages.add_message(request, messages.ERROR, MESSAGES["EVENT_ERROR"])

    return render_to_response('list_events.html', {
        'page_name': page_name,
        'events_dict': events_dict,
        'now': now(),
        'event_form': event_form,
    }, context_instance=RequestContext(request))