comparison shoutbox/views.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/shoutbox/views.py@7ddd60164245
children 0ca691cccf8d
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
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.core.paginator import InvalidPage
9 from django.http import HttpResponse
10 from django.http import HttpResponseBadRequest
11 from django.http import HttpResponseForbidden
12 from django.http import HttpResponseRedirect
13 from django.http import Http404
14 from django.contrib.auth.decorators import login_required
15 from django.views.decorators.http import require_POST
16
17 from core.paginator import DiggPaginator
18 from core.functions import email_admins
19 from core.functions import get_page
20 from shoutbox.forms import ShoutBoxForm
21 from shoutbox.models import Shout
22 from shoutbox.models import ShoutFlag
23
24 SHOUTS_PER_PAGE = 10
25
26 @login_required
27 @require_POST
28 def shout(request):
29 msg = request.POST.get('msg', '').strip()
30 if msg == '':
31 return HttpResponse('')
32
33 shout = Shout(user=request.user, shout=msg)
34 shout.save()
35 return render_to_response('shoutbox/shout.html', {
36 'shout': shout,
37 },
38 context_instance = RequestContext(request))
39
40
41 def view_shout(request, id):
42 """This view is for viewing an individual shout."""
43 try:
44 shout = Shout.objects.get(pk=id)
45 except Shout.DoesNotExist:
46 return render_to_response('shoutbox/missing_shout.html', {},
47 context_instance = RequestContext(request))
48
49 return render_to_response('shoutbox/view_shout.html', {
50 'shout': shout,
51 },
52 context_instance = RequestContext(request))
53
54
55 def view_history(request):
56 """This view allows one to view the shoutbox history."""
57 paginator = DiggPaginator(Shout.objects.all().select_related(),
58 SHOUTS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
59 page = get_page(request.GET)
60 try:
61 the_page = paginator.page(page)
62 except InvalidPage:
63 raise Http404
64
65 return render_to_response('shoutbox/view.html', {
66 'page': the_page,
67 },
68 context_instance = RequestContext(request))
69
70
71 shout_id_re = re.compile(r'shout-(\d+)')
72
73 def text(request):
74 """This view function retrieves the text of a shout; it is used in the in-place
75 editing of shouts on the shoutbox history view."""
76 if request.user.is_authenticated():
77 m = shout_id_re.match(request.GET.get('id', ''))
78 if m is None:
79 return HttpResponseBadRequest()
80 try:
81 shout = Shout.objects.get(pk=m.group(1))
82 except Shout.DoesNotExist:
83 return HttpResponseBadRequest()
84 return HttpResponse(shout.shout)
85
86 return HttpResponseForbidden()
87
88
89 def edit(request):
90 """This view accepts a shoutbox edit from the shoutbox history view."""
91 if request.user.is_authenticated():
92 m = shout_id_re.match(request.POST.get('id', ''))
93 if m is None:
94 return HttpResponseBadRequest()
95 try:
96 shout = Shout.objects.get(pk=m.group(1))
97 except Shout.DoesNotExist:
98 return HttpResponseBadRequest()
99 if request.user != shout.user:
100 return HttpResponseForbidden()
101 new_shout = request.POST.get('value', '').strip()
102 if new_shout == '':
103 return HttpResponseBadRequest()
104 shout.shout = new_shout
105 shout.save()
106 return HttpResponse(shout.html)
107
108 return HttpResponseForbidden()
109
110
111 def delete(request):
112 """This view deletes a shout. It is called by AJAX from the shoutbox history view."""
113 if request.user.is_authenticated():
114 id = request.POST.get('id', None)
115 if id is None or not id.isdigit():
116 return HttpResponseBadRequest()
117 try:
118 shout = Shout.objects.get(pk=id)
119 except Shout.DoesNotExist:
120 return HttpResponseBadRequest()
121 if request.user != shout.user:
122 return HttpResponseForbidden()
123 shout.delete()
124 return HttpResponse(id)
125
126 return HttpResponseForbidden()
127
128
129 @require_POST
130 def flag(request):
131 """
132 This function handles the flagging of shouts by users. This function should
133 be the target of an AJAX post.
134 """
135 if not request.user.is_authenticated():
136 return HttpResponse('Please login or register to flag a shout.')
137
138 id = request.POST.get('id', None)
139 if id is None:
140 return HttpResponseBadRequest('No id')
141
142 try:
143 shout = Shout.objects.get(pk=id)
144 except Shout.DoesNotExist:
145 return HttpResponseBadRequest('No shout with id %s' % id)
146
147 flag = ShoutFlag(user=request.user, shout=shout)
148 flag.save()
149 email_admins('A Shout Has Been Flagged', """Hello,
150
151 A user has flagged a shout for review.
152 """)
153 return HttpResponse('The shout was flagged. A moderator will review the shout shortly. ' \
154 'Thanks for helping to improve the quality of this site.')
155
156 # vim: ts=4 sw=4