comparison gpp/shoutbox/views.py @ 1:dbd703f7d63a

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