<s> from __future__ import unicode_literals <EOL> from future . builtins import str <EOL> from json import dumps <EOL> from string import punctuation <EOL> from django . apps import apps <EOL> from django . contrib . admin . views . decorators import staff_member_required <EOL> from django . contrib . messages import error <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import ObjectDoesNotExist <EOL> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from django . shortcuts import redirect <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from mezzanine . conf import settings <EOL> from mezzanine . generic . forms import ThreadedCommentForm , RatingForm <EOL> from mezzanine . generic . models import Keyword <EOL> from mezzanine . utils . cache import add_cache_bypass <EOL> from mezzanine . utils . views import set_cookie , is_spam <EOL> from mezzanine . utils . importing import import_dotted_path <EOL> @ staff_member_required <EOL> def admin_keywords_submit ( request ) : <EOL> """<STR_LIT>""" <EOL> keyword_ids , titles = [ ] , [ ] <EOL> remove = punctuation . replace ( "<STR_LIT:->" , "<STR_LIT>" ) <EOL> for title in request . POST . get ( "<STR_LIT>" , "<STR_LIT>" ) . split ( "<STR_LIT:U+002C>" ) : <EOL> title = "<STR_LIT>" . join ( [ c for c in title if c not in remove ] ) . strip ( ) <EOL> if title : <EOL> kw , created = Keyword . objects . get_or_create_iexact ( title = title ) <EOL> keyword_id = str ( kw . id ) <EOL> if keyword_id not in keyword_ids : <EOL> keyword_ids . append ( keyword_id ) <EOL> titles . append ( title ) <EOL> return HttpResponse ( "<STR_LIT>" % ( "<STR_LIT:U+002C>" . join ( keyword_ids ) , "<STR_LIT:U+002CU+0020>" . join ( titles ) ) , <EOL> content_type = '<STR_LIT>' ) <EOL> def initial_validation ( request , prefix ) : <EOL> """<STR_LIT>""" <EOL> post_data = request . POST <EOL> login_required_setting_name = prefix . upper ( ) + "<STR_LIT>" <EOL> posted_session_key = "<STR_LIT>" + prefix <EOL> redirect_url = "<STR_LIT>" <EOL> if getattr ( settings , login_required_setting_name , False ) : <EOL> if not request . user . is_authenticated ( ) : <EOL> request . session [ posted_session_key ] = request . POST <EOL> error ( request , _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> redirect_url = "<STR_LIT>" % ( settings . LOGIN_URL , reverse ( prefix ) ) <EOL> elif posted_session_key in request . session : <EOL> post_data = request . session . pop ( posted_session_key ) <EOL> if not redirect_url : <EOL> model_data = post_data . get ( "<STR_LIT>" , "<STR_LIT>" ) . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> if len ( model_data ) != <NUM_LIT:2> : <EOL> return HttpResponseBadRequest ( ) <EOL> try : <EOL> model = apps . get_model ( * model_data ) <EOL> obj = model . objects . get ( id = post_data . get ( "<STR_LIT>" , None ) ) <EOL> except ( TypeError , ObjectDoesNotExist , LookupError ) : <EOL> redirect_url = "<STR_LIT:/>" <EOL> if redirect_url : <EOL> if request . is_ajax ( ) : <EOL> return HttpResponse ( dumps ( { "<STR_LIT:location>" : redirect_url } ) ) <EOL> else : <EOL> return redirect ( redirect_url ) <EOL> return obj , post_data <EOL> def comment ( request , template = "<STR_LIT>" , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> response = initial_validation ( request , "<STR_LIT>" ) <EOL> if isinstance ( response , HttpResponse ) : <EOL> return response <EOL> obj , post_data = response <EOL> form_class = import_dotted_path ( settings . COMMENT_FORM_CLASS ) <EOL> form = form_class ( request , obj , post_data ) <EOL> if form . is_valid ( ) : <EOL> url = obj . get_absolute_url ( ) <EOL> if is_spam ( request , form , url ) : <EOL> return redirect ( url ) <EOL> comment = form . save ( request ) <EOL> response = redirect ( add_cache_bypass ( comment . get_absolute_url ( ) ) ) <EOL> for field in ThreadedCommentForm . cookie_fields : <EOL> cookie_name = ThreadedCommentForm . cookie_prefix + field <EOL> cookie_value = post_data . get ( field , "<STR_LIT>" ) <EOL> set_cookie ( response , cookie_name , cookie_value ) <EOL> return response <EOL> elif request . is_ajax ( ) and form . errors : <EOL> return HttpResponse ( dumps ( { "<STR_LIT>" : form . errors } ) ) <EOL> context = { "<STR_LIT>" : obj , "<STR_LIT>" : form } <EOL> context . update ( extra_context or { } ) <EOL> return TemplateResponse ( request , template , context ) <EOL> def rating ( request ) : <EOL> """<STR_LIT>""" <EOL> response = initial_validation ( request , "<STR_LIT>" ) <EOL> if isinstance ( response , HttpResponse ) : <EOL> return response <EOL> obj , post_data = response <EOL> url = add_cache_bypass ( obj . get_absolute_url ( ) . split ( "<STR_LIT:#>" ) [ <NUM_LIT:0> ] ) <EOL> response = redirect ( url + "<STR_LIT>" % obj . id ) <EOL> rating_form = RatingForm ( request , obj , post_data ) <EOL> if rating_form . is_valid ( ) : <EOL> rating_form . save ( ) <EOL> if request . is_ajax ( ) : <EOL> obj = obj . __class__ . objects . get ( id = obj . id ) <EOL> rating_name = obj . get_ratingfield_name ( ) <EOL> json = { } <EOL> for f in ( "<STR_LIT>" , "<STR_LIT:count>" , "<STR_LIT>" ) : <EOL> json [ "<STR_LIT>" + f ] = getattr ( obj , "<STR_LIT>" % ( rating_name , f ) ) <EOL> response = HttpResponse ( dumps ( json ) ) <EOL> if rating_form . undoing : <EOL> ratings = set ( rating_form . previous ) ^ set ( [ rating_form . current ] ) <EOL> else : <EOL> ratings = rating_form . previous + [ rating_form . current ] <EOL> set_cookie ( response , "<STR_LIT>" , "<STR_LIT:U+002C>" . join ( ratings ) ) <EOL> return response </s>
