comparison gcalendar/views.py @ 1094:110bbc78a482

GCalendar V3 conversion in progress.
author Brian Neal <bgneal@gmail.com>
date Sun, 29 May 2016 23:09:23 -0500
parents e1c03da72818
children d9cd3180c12c
comparison
equal deleted inserted replaced
1093:1ac847818aea 1094:110bbc78a482
1 """ 1 """
2 Views for the gcalendar application. 2 Views for the gcalendar application.
3 """ 3 """
4 4
5 from django.contrib.auth.decorators import login_required 5 from django.contrib.auth.decorators import login_required
6 from django.core.exceptions import PermissionDenied
7 from django.core.paginator import InvalidPage
6 from django.core.urlresolvers import reverse 8 from django.core.urlresolvers import reverse
7 from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
8 from django.http import HttpResponse 9 from django.http import HttpResponse
9 from django.http import HttpResponseBadRequest 10 from django.http import HttpResponseBadRequest
10 from django.http import HttpResponseForbidden 11 from django.http import HttpResponseForbidden
11 from django.http import HttpResponseRedirect 12 from django.http import HttpResponseRedirect
12 from django.http import Http404 13 from django.http import Http404
13 from django.shortcuts import get_object_or_404 14 from django.shortcuts import get_object_or_404
14 from django.shortcuts import render 15 from django.shortcuts import render
15 16
17 from core.functions import get_page
18 from core.paginator import DiggPaginator
16 from gcalendar.forms import EventEntryForm 19 from gcalendar.forms import EventEntryForm
17 from gcalendar.models import Event 20 from gcalendar.models import Event
18 21
19 22
20 def index(request): 23 def index(request):
24 else: 27 else:
25 tz = 'US/Pacific' 28 tz = 'US/Pacific'
26 29
27 return render(request, 'gcalendar/index.html', { 30 return render(request, 'gcalendar/index.html', {
28 'tz': tz, 31 'tz': tz,
32 'V3_DESIGN': True,
29 }) 33 })
30 34
31 35
32 @login_required 36 @login_required
33 def add_event(request): 37 def add_event(request):
43 form = EventEntryForm() 47 form = EventEntryForm()
44 48
45 return render(request, 'gcalendar/event.html', { 49 return render(request, 'gcalendar/event.html', {
46 'title': 'Add Calendar Event', 50 'title': 'Add Calendar Event',
47 'form': form, 51 'form': form,
52 'V3_DESIGN': True,
48 }) 53 })
49 54
50 55
51 @login_required 56 @login_required
52 def add_thanks(request): 57 def add_thanks(request):
56 61
57 @login_required 62 @login_required
58 def edit_events(request): 63 def edit_events(request):
59 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\ 64 events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\
60 order_by('-start_date') 65 order_by('-start_date')
61 paginator = Paginator(events, 25) 66 paginator = DiggPaginator(events, 25, body=5, tail=2, margin=3, padding=2)
62 num = request.GET.get('page') 67 num = get_page(request.GET)
63 try: 68 try:
64 page = paginator.page(num) 69 page = paginator.page(num)
65 except PageNotAnInteger: 70 except InvalidPage:
66 page = paginator.page(1) 71 raise Http404
67 except EmptyPage:
68 page = paginator.page(paginator.num_pages)
69 72
70 return render(request, 'gcalendar/edit.html', { 73 return render(request, 'gcalendar/edit.html', {
71 'page': page, 74 'page': page,
75 'V3_DESIGN': True,
72 }) 76 })
73 77
74 78
75 @login_required 79 @login_required
76 def edit_event(request, event_id): 80 def edit_event(request, event_id):
77 event = get_object_or_404(Event, pk=event_id) 81 event = get_object_or_404(Event, pk=event_id)
78 if event.user != request.user: 82 if event.user != request.user:
79 raise Http404 83 raise PermissionDenied
80 84
81 if request.method == 'POST': 85 if request.method == 'POST':
82 form = EventEntryForm(request.POST, instance=event) 86 form = EventEntryForm(request.POST, instance=event)
83 if form.is_valid(): 87 if form.is_valid():
84 event = form.save(commit=False) 88 event = form.save(commit=False)
103 107
104 108
105 def delete_event(request): 109 def delete_event(request):
106 """This view marks an event for deletion. It is called via AJAX.""" 110 """This view marks an event for deletion. It is called via AJAX."""
107 if request.user.is_authenticated(): 111 if request.user.is_authenticated():
108 id = request.POST.get('id', None) 112 event_id = request.POST.get('id')
109 if id is None or not id.isdigit(): 113 if event_id is None or not event_id.isdigit():
110 return HttpResponseBadRequest() 114 return HttpResponseBadRequest()
111 try: 115 try:
112 event = Event.objects.get(pk=id) 116 event = Event.objects.get(pk=event_id)
113 except Event.DoesNotExist: 117 except Event.DoesNotExist:
114 return HttpResponseBadRequest() 118 return HttpResponseBadRequest()
115 if request.user != event.user: 119 if request.user != event.user:
116 return HttpResponseForbidden() 120 return HttpResponseForbidden()
117 121
118 event.status = Event.DEL_REQ 122 event.status = Event.DEL_REQ
119 event.save() 123 event.save()
120 return HttpResponse(id) 124 return HttpResponse(event_id, content_type="text/plain")
121 125
122 return HttpResponseForbidden() 126 return HttpResponseForbidden()