gremmie@1: """ gremmie@1: Views for the gcalendar application. gremmie@1: """ gremmie@1: gremmie@1: from django.contrib.auth.decorators import login_required bgneal@1094: from django.core.exceptions import PermissionDenied bgneal@1094: from django.core.paginator import InvalidPage 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 get_object_or_404 bgneal@1031: from django.shortcuts import render gremmie@1: bgneal@1094: from core.functions import get_page bgneal@1094: from core.paginator import DiggPaginator 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: bgneal@1031: return render(request, 'gcalendar/index.html', { bgneal@345: 'tz': tz, bgneal@1094: 'V3_DESIGN': True, bgneal@1031: }) 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' bgneal@1021: event.save(html=form.html) gremmie@1: return HttpResponseRedirect(reverse('gcalendar-add_thanks')) gremmie@1: else: gremmie@1: form = EventEntryForm() gremmie@1: bgneal@1031: return render(request, 'gcalendar/event.html', { gremmie@1: 'title': 'Add Calendar Event', gremmie@1: 'form': form, bgneal@1094: 'V3_DESIGN': True, bgneal@1031: }) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def add_thanks(request): bgneal@1031: return render(request, 'gcalendar/thanks_add.html', { bgneal@1031: }) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def edit_events(request): bgneal@817: events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\ bgneal@817: order_by('-start_date') bgneal@1094: paginator = DiggPaginator(events, 25, body=5, tail=2, margin=3, padding=2) bgneal@1094: num = get_page(request.GET) bgneal@817: try: bgneal@817: page = paginator.page(num) bgneal@1094: except InvalidPage: bgneal@1094: raise Http404 bgneal@817: bgneal@1031: return render(request, 'gcalendar/edit.html', { bgneal@817: 'page': page, bgneal@1094: 'V3_DESIGN': True, bgneal@1031: }) 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: bgneal@1094: raise PermissionDenied 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: bgneal@1031: return render(request, 'gcalendar/event.html', { gremmie@1: 'title': 'Change Calendar Event', gremmie@1: 'form': form, bgneal@1031: }) gremmie@1: gremmie@1: gremmie@1: @login_required gremmie@1: def edit_thanks(request): bgneal@1031: return render(request, 'gcalendar/thanks_edit.html', { bgneal@1031: }) 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(): bgneal@1094: event_id = request.POST.get('id') bgneal@1094: if event_id is None or not event_id.isdigit(): gremmie@1: return HttpResponseBadRequest() gremmie@1: try: bgneal@1094: event = Event.objects.get(pk=event_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() bgneal@1094: return HttpResponse(event_id, content_type="text/plain") gremmie@1: gremmie@1: return HttpResponseForbidden()