gremmie@1: """ gremmie@1: Views for the gcalendar application. gremmie@1: """ gremmie@1: gremmie@1: from django.contrib.auth.decorators import login_required gremmie@1: from django.core.urlresolvers import reverse gremmie@1: from django.http import HttpResponse gremmie@1: from django.http import HttpResponseBadRequest gremmie@1: from django.http import HttpResponseForbidden gremmie@1: from django.http import HttpResponseRedirect gremmie@1: from django.http import Http404 gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.shortcuts import get_object_or_404 gremmie@1: from django.template import RequestContext gremmie@1: gremmie@1: from gcalendar.forms import EventEntryForm gremmie@1: from gcalendar.models import Event gremmie@1: gremmie@1: gremmie@1: def index(request): bgneal@345: user = request.user bgneal@345: if user.is_authenticated(): bgneal@789: tz = user.profile.time_zone bgneal@345: else: bgneal@345: tz = 'US/Pacific' bgneal@345: gremmie@1: return render_to_response('gcalendar/index.html', { bgneal@345: 'tz': tz, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def add_event(request): gremmie@1: if request.method == 'POST': gremmie@1: form = EventEntryForm(request.POST) gremmie@1: if form.is_valid(): gremmie@1: event = form.save(commit=False) gremmie@1: event.user = request.user gremmie@1: event.repeat = 'none' gremmie@1: event.save() gremmie@1: return HttpResponseRedirect(reverse('gcalendar-add_thanks')) gremmie@1: else: gremmie@1: form = EventEntryForm() gremmie@1: gremmie@1: return render_to_response('gcalendar/event.html', { gremmie@1: 'title': 'Add Calendar Event', gremmie@1: 'form': form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def add_thanks(request): gremmie@1: return render_to_response('gcalendar/thanks_add.html', { gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def edit_events(request): gremmie@1: events = Event.objects.filter(user=request.user, status=Event.ON_CAL).order_by('start_date') gremmie@1: return render_to_response('gcalendar/edit.html', { gremmie@1: 'events': events, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def edit_event(request, event_id): gremmie@1: event = get_object_or_404(Event, pk=event_id) gremmie@1: if event.user != request.user: gremmie@1: raise Http404 gremmie@1: gremmie@1: if request.method == 'POST': gremmie@1: form = EventEntryForm(request.POST, instance=event) gremmie@1: if form.is_valid(): gremmie@1: event = form.save(commit=False) gremmie@1: event.user = request.user gremmie@1: event.repeat = 'none' gremmie@1: event.status = Event.EDIT_REQ gremmie@1: event.save() gremmie@1: return HttpResponseRedirect(reverse('gcalendar-edit_thanks')) gremmie@1: else: gremmie@1: form = EventEntryForm(instance=event) gremmie@1: gremmie@1: return render_to_response('gcalendar/event.html', { gremmie@1: 'title': 'Change Calendar Event', gremmie@1: 'form': form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def edit_thanks(request): gremmie@1: return render_to_response('gcalendar/thanks_edit.html', { gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: def delete_event(request): gremmie@1: """This view marks an event for deletion. It is called via AJAX.""" gremmie@1: if request.user.is_authenticated(): gremmie@1: id = request.POST.get('id', None) gremmie@1: if id is None or not id.isdigit(): gremmie@1: return HttpResponseBadRequest() gremmie@1: try: gremmie@1: event = Event.objects.get(pk=id) gremmie@1: except Event.DoesNotExist: gremmie@1: return HttpResponseBadRequest() gremmie@1: if request.user != event.user: gremmie@1: return HttpResponseForbidden() gremmie@1: gremmie@1: event.status = Event.DEL_REQ gremmie@1: event.save() gremmie@1: return HttpResponse(id) gremmie@1: gremmie@1: return HttpResponseForbidden() gremmie@1: gremmie@1: gremmie@1: # vim: ts=4 sw=4