2011-10-18 02:45:22 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
from django import template
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib import comments
|
2011-10-18 02:45:22 +08:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2008-08-26 06:14:22 +08:00
|
|
|
from django.contrib.comments import signals
|
2011-10-18 02:45:22 +08:00
|
|
|
from django.contrib.comments.views.utils import next_redirect, confirmation_view
|
|
|
|
from django.shortcuts import get_object_or_404, render_to_response
|
2009-10-27 08:36:34 +08:00
|
|
|
from django.views.decorators.csrf import csrf_protect
|
2008-08-26 06:14:22 +08:00
|
|
|
|
2011-10-18 02:45:22 +08:00
|
|
|
|
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
@csrf_protect
|
2009-10-24 03:22:31 +08:00
|
|
|
@login_required
|
2008-08-26 06:14:22 +08:00
|
|
|
def flag(request, comment_id, next=None):
|
|
|
|
"""
|
|
|
|
Flags a comment. Confirmation on GET, action on POST.
|
|
|
|
|
|
|
|
Templates: `comments/flag.html`,
|
|
|
|
Context:
|
|
|
|
comment
|
|
|
|
the flagged `comments.comment` object
|
|
|
|
"""
|
|
|
|
comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID)
|
|
|
|
|
|
|
|
# Flag on POST
|
|
|
|
if request.method == 'POST':
|
2009-10-24 03:22:31 +08:00
|
|
|
perform_flag(request, comment)
|
2008-08-26 06:14:22 +08:00
|
|
|
return next_redirect(request.POST.copy(), next, flag_done, c=comment.pk)
|
|
|
|
|
|
|
|
# Render a form on GET
|
|
|
|
else:
|
|
|
|
return render_to_response('comments/flag.html',
|
|
|
|
{'comment': comment, "next": next},
|
|
|
|
template.RequestContext(request)
|
|
|
|
)
|
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
@csrf_protect
|
2009-10-24 03:22:31 +08:00
|
|
|
@permission_required("comments.can_moderate")
|
2008-08-26 06:14:22 +08:00
|
|
|
def delete(request, comment_id, next=None):
|
|
|
|
"""
|
|
|
|
Deletes a comment. Confirmation on GET, action on POST. Requires the "can
|
|
|
|
moderate comments" permission.
|
|
|
|
|
|
|
|
Templates: `comments/delete.html`,
|
|
|
|
Context:
|
|
|
|
comment
|
|
|
|
the flagged `comments.comment` object
|
|
|
|
"""
|
|
|
|
comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID)
|
|
|
|
|
|
|
|
# Delete on POST
|
|
|
|
if request.method == 'POST':
|
|
|
|
# Flag the comment as deleted instead of actually deleting it.
|
2009-10-24 03:22:31 +08:00
|
|
|
perform_delete(request, comment)
|
2008-08-26 06:14:22 +08:00
|
|
|
return next_redirect(request.POST.copy(), next, delete_done, c=comment.pk)
|
|
|
|
|
|
|
|
# Render a form on GET
|
|
|
|
else:
|
|
|
|
return render_to_response('comments/delete.html',
|
|
|
|
{'comment': comment, "next": next},
|
|
|
|
template.RequestContext(request)
|
|
|
|
)
|
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
@csrf_protect
|
2009-10-24 03:22:31 +08:00
|
|
|
@permission_required("comments.can_moderate")
|
2008-08-26 06:14:22 +08:00
|
|
|
def approve(request, comment_id, next=None):
|
|
|
|
"""
|
|
|
|
Approve a comment (that is, mark it as public and non-removed). Confirmation
|
|
|
|
on GET, action on POST. Requires the "can moderate comments" permission.
|
|
|
|
|
|
|
|
Templates: `comments/approve.html`,
|
|
|
|
Context:
|
|
|
|
comment
|
|
|
|
the `comments.comment` object for approval
|
|
|
|
"""
|
|
|
|
comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID)
|
|
|
|
|
|
|
|
# Delete on POST
|
|
|
|
if request.method == 'POST':
|
|
|
|
# Flag the comment as approved.
|
2009-10-24 03:22:31 +08:00
|
|
|
perform_approve(request, comment)
|
2008-08-26 06:14:22 +08:00
|
|
|
return next_redirect(request.POST.copy(), next, approve_done, c=comment.pk)
|
|
|
|
|
|
|
|
# Render a form on GET
|
|
|
|
else:
|
|
|
|
return render_to_response('comments/approve.html',
|
|
|
|
{'comment': comment, "next": next},
|
|
|
|
template.RequestContext(request)
|
|
|
|
)
|
|
|
|
|
2009-10-24 03:22:31 +08:00
|
|
|
# The following functions actually perform the various flag/aprove/delete
|
2011-08-12 22:14:15 +08:00
|
|
|
# actions. They've been broken out into separate functions to that they
|
2009-10-24 03:22:31 +08:00
|
|
|
# may be called from admin actions.
|
2008-08-26 06:14:22 +08:00
|
|
|
|
2009-10-24 03:22:31 +08:00
|
|
|
def perform_flag(request, comment):
|
2008-08-26 06:14:22 +08:00
|
|
|
"""
|
2009-10-24 03:22:31 +08:00
|
|
|
Actually perform the flagging of a comment from a request.
|
2008-08-26 06:14:22 +08:00
|
|
|
"""
|
2009-10-24 03:22:31 +08:00
|
|
|
flag, created = comments.models.CommentFlag.objects.get_or_create(
|
|
|
|
comment = comment,
|
|
|
|
user = request.user,
|
|
|
|
flag = comments.models.CommentFlag.SUGGEST_REMOVAL
|
|
|
|
)
|
|
|
|
signals.comment_was_flagged.send(
|
|
|
|
sender = comment.__class__,
|
|
|
|
comment = comment,
|
|
|
|
flag = flag,
|
|
|
|
created = created,
|
|
|
|
request = request,
|
|
|
|
)
|
|
|
|
|
|
|
|
def perform_delete(request, comment):
|
|
|
|
flag, created = comments.models.CommentFlag.objects.get_or_create(
|
|
|
|
comment = comment,
|
|
|
|
user = request.user,
|
|
|
|
flag = comments.models.CommentFlag.MODERATOR_DELETION
|
|
|
|
)
|
|
|
|
comment.is_removed = True
|
|
|
|
comment.save()
|
|
|
|
signals.comment_was_flagged.send(
|
|
|
|
sender = comment.__class__,
|
|
|
|
comment = comment,
|
|
|
|
flag = flag,
|
|
|
|
created = created,
|
|
|
|
request = request,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def perform_approve(request, comment):
|
|
|
|
flag, created = comments.models.CommentFlag.objects.get_or_create(
|
|
|
|
comment = comment,
|
|
|
|
user = request.user,
|
|
|
|
flag = comments.models.CommentFlag.MODERATOR_APPROVAL,
|
|
|
|
)
|
|
|
|
|
|
|
|
comment.is_removed = False
|
|
|
|
comment.is_public = True
|
|
|
|
comment.save()
|
|
|
|
|
|
|
|
signals.comment_was_flagged.send(
|
|
|
|
sender = comment.__class__,
|
|
|
|
comment = comment,
|
|
|
|
flag = flag,
|
|
|
|
created = created,
|
|
|
|
request = request,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Confirmation views.
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
flag_done = confirmation_view(
|
|
|
|
template = "comments/flagged.html",
|
|
|
|
doc = 'Displays a "comment was flagged" success page.'
|
|
|
|
)
|
|
|
|
delete_done = confirmation_view(
|
|
|
|
template = "comments/deleted.html",
|
|
|
|
doc = 'Displays a "comment was deleted" success page.'
|
|
|
|
)
|
|
|
|
approve_done = confirmation_view(
|
|
|
|
template = "comments/approved.html",
|
|
|
|
doc = 'Displays a "comment was approved" success page.'
|
|
|
|
)
|