gremmie@1: """ gremmie@1: Views for the Shoutbox application. gremmie@1: """ gremmie@1: gremmie@1: import re gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.template import RequestContext bgneal@92: from django.core.paginator import InvalidPage gremmie@1: from django.http import HttpResponse gremmie@1: from django.http import HttpResponseBadRequest gremmie@1: from django.http import HttpResponseForbidden gremmie@1: from django.http import HttpResponseRedirect gremmie@1: from django.contrib.auth.decorators import login_required bgneal@12: from django.views.decorators.http import require_POST gremmie@1: gremmie@1: from core.paginator import DiggPaginator bgneal@13: from core.functions import email_admins gremmie@1: from shoutbox.forms import ShoutBoxForm gremmie@1: from shoutbox.models import Shout bgneal@13: from shoutbox.models import ShoutFlag gremmie@1: gremmie@1: SHOUTS_PER_PAGE = 10 gremmie@1: gremmie@1: @login_required bgneal@12: @require_POST gremmie@1: def shout(request): bgneal@12: msg = request.POST.get('msg', '').strip() bgneal@12: if msg == '': bgneal@12: return HttpResponse('') bgneal@12: bgneal@12: shout = Shout(user=request.user, shout=msg) bgneal@12: shout.save() bgneal@12: return render_to_response('shoutbox/shout.html', { bgneal@12: 'shout': shout, bgneal@12: }, bgneal@12: context_instance = RequestContext(request)) gremmie@1: gremmie@1: bgneal@13: def view_shout(request, id): bgneal@13: """This view is for viewing an individual shout.""" bgneal@13: try: bgneal@13: shout = Shout.objects.get(pk=id) bgneal@13: except Shout.DoesNotExist: bgneal@13: return render_to_response('shoutbox/missing_shout.html', {}, bgneal@13: context_instance = RequestContext(request)) bgneal@13: bgneal@13: return render_to_response('shoutbox/view_shout.html', { bgneal@13: 'shout': shout, bgneal@13: }, bgneal@13: context_instance = RequestContext(request)) bgneal@13: bgneal@13: bgneal@13: def view_history(request, page=1): gremmie@1: """This view allows one to view the shoutbox history.""" gremmie@1: paginator = DiggPaginator(Shout.objects.all(), SHOUTS_PER_PAGE, body=5, tail=3, margin=3, padding=2) gremmie@1: try: gremmie@1: the_page = paginator.page(int(page)) gremmie@1: except InvalidPage: gremmie@1: raise Http404 gremmie@1: gremmie@1: return render_to_response('shoutbox/view.html', { gremmie@1: 'page': the_page, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: gremmie@1: shout_id_re = re.compile(r'shout-(\d+)') gremmie@1: gremmie@1: def text(request): gremmie@1: """This view function retrieves the text of a shout; it is used in the in-place gremmie@1: editing of shouts on the shoutbox history view.""" gremmie@1: if request.user.is_authenticated(): gremmie@1: m = shout_id_re.match(request.GET.get('id', '')) gremmie@1: if m is None: gremmie@1: return HttpResponseBadRequest() gremmie@1: try: gremmie@1: shout = Shout.objects.get(pk=m.group(1)) gremmie@1: except Shout.DoesNotExist: gremmie@1: return HttpResponseBadRequest() gremmie@1: return HttpResponse(shout.shout) gremmie@1: gremmie@1: return HttpResponseForbidden() gremmie@1: gremmie@1: gremmie@1: def edit(request): gremmie@1: """This view accepts a shoutbox edit from the shoutbox history view.""" gremmie@1: if request.user.is_authenticated(): gremmie@1: m = shout_id_re.match(request.POST.get('id', '')) gremmie@1: if m is None: gremmie@1: return HttpResponseBadRequest() gremmie@1: try: gremmie@1: shout = Shout.objects.get(pk=m.group(1)) gremmie@1: except Shout.DoesNotExist: gremmie@1: return HttpResponseBadRequest() gremmie@1: if request.user != shout.user: gremmie@1: return HttpResponseForbidden() gremmie@1: new_shout = request.POST.get('value', '').strip() gremmie@1: if new_shout == '': gremmie@1: return HttpResponseBadRequest() gremmie@1: shout.shout = new_shout gremmie@1: shout.save() gremmie@1: return render_to_response('shoutbox/render_shout.html', { gremmie@1: 'shout': shout, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: return HttpResponseForbidden() gremmie@1: gremmie@1: gremmie@1: def delete(request): gremmie@1: """This view deletes a shout. It is called by AJAX from the shoutbox history view.""" gremmie@1: if request.user.is_authenticated(): gremmie@1: id = request.POST.get('id', None) gremmie@1: if id is None or not id.isdigit(): gremmie@1: return HttpResponseBadRequest() gremmie@1: try: gremmie@1: shout = Shout.objects.get(pk=id) gremmie@1: except Shout.DoesNotExist: gremmie@1: return HttpResponseBadRequest() gremmie@1: if request.user != shout.user: gremmie@1: return HttpResponseForbidden() gremmie@1: shout.delete() gremmie@1: return HttpResponse(id) gremmie@1: gremmie@1: return HttpResponseForbidden() bgneal@12: bgneal@13: bgneal@13: @require_POST bgneal@13: def flag(request): bgneal@13: """ bgneal@13: This function handles the flagging of shouts by users. This function should bgneal@13: be the target of an AJAX post. bgneal@13: """ bgneal@13: if not request.user.is_authenticated(): bgneal@13: return HttpResponse('Please login or register to flag a shout.') bgneal@13: bgneal@13: id = request.POST.get('id', None) bgneal@13: if id is None: bgneal@13: return HttpResponseBadRequest('No id') bgneal@13: bgneal@13: try: bgneal@13: shout = Shout.objects.get(pk=id) bgneal@13: except Shout.DoesNotExist: bgneal@13: return HttpResponseBadRequest('No shout with id %s' % id) bgneal@13: bgneal@13: flag = ShoutFlag(user=request.user, shout=shout) bgneal@13: flag.save() bgneal@13: email_admins('A Shout Has Been Flagged', """Hello, bgneal@13: bgneal@13: A user has flagged a shout for review. bgneal@13: """) bgneal@13: return HttpResponse('The shout was flagged. A moderator will review the shout shortly. ' \ bgneal@13: 'Thanks for helping to improve the quality of this site.') bgneal@13: bgneal@12: # vim: ts=4 sw=4