PageRenderTime 80ms CodeModel.GetById 64ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/comments/views/moderation.py

https://code.google.com/p/mango-py/
Python | 159 lines | 144 code | 4 blank | 11 comment | 1 complexity | 16576195b152075f097ad7e5c3a2e85c MD5 | raw file
  1from django import template
  2from django.conf import settings
  3from django.shortcuts import get_object_or_404, render_to_response
  4from django.contrib.auth.decorators import login_required, permission_required
  5from utils import next_redirect, confirmation_view
  6from django.contrib import comments
  7from django.contrib.comments import signals
  8from django.views.decorators.csrf import csrf_protect
  9
 10@csrf_protect
 11@login_required
 12def flag(request, comment_id, next=None):
 13    """
 14    Flags a comment. Confirmation on GET, action on POST.
 15
 16    Templates: `comments/flag.html`,
 17    Context:
 18        comment
 19            the flagged `comments.comment` object
 20    """
 21    comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID)
 22
 23    # Flag on POST
 24    if request.method == 'POST':
 25        perform_flag(request, comment)
 26        return next_redirect(request.POST.copy(), next, flag_done, c=comment.pk)
 27
 28    # Render a form on GET
 29    else:
 30        return render_to_response('comments/flag.html',
 31            {'comment': comment, "next": next},
 32            template.RequestContext(request)
 33        )
 34
 35@csrf_protect
 36@permission_required("comments.can_moderate")
 37def delete(request, comment_id, next=None):
 38    """
 39    Deletes a comment. Confirmation on GET, action on POST. Requires the "can
 40    moderate comments" permission.
 41
 42    Templates: `comments/delete.html`,
 43    Context:
 44        comment
 45            the flagged `comments.comment` object
 46    """
 47    comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID)
 48
 49    # Delete on POST
 50    if request.method == 'POST':
 51        # Flag the comment as deleted instead of actually deleting it.
 52        perform_delete(request, comment)
 53        return next_redirect(request.POST.copy(), next, delete_done, c=comment.pk)
 54
 55    # Render a form on GET
 56    else:
 57        return render_to_response('comments/delete.html',
 58            {'comment': comment, "next": next},
 59            template.RequestContext(request)
 60        )
 61
 62@csrf_protect
 63@permission_required("comments.can_moderate")
 64def approve(request, comment_id, next=None):
 65    """
 66    Approve a comment (that is, mark it as public and non-removed). Confirmation
 67    on GET, action on POST. Requires the "can moderate comments" permission.
 68
 69    Templates: `comments/approve.html`,
 70    Context:
 71        comment
 72            the `comments.comment` object for approval
 73    """
 74    comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID)
 75
 76    # Delete on POST
 77    if request.method == 'POST':
 78        # Flag the comment as approved.
 79        perform_approve(request, comment)
 80        return next_redirect(request.POST.copy(), next, approve_done, c=comment.pk)
 81
 82    # Render a form on GET
 83    else:
 84        return render_to_response('comments/approve.html',
 85            {'comment': comment, "next": next},
 86            template.RequestContext(request)
 87        )
 88
 89# The following functions actually perform the various flag/aprove/delete
 90# actions. They've been broken out into seperate functions to that they
 91# may be called from admin actions.
 92
 93def perform_flag(request, comment):
 94    """
 95    Actually perform the flagging of a comment from a request.
 96    """
 97    flag, created = comments.models.CommentFlag.objects.get_or_create(
 98        comment = comment,
 99        user    = request.user,
100        flag    = comments.models.CommentFlag.SUGGEST_REMOVAL
101    )
102    signals.comment_was_flagged.send(
103        sender  = comment.__class__,
104        comment = comment,
105        flag    = flag,
106        created = created,
107        request = request,
108    )
109
110def perform_delete(request, comment):
111    flag, created = comments.models.CommentFlag.objects.get_or_create(
112        comment = comment,
113        user    = request.user,
114        flag    = comments.models.CommentFlag.MODERATOR_DELETION
115    )
116    comment.is_removed = True
117    comment.save()
118    signals.comment_was_flagged.send(
119        sender  = comment.__class__,
120        comment = comment,
121        flag    = flag,
122        created = created,
123        request = request,
124    )
125
126
127def perform_approve(request, comment):
128    flag, created = comments.models.CommentFlag.objects.get_or_create(
129        comment = comment,
130        user    = request.user,
131        flag    = comments.models.CommentFlag.MODERATOR_APPROVAL,
132    )
133
134    comment.is_removed = False
135    comment.is_public = True
136    comment.save()
137
138    signals.comment_was_flagged.send(
139        sender  = comment.__class__,
140        comment = comment,
141        flag    = flag,
142        created = created,
143        request = request,
144    )
145
146# Confirmation views.
147
148flag_done = confirmation_view(
149    template = "comments/flagged.html",
150    doc = 'Displays a "comment was flagged" success page.'
151)
152delete_done = confirmation_view(
153    template = "comments/deleted.html",
154    doc = 'Displays a "comment was deleted" success page.'
155)
156approve_done = confirmation_view(
157    template = "comments/approved.html",
158    doc = 'Displays a "comment was approved" success page.'
159)