<s> from __future__ import absolute_import <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . contrib import comments <EOL> from django . contrib . comments import signals <EOL> from django . contrib . comments . views . utils import next_redirect , confirmation_view <EOL> from django . core . exceptions import ObjectDoesNotExist , ValidationError <EOL> from django . db import models <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from django . template . loader import render_to_string <EOL> from django . utils . html import escape <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from django . views . decorators . http import require_POST <EOL> class CommentPostBadRequest ( http . HttpResponseBadRequest ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , why ) : <EOL> super ( CommentPostBadRequest , self ) . __init__ ( ) <EOL> if settings . DEBUG : <EOL> self . content = render_to_string ( "<STR_LIT>" , { "<STR_LIT>" : why } ) <EOL> @ csrf_protect <EOL> @ require_POST <EOL> def post_comment ( request , next = None , using = None ) : <EOL> """<STR_LIT>""" <EOL> data = request . POST . copy ( ) <EOL> if request . user . is_authenticated ( ) : <EOL> if not data . get ( '<STR_LIT:name>' , '<STR_LIT>' ) : <EOL> data [ "<STR_LIT:name>" ] = request . user . get_full_name ( ) or request . user . get_username ( ) <EOL> if not data . get ( '<STR_LIT:email>' , '<STR_LIT>' ) : <EOL> data [ "<STR_LIT:email>" ] = request . user . email <EOL> ctype = data . get ( "<STR_LIT>" ) <EOL> object_pk = data . get ( "<STR_LIT>" ) <EOL> if ctype is None or object_pk is None : <EOL> return CommentPostBadRequest ( "<STR_LIT>" ) <EOL> try : <EOL> model = models . get_model ( * ctype . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) ) <EOL> target = model . _default_manager . using ( using ) . get ( pk = object_pk ) <EOL> except TypeError : <EOL> return CommentPostBadRequest ( <EOL> "<STR_LIT>" % escape ( ctype ) ) <EOL> except AttributeError : <EOL> return CommentPostBadRequest ( <EOL> "<STR_LIT>" % escape ( ctype ) ) <EOL> except ObjectDoesNotExist : <EOL> return CommentPostBadRequest ( <EOL> "<STR_LIT>" % ( escape ( ctype ) , escape ( object_pk ) ) ) <EOL> except ( ValueError , ValidationError ) as e : <EOL> return CommentPostBadRequest ( <EOL> "<STR_LIT>" % ( escape ( ctype ) , escape ( object_pk ) , e . __class__ . __name__ ) ) <EOL> preview = "<STR_LIT>" in data <EOL> form = comments . get_form ( ) ( target , data = data ) <EOL> if form . security_errors ( ) : <EOL> return CommentPostBadRequest ( <EOL> "<STR_LIT>" % escape ( str ( form . security_errors ( ) ) ) ) <EOL> if form . errors or preview : <EOL> template_list = [ <EOL> "<STR_LIT>" % ( model . _meta . app_label , model . _meta . module_name ) , <EOL> "<STR_LIT>" % model . _meta . app_label , <EOL> "<STR_LIT>" % ( model . _meta . app_label , model . _meta . module_name ) , <EOL> "<STR_LIT>" % model . _meta . app_label , <EOL> "<STR_LIT>" , <EOL> ] <EOL> return render_to_response ( <EOL> template_list , { <EOL> "<STR_LIT>" : form . data . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : data . get ( "<STR_LIT>" , next ) , <EOL> } , <EOL> RequestContext ( request , { } ) <EOL> ) <EOL> comment = form . get_comment_object ( ) <EOL> comment . ip_address = request . META . get ( "<STR_LIT>" , None ) <EOL> if request . user . is_authenticated ( ) : <EOL> comment . user = request . user <EOL> responses = signals . comment_will_be_posted . send ( <EOL> sender = comment . __class__ , <EOL> comment = comment , <EOL> request = request <EOL> ) <EOL> for ( receiver , response ) in responses : <EOL> if response == False : <EOL> return CommentPostBadRequest ( <EOL> "<STR_LIT>" % receiver . __name__ ) <EOL> comment . save ( ) <EOL> signals . comment_was_posted . send ( <EOL> sender = comment . __class__ , <EOL> comment = comment , <EOL> request = request <EOL> ) <EOL> return next_redirect ( request , fallback = next or '<STR_LIT>' , <EOL> c = comment . _get_pk_val ( ) ) <EOL> comment_done = confirmation_view ( <EOL> template = "<STR_LIT>" , <EOL> doc = """<STR_LIT>""" <EOL> ) </s>
