Converted django.contrib.comments.views.* to use load_and_render

git-svn-id: http://code.djangoproject.com/svn/django/trunk@661 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-09-22 05:32:47 +00:00
parent 4175aec30f
commit a269a2058a
3 changed files with 16 additions and 51 deletions

View File

@ -1,12 +1,12 @@
from django.core import formfields, template_loader, validators from django.core import formfields, validators
from django.core.mail import mail_admins, mail_managers from django.core.mail import mail_admins, mail_managers
from django.core.exceptions import Http404, ObjectDoesNotExist from django.core.exceptions import Http404, ObjectDoesNotExist
from django.core.extensions import DjangoContext as Context from django.core.extensions import DjangoContext, load_and_render
from django.models.auth import users from django.models.auth import users
from django.models.comments import comments, freecomments from django.models.comments import comments, freecomments
from django.models.core import contenttypes from django.models.core import contenttypes
from django.parts.auth.formfields import AuthenticationForm from django.parts.auth.formfields import AuthenticationForm
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect from django.utils.httpwrappers import HttpResponseRedirect
from django.utils.text import normalize_newlines from django.utils.text import normalize_newlines
from django.conf.settings import BANNED_IPS, COMMENTS_ALLOW_PROFANITIES, COMMENTS_SKETCHY_USERS_GROUP, COMMENTS_FIRST_FEW, SITE_ID from django.conf.settings import BANNED_IPS, COMMENTS_ALLOW_PROFANITIES, COMMENTS_SKETCHY_USERS_GROUP, COMMENTS_FIRST_FEW, SITE_ID
import base64, datetime import base64, datetime
@ -206,7 +206,6 @@ def post_comment(request):
new_data['object_id'] = object_id new_data['object_id'] = object_id
new_data['ip_address'] = request.META.get('REMOTE_ADDR') new_data['ip_address'] = request.META.get('REMOTE_ADDR')
new_data['is_public'] = comments.IS_PUBLIC in option_list new_data['is_public'] = comments.IS_PUBLIC in option_list
response = HttpResponse()
manipulator = PublicCommentManipulator(request.user, manipulator = PublicCommentManipulator(request.user,
ratings_required=comments.RATINGS_REQUIRED in option_list, ratings_required=comments.RATINGS_REQUIRED in option_list,
ratings_range=rating_range, ratings_range=rating_range,
@ -228,8 +227,7 @@ def post_comment(request):
return field_list return field_list
comment = errors and '' or manipulator.get_comment(new_data) comment = errors and '' or manipulator.get_comment(new_data)
comment_form = CommentFormWrapper(manipulator, new_data, errors, rating_choices) comment_form = CommentFormWrapper(manipulator, new_data, errors, rating_choices)
t = template_loader.get_template('comments/preview') return load_and_render('comments/preview', {
c = Context(request, {
'comment': comment, 'comment': comment,
'comment_form': comment_form, 'comment_form': comment_form,
'options': options, 'options': options,
@ -240,7 +238,7 @@ def post_comment(request):
'ratings_required': comments.RATINGS_REQUIRED in option_list, 'ratings_required': comments.RATINGS_REQUIRED in option_list,
'rating_range': rating_range, 'rating_range': rating_range,
'rating_choices': rating_choices, 'rating_choices': rating_choices,
}) }, context_instance=DjangoContext(request))
elif request.POST.has_key('post'): elif request.POST.has_key('post'):
# If the IP is banned, mail the admins, do NOT save the comment, and # If the IP is banned, mail the admins, do NOT save the comment, and
# serve up the "Thanks for posting" page as if the comment WAS posted. # serve up the "Thanks for posting" page as if the comment WAS posted.
@ -252,8 +250,6 @@ def post_comment(request):
return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id))
else: else:
raise Http404, "The comment form didn't provide either 'preview' or 'post'" raise Http404, "The comment form didn't provide either 'preview' or 'post'"
response.write(t.render(c))
return response
def post_free_comment(request): def post_free_comment(request):
""" """
@ -295,19 +291,17 @@ def post_free_comment(request):
new_data['object_id'] = object_id new_data['object_id'] = object_id
new_data['ip_address'] = request.META['REMOTE_ADDR'] new_data['ip_address'] = request.META['REMOTE_ADDR']
new_data['is_public'] = comments.IS_PUBLIC in option_list new_data['is_public'] = comments.IS_PUBLIC in option_list
response = HttpResponse()
manipulator = PublicFreeCommentManipulator() manipulator = PublicFreeCommentManipulator()
errors = manipulator.get_validation_errors(new_data) errors = manipulator.get_validation_errors(new_data)
if errors or request.POST.has_key('preview'): if errors or request.POST.has_key('preview'):
comment = errors and '' or manipulator.get_comment(new_data) comment = errors and '' or manipulator.get_comment(new_data)
t = template_loader.get_template('comments/free_preview') return load_and_render('comments/free_preview', {
c = Context(request, {
'comment': comment, 'comment': comment,
'comment_form': formfields.FormWrapper(manipulator, new_data, errors), 'comment_form': formfields.FormWrapper(manipulator, new_data, errors),
'options': options, 'options': options,
'target': target, 'target': target,
'hash': security_hash, 'hash': security_hash,
}) }, context_instance=DjangoContext(request))
elif request.POST.has_key('post'): elif request.POST.has_key('post'):
# If the IP is banned, mail the admins, do NOT save the comment, and # If the IP is banned, mail the admins, do NOT save the comment, and
# serve up the "Thanks for posting" page as if the comment WAS posted. # serve up the "Thanks for posting" page as if the comment WAS posted.
@ -320,8 +314,6 @@ def post_free_comment(request):
return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id))
else: else:
raise Http404, "The comment form didn't provide either 'preview' or 'post'" raise Http404, "The comment form didn't provide either 'preview' or 'post'"
response.write(t.render(c))
return response
def comment_was_posted(request): def comment_was_posted(request):
""" """
@ -340,8 +332,4 @@ def comment_was_posted(request):
obj = content_type.get_object_for_this_type(pk=object_id) obj = content_type.get_object_for_this_type(pk=object_id)
except ObjectDoesNotExist: except ObjectDoesNotExist:
pass pass
t = template_loader.get_template('comments/posted') return load_and_render('comments/posted', {'object': obj}, context_instance=DjangoContext(request))
c = Context(request, {
'object': obj,
})
return HttpResponse(t.render(c))

View File

@ -1,8 +1,6 @@
from django.core import template_loader
from django.core.extensions import DjangoContext as Context
from django.core.exceptions import Http404 from django.core.exceptions import Http404
from django.core.extensions import DjangoContext, load_and_render
from django.models.comments import comments, karma from django.models.comments import comments, karma
from django.utils.httpwrappers import HttpResponse
def vote(request, comment_id, vote): def vote(request, comment_id, vote):
""" """
@ -27,8 +25,4 @@ def vote(request, comment_id, vote):
karma.vote(request.user.id, comment_id, rating) karma.vote(request.user.id, comment_id, rating)
# Reload comment to ensure we have up to date karma count # Reload comment to ensure we have up to date karma count
comment = comments.get_object(pk=comment_id) comment = comments.get_object(pk=comment_id)
t = template_loader.get_template('comments/karma_vote_accepted') return load_and_render('comments/karma_vote_accepted', {'comment': comment}, context_instance=DjangoContext(request))
c = Context(request, {
'comment': comment
})
return HttpResponse(t.render(c))

View File

@ -1,9 +1,8 @@
from django.core import template_loader from django.core.extensions import DjangoContext, load_and_render
from django.core.extensions import DjangoContext as Context
from django.core.exceptions import Http404 from django.core.exceptions import Http404
from django.models.comments import comments, moderatordeletions, userflags from django.models.comments import comments, moderatordeletions, userflags
from django.views.decorators.auth import login_required from django.views.decorators.auth import login_required
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect from django.utils.httpwrappers import HttpResponseRedirect
from django.conf.settings import SITE_ID from django.conf.settings import SITE_ID
def flag(request, comment_id): def flag(request, comment_id):
@ -22,11 +21,7 @@ def flag(request, comment_id):
if request.POST: if request.POST:
userflags.flag(comment, request.user) userflags.flag(comment, request.user)
return HttpResponseRedirect('%sdone/' % request.path) return HttpResponseRedirect('%sdone/' % request.path)
t = template_loader.get_template('comments/flag_verify') return load_and_render('comments/flag_verify', {'comment': comment}, context_instance=DjangoContext(request))
c = Context(request, {
'comment': comment,
})
return HttpResponse(t.render(c))
flag = login_required(flag) flag = login_required(flag)
def flag_done(request, comment_id): def flag_done(request, comment_id):
@ -34,11 +29,7 @@ def flag_done(request, comment_id):
comment = comments.get_object(pk=comment_id, site__id__exact=SITE_ID) comment = comments.get_object(pk=comment_id, site__id__exact=SITE_ID)
except comments.CommentDoesNotExist: except comments.CommentDoesNotExist:
raise Http404 raise Http404
t = template_loader.get_template('comments/flag_done') return load_and_render('comments/flag_done', {'comment': comment}, context_instance=DjangoContext(request))
c = Context(request, {
'comment': comment,
})
return HttpResponse(t.render(c))
def delete(request, comment_id): def delete(request, comment_id):
""" """
@ -63,11 +54,7 @@ def delete(request, comment_id):
m = moderatordeletions.ModeratorDeletion(None, request.user.id, comment.id, None) m = moderatordeletions.ModeratorDeletion(None, request.user.id, comment.id, None)
m.save() m.save()
return HttpResponseRedirect('%sdone/' % request.path) return HttpResponseRedirect('%sdone/' % request.path)
t = template_loader.get_template('comments/delete_verify') return load_and_render('comments/delete_verify', {'comment': comment}, context_instance=DjangoContext(request))
c = Context(request, {
'comment': comment,
})
return HttpResponse(t.render(c))
delete = login_required(delete) delete = login_required(delete)
def delete_done(request, comment_id): def delete_done(request, comment_id):
@ -75,8 +62,4 @@ def delete_done(request, comment_id):
comment = comments.get_object(pk=comment_id, site__id__exact=SITE_ID) comment = comments.get_object(pk=comment_id, site__id__exact=SITE_ID)
except comments.CommentDoesNotExist: except comments.CommentDoesNotExist:
raise Http404 raise Http404
t = template_loader.get_template('comments/delete_done') return load_and_render('comments/delete_done', {'comment': comment}, context_instance=DjangoContext(request))
c = Context(request, {
'comment': comment,
})
return HttpResponse(t.render(c))