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