annotate shoutbox/views.py @ 1180:6d65aa69420e

Add 2019 MP3 links.
author Brian Neal <bgneal@gmail.com>
date Wed, 01 Jan 2020 12:54:35 -0600
parents e932f2ecd4a7
children
rev   line source
gremmie@1 1 """
gremmie@1 2 Views for the Shoutbox application.
gremmie@1 3 """
gremmie@1 4
gremmie@1 5 import re
bgneal@1032 6 from django.shortcuts import render
bgneal@92 7 from django.core.paginator import InvalidPage
gremmie@1 8 from django.http import HttpResponse
gremmie@1 9 from django.http import HttpResponseBadRequest
gremmie@1 10 from django.http import HttpResponseForbidden
bgneal@151 11 from django.http import Http404
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
bgneal@13 16 from core.functions import email_admins
bgneal@243 17 from core.functions import get_page
gremmie@1 18 from shoutbox.models import Shout
bgneal@13 19 from shoutbox.models import ShoutFlag
gremmie@1 20
gremmie@1 21 SHOUTS_PER_PAGE = 10
gremmie@1 22
gremmie@1 23 @login_required
bgneal@12 24 @require_POST
gremmie@1 25 def shout(request):
bgneal@12 26 msg = request.POST.get('msg', '').strip()
bgneal@12 27 if msg == '':
bgneal@12 28 return HttpResponse('')
bgneal@12 29
bgneal@12 30 shout = Shout(user=request.user, shout=msg)
bgneal@12 31 shout.save()
bgneal@1032 32 return render(request, 'shoutbox/shout.html', {
bgneal@12 33 'shout': shout,
bgneal@1032 34 })
gremmie@1 35
gremmie@1 36
bgneal@13 37 def view_shout(request, id):
bgneal@13 38 """This view is for viewing an individual shout."""
bgneal@13 39 try:
bgneal@13 40 shout = Shout.objects.get(pk=id)
bgneal@13 41 except Shout.DoesNotExist:
bgneal@1032 42 return render(request, 'shoutbox/missing_shout.html')
bgneal@13 43
bgneal@1032 44 return render(request, 'shoutbox/view_shout.html', {
bgneal@13 45 'shout': shout,
bgneal@1032 46 })
bgneal@13 47
bgneal@13 48
bgneal@243 49 def view_history(request):
gremmie@1 50 """This view allows one to view the shoutbox history."""
bgneal@791 51 qs = Shout.objects.select_related('user', 'user__profile')
bgneal@791 52 paginator = DiggPaginator(qs,
bgneal@118 53 SHOUTS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
bgneal@243 54 page = get_page(request.GET)
gremmie@1 55 try:
bgneal@243 56 the_page = paginator.page(page)
gremmie@1 57 except InvalidPage:
gremmie@1 58 raise Http404
gremmie@1 59
bgneal@1032 60 return render(request, 'shoutbox/view.html', {
gremmie@1 61 'page': the_page,
bgneal@1032 62 })
bgneal@791 63
gremmie@1 64
gremmie@1 65 shout_id_re = re.compile(r'shout-(\d+)')
gremmie@1 66
gremmie@1 67 def text(request):
gremmie@1 68 """This view function retrieves the text of a shout; it is used in the in-place
gremmie@1 69 editing of shouts on the shoutbox history view."""
gremmie@1 70 if request.user.is_authenticated():
gremmie@1 71 m = shout_id_re.match(request.GET.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 return HttpResponse(shout.shout)
gremmie@1 79
gremmie@1 80 return HttpResponseForbidden()
gremmie@1 81
gremmie@1 82
gremmie@1 83 def edit(request):
gremmie@1 84 """This view accepts a shoutbox edit from the shoutbox history view."""
gremmie@1 85 if request.user.is_authenticated():
gremmie@1 86 m = shout_id_re.match(request.POST.get('id', ''))
gremmie@1 87 if m is None:
gremmie@1 88 return HttpResponseBadRequest()
gremmie@1 89 try:
gremmie@1 90 shout = Shout.objects.get(pk=m.group(1))
gremmie@1 91 except Shout.DoesNotExist:
gremmie@1 92 return HttpResponseBadRequest()
gremmie@1 93 if request.user != shout.user:
gremmie@1 94 return HttpResponseForbidden()
gremmie@1 95 new_shout = request.POST.get('value', '').strip()
gremmie@1 96 if new_shout == '':
gremmie@1 97 return HttpResponseBadRequest()
gremmie@1 98 shout.shout = new_shout
gremmie@1 99 shout.save()
bgneal@151 100 return HttpResponse(shout.html)
gremmie@1 101
gremmie@1 102 return HttpResponseForbidden()
gremmie@1 103
gremmie@1 104
gremmie@1 105 def delete(request):
gremmie@1 106 """This view deletes a shout. It is called by AJAX from the shoutbox history view."""
gremmie@1 107 if request.user.is_authenticated():
gremmie@1 108 id = request.POST.get('id', None)
gremmie@1 109 if id is None or not id.isdigit():
gremmie@1 110 return HttpResponseBadRequest()
gremmie@1 111 try:
gremmie@1 112 shout = Shout.objects.get(pk=id)
gremmie@1 113 except Shout.DoesNotExist:
gremmie@1 114 return HttpResponseBadRequest()
gremmie@1 115 if request.user != shout.user:
gremmie@1 116 return HttpResponseForbidden()
gremmie@1 117 shout.delete()
gremmie@1 118 return HttpResponse(id)
gremmie@1 119
gremmie@1 120 return HttpResponseForbidden()
bgneal@12 121
bgneal@13 122
bgneal@13 123 @require_POST
bgneal@13 124 def flag(request):
bgneal@13 125 """
bgneal@13 126 This function handles the flagging of shouts by users. This function should
bgneal@13 127 be the target of an AJAX post.
bgneal@13 128 """
bgneal@13 129 if not request.user.is_authenticated():
bgneal@13 130 return HttpResponse('Please login or register to flag a shout.')
bgneal@13 131
bgneal@13 132 id = request.POST.get('id', None)
bgneal@13 133 if id is None:
bgneal@13 134 return HttpResponseBadRequest('No id')
bgneal@13 135
bgneal@13 136 try:
bgneal@13 137 shout = Shout.objects.get(pk=id)
bgneal@13 138 except Shout.DoesNotExist:
bgneal@13 139 return HttpResponseBadRequest('No shout with id %s' % id)
bgneal@13 140
bgneal@13 141 flag = ShoutFlag(user=request.user, shout=shout)
bgneal@13 142 flag.save()
bgneal@13 143 email_admins('A Shout Has Been Flagged', """Hello,
bgneal@13 144
bgneal@13 145 A user has flagged a shout for review.
bgneal@13 146 """)
bgneal@13 147 return HttpResponse('The shout was flagged. A moderator will review the shout shortly. ' \
bgneal@13 148 'Thanks for helping to improve the quality of this site.')