From a269a2058a3e66b5c0d1a08eb2c81d31b4222327 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 22 Sep 2005 05:32:47 +0000 Subject: [PATCH] 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 --- django/contrib/comments/views/comments.py | 28 ++++++--------------- django/contrib/comments/views/karma.py | 10 ++------ django/contrib/comments/views/userflags.py | 29 +++++----------------- 3 files changed, 16 insertions(+), 51 deletions(-) diff --git a/django/contrib/comments/views/comments.py b/django/contrib/comments/views/comments.py index 324ba0f0c9..3854c024b3 100644 --- a/django/contrib/comments/views/comments.py +++ b/django/contrib/comments/views/comments.py @@ -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.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.comments import comments, freecomments from django.models.core import contenttypes 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.conf.settings import BANNED_IPS, COMMENTS_ALLOW_PROFANITIES, COMMENTS_SKETCHY_USERS_GROUP, COMMENTS_FIRST_FEW, SITE_ID import base64, datetime @@ -206,7 +206,6 @@ def post_comment(request): new_data['object_id'] = object_id new_data['ip_address'] = request.META.get('REMOTE_ADDR') new_data['is_public'] = comments.IS_PUBLIC in option_list - response = HttpResponse() manipulator = PublicCommentManipulator(request.user, ratings_required=comments.RATINGS_REQUIRED in option_list, ratings_range=rating_range, @@ -228,8 +227,7 @@ def post_comment(request): return field_list comment = errors and '' or manipulator.get_comment(new_data) comment_form = CommentFormWrapper(manipulator, new_data, errors, rating_choices) - t = template_loader.get_template('comments/preview') - c = Context(request, { + return load_and_render('comments/preview', { 'comment': comment, 'comment_form': comment_form, 'options': options, @@ -240,7 +238,7 @@ def post_comment(request): 'ratings_required': comments.RATINGS_REQUIRED in option_list, 'rating_range': rating_range, 'rating_choices': rating_choices, - }) + }, context_instance=DjangoContext(request)) elif request.POST.has_key('post'): # 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. @@ -252,8 +250,6 @@ def post_comment(request): return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) else: raise Http404, "The comment form didn't provide either 'preview' or 'post'" - response.write(t.render(c)) - return response def post_free_comment(request): """ @@ -295,19 +291,17 @@ def post_free_comment(request): new_data['object_id'] = object_id new_data['ip_address'] = request.META['REMOTE_ADDR'] new_data['is_public'] = comments.IS_PUBLIC in option_list - response = HttpResponse() manipulator = PublicFreeCommentManipulator() errors = manipulator.get_validation_errors(new_data) if errors or request.POST.has_key('preview'): comment = errors and '' or manipulator.get_comment(new_data) - t = template_loader.get_template('comments/free_preview') - c = Context(request, { + return load_and_render('comments/free_preview', { 'comment': comment, 'comment_form': formfields.FormWrapper(manipulator, new_data, errors), 'options': options, 'target': target, 'hash': security_hash, - }) + }, context_instance=DjangoContext(request)) elif request.POST.has_key('post'): # 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. @@ -320,8 +314,6 @@ def post_free_comment(request): return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) else: raise Http404, "The comment form didn't provide either 'preview' or 'post'" - response.write(t.render(c)) - return response 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) except ObjectDoesNotExist: pass - t = template_loader.get_template('comments/posted') - c = Context(request, { - 'object': obj, - }) - return HttpResponse(t.render(c)) + return load_and_render('comments/posted', {'object': obj}, context_instance=DjangoContext(request)) diff --git a/django/contrib/comments/views/karma.py b/django/contrib/comments/views/karma.py index 7422047fad..24d3802337 100644 --- a/django/contrib/comments/views/karma.py +++ b/django/contrib/comments/views/karma.py @@ -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.extensions import DjangoContext, load_and_render from django.models.comments import comments, karma -from django.utils.httpwrappers import HttpResponse def vote(request, comment_id, vote): """ @@ -27,8 +25,4 @@ def vote(request, comment_id, vote): karma.vote(request.user.id, comment_id, rating) # Reload comment to ensure we have up to date karma count comment = comments.get_object(pk=comment_id) - t = template_loader.get_template('comments/karma_vote_accepted') - c = Context(request, { - 'comment': comment - }) - return HttpResponse(t.render(c)) + return load_and_render('comments/karma_vote_accepted', {'comment': comment}, context_instance=DjangoContext(request)) diff --git a/django/contrib/comments/views/userflags.py b/django/contrib/comments/views/userflags.py index 3395fe017e..ac0c70bda0 100644 --- a/django/contrib/comments/views/userflags.py +++ b/django/contrib/comments/views/userflags.py @@ -1,9 +1,8 @@ -from django.core import template_loader -from django.core.extensions import DjangoContext as Context +from django.core.extensions import DjangoContext, load_and_render from django.core.exceptions import Http404 from django.models.comments import comments, moderatordeletions, userflags 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 def flag(request, comment_id): @@ -22,11 +21,7 @@ def flag(request, comment_id): if request.POST: userflags.flag(comment, request.user) return HttpResponseRedirect('%sdone/' % request.path) - t = template_loader.get_template('comments/flag_verify') - c = Context(request, { - 'comment': comment, - }) - return HttpResponse(t.render(c)) + return load_and_render('comments/flag_verify', {'comment': comment}, context_instance=DjangoContext(request)) flag = login_required(flag) 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) except comments.CommentDoesNotExist: raise Http404 - t = template_loader.get_template('comments/flag_done') - c = Context(request, { - 'comment': comment, - }) - return HttpResponse(t.render(c)) + return load_and_render('comments/flag_done', {'comment': comment}, context_instance=DjangoContext(request)) 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.save() return HttpResponseRedirect('%sdone/' % request.path) - t = template_loader.get_template('comments/delete_verify') - c = Context(request, { - 'comment': comment, - }) - return HttpResponse(t.render(c)) + return load_and_render('comments/delete_verify', {'comment': comment}, context_instance=DjangoContext(request)) delete = login_required(delete) 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) except comments.CommentDoesNotExist: raise Http404 - t = template_loader.get_template('comments/delete_done') - c = Context(request, { - 'comment': comment, - }) - return HttpResponse(t.render(c)) + return load_and_render('comments/delete_done', {'comment': comment}, context_instance=DjangoContext(request))