gremmie@1
|
1 """
|
gremmie@1
|
2 Views for the Shoutbox application.
|
gremmie@1
|
3 """
|
gremmie@1
|
4
|
gremmie@1
|
5 import re
|
gremmie@1
|
6 from django.shortcuts import render_to_response
|
gremmie@1
|
7 from django.template import RequestContext
|
gremmie@1
|
8 from django.http import HttpResponse
|
gremmie@1
|
9 from django.http import HttpResponseBadRequest
|
gremmie@1
|
10 from django.http import HttpResponseForbidden
|
gremmie@1
|
11 from django.http import HttpResponseRedirect
|
gremmie@1
|
12 from django.contrib.auth.decorators import login_required
|
gremmie@1
|
13
|
gremmie@1
|
14 from core.paginator import DiggPaginator
|
gremmie@1
|
15 from shoutbox.forms import ShoutBoxForm
|
gremmie@1
|
16 from shoutbox.models import Shout
|
gremmie@1
|
17
|
gremmie@1
|
18 SHOUTS_PER_PAGE = 10
|
gremmie@1
|
19
|
gremmie@1
|
20 @login_required
|
gremmie@1
|
21 def shout(request):
|
gremmie@1
|
22 if request.method == 'POST':
|
gremmie@1
|
23 msg = request.POST.get('msg', '').strip()
|
gremmie@1
|
24 if msg != '':
|
gremmie@1
|
25 shout = Shout(user=request.user, shout=msg)
|
gremmie@1
|
26 shout.save()
|
gremmie@1
|
27
|
gremmie@1
|
28 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
|
gremmie@1
|
29
|
gremmie@1
|
30
|
gremmie@1
|
31 def view(request, page=1):
|
gremmie@1
|
32 """This view allows one to view the shoutbox history."""
|
gremmie@1
|
33 paginator = DiggPaginator(Shout.objects.all(), SHOUTS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
|
gremmie@1
|
34 try:
|
gremmie@1
|
35 the_page = paginator.page(int(page))
|
gremmie@1
|
36 except InvalidPage:
|
gremmie@1
|
37 raise Http404
|
gremmie@1
|
38
|
gremmie@1
|
39 return render_to_response('shoutbox/view.html', {
|
gremmie@1
|
40 'page': the_page,
|
gremmie@1
|
41 },
|
gremmie@1
|
42 context_instance = RequestContext(request))
|
gremmie@1
|
43
|
gremmie@1
|
44
|
gremmie@1
|
45 shout_id_re = re.compile(r'shout-(\d+)')
|
gremmie@1
|
46
|
gremmie@1
|
47 def text(request):
|
gremmie@1
|
48 """This view function retrieves the text of a shout; it is used in the in-place
|
gremmie@1
|
49 editing of shouts on the shoutbox history view."""
|
gremmie@1
|
50 if request.user.is_authenticated():
|
gremmie@1
|
51 m = shout_id_re.match(request.GET.get('id', ''))
|
gremmie@1
|
52 if m is None:
|
gremmie@1
|
53 return HttpResponseBadRequest()
|
gremmie@1
|
54 try:
|
gremmie@1
|
55 shout = Shout.objects.get(pk=m.group(1))
|
gremmie@1
|
56 except Shout.DoesNotExist:
|
gremmie@1
|
57 return HttpResponseBadRequest()
|
gremmie@1
|
58 return HttpResponse(shout.shout)
|
gremmie@1
|
59
|
gremmie@1
|
60 return HttpResponseForbidden()
|
gremmie@1
|
61
|
gremmie@1
|
62
|
gremmie@1
|
63 def edit(request):
|
gremmie@1
|
64 """This view accepts a shoutbox edit from the shoutbox history view."""
|
gremmie@1
|
65 if request.user.is_authenticated():
|
gremmie@1
|
66 m = shout_id_re.match(request.POST.get('id', ''))
|
gremmie@1
|
67 if m is None:
|
gremmie@1
|
68 return HttpResponseBadRequest()
|
gremmie@1
|
69 try:
|
gremmie@1
|
70 shout = Shout.objects.get(pk=m.group(1))
|
gremmie@1
|
71 except Shout.DoesNotExist:
|
gremmie@1
|
72 return HttpResponseBadRequest()
|
gremmie@1
|
73 if request.user != shout.user:
|
gremmie@1
|
74 return HttpResponseForbidden()
|
gremmie@1
|
75 new_shout = request.POST.get('value', '').strip()
|
gremmie@1
|
76 if new_shout == '':
|
gremmie@1
|
77 return HttpResponseBadRequest()
|
gremmie@1
|
78 shout.shout = new_shout
|
gremmie@1
|
79 shout.save()
|
gremmie@1
|
80 return render_to_response('shoutbox/render_shout.html', {
|
gremmie@1
|
81 'shout': shout,
|
gremmie@1
|
82 },
|
gremmie@1
|
83 context_instance = RequestContext(request))
|
gremmie@1
|
84
|
gremmie@1
|
85 return HttpResponseForbidden()
|
gremmie@1
|
86
|
gremmie@1
|
87
|
gremmie@1
|
88 def delete(request):
|
gremmie@1
|
89 """This view deletes a shout. It is called by AJAX from the shoutbox history view."""
|
gremmie@1
|
90 if request.user.is_authenticated():
|
gremmie@1
|
91 id = request.POST.get('id', None)
|
gremmie@1
|
92 if id is None or not id.isdigit():
|
gremmie@1
|
93 return HttpResponseBadRequest()
|
gremmie@1
|
94 try:
|
gremmie@1
|
95 shout = Shout.objects.get(pk=id)
|
gremmie@1
|
96 except Shout.DoesNotExist:
|
gremmie@1
|
97 return HttpResponseBadRequest()
|
gremmie@1
|
98 if request.user != shout.user:
|
gremmie@1
|
99 return HttpResponseForbidden()
|
gremmie@1
|
100 shout.delete()
|
gremmie@1
|
101 return HttpResponse(id)
|
gremmie@1
|
102
|
gremmie@1
|
103 return HttpResponseForbidden()
|