<s> import cgi <EOL> import logging <EOL> import urlparse <EOL> from django import http <EOL> from django import template <EOL> from django . conf import settings <EOL> from django . template import loader <EOL> from common import api <EOL> from common import exception <EOL> from common import messages <EOL> from common import util <EOL> from common import validate <EOL> def common_confirm ( request ) : <EOL> message = request . REQUEST [ '<STR_LIT:message>' ] <EOL> redirect_to = request . REQUEST [ '<STR_LIT>' ] <EOL> try : <EOL> validate . nonce ( request , message + redirect_to ) <EOL> parts = urlparse . urlparse ( redirect_to ) <EOL> action_url = parts [ <NUM_LIT:2> ] <EOL> query_dict = cgi . parse_qs ( parts [ <NUM_LIT:4> ] , keep_blank_values = True ) <EOL> query_dict = dict ( [ ( k , v [ <NUM_LIT:0> ] ) for k , v in query_dict . iteritems ( ) ] ) <EOL> except : <EOL> message = None <EOL> exception . handle_exception ( request ) <EOL> c = template . RequestContext ( request , locals ( ) ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> return http . HttpResponse ( t . render ( c ) ) <EOL> def common_logme ( request ) : <EOL> logging . info ( "<STR_LIT>" , request ) <EOL> raise http . Http404 ( ) <EOL> def common_404 ( request , template_name = '<STR_LIT>' ) : <EOL> t = loader . get_template ( template_name ) <EOL> return http . HttpResponseNotFound ( <EOL> t . render ( template . RequestContext ( request , { '<STR_LIT>' : request . path } ) ) ) <EOL> def common_500 ( request , template_name = '<STR_LIT>' ) : <EOL> logging . error ( "<STR_LIT>" , str ( request ) ) <EOL> t = loader . get_template ( template_name ) <EOL> return http . HttpResponse ( t . render ( template . RequestContext ( request , { } ) ) ) <EOL> def common_error ( request ) : <EOL> message = request . REQUEST [ '<STR_LIT:error>' ] <EOL> try : <EOL> validate . error_nonce ( request , message ) <EOL> except : <EOL> exception . handle_exception ( request ) <EOL> message = "<STR_LIT>" <EOL> c = template . RequestContext ( request , locals ( ) ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> return http . HttpResponse ( t . render ( c ) ) <EOL> def common_noslash ( request , path = "<STR_LIT>" ) : <EOL> return http . HttpResponseRedirect ( "<STR_LIT:/>" + path ) <EOL> def common_photo_upload ( request , success = "<STR_LIT:/>" , nick = None ) : <EOL> if not nick : <EOL> nick = request . user . nick <EOL> if request . FILES : <EOL> try : <EOL> validate . nonce ( request , '<STR_LIT>' ) <EOL> img = request . FILES . get ( '<STR_LIT>' ) <EOL> if not img : <EOL> raise exception . ValidationError ( '<STR_LIT>' ) <EOL> validate . avatar_photo_size ( img ) <EOL> img_url = api . avatar_upload ( request . user , <EOL> nick , <EOL> img . read ( ) ) <EOL> api . avatar_set_actor ( request . user , nick , img_url ) <EOL> return util . RedirectFlash ( success , "<STR_LIT>" ) <EOL> except : <EOL> exception . handle_exception ( request ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> try : <EOL> validate . nonce ( request , '<STR_LIT>' ) <EOL> avatar_path = request . POST . get ( '<STR_LIT>' ) <EOL> if not avatar_path : <EOL> raise exception . ValidationError ( '<STR_LIT>' ) <EOL> rv = api . avatar_set_actor ( request . user , nick , avatar_path ) <EOL> if not rv : <EOL> raise exception . ValidationError ( '<STR_LIT>' ) <EOL> return util . RedirectFlash ( success , "<STR_LIT>" ) <EOL> except : <EOL> exception . handle_exception ( request ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> try : <EOL> validate . nonce ( request , '<STR_LIT>' ) <EOL> validate . confirm_dangerous ( request , '<STR_LIT>' ) <EOL> rv = api . avatar_clear_actor ( request . user , nick ) <EOL> return util . RedirectFlash ( success , "<STR_LIT>" ) <EOL> except : <EOL> exception . handle_exception ( request ) <EOL> def call_api_from_request ( request , api_call ) : <EOL> """<STR_LIT>""" <EOL> for request_dict in ( request . POST , request . GET ) : <EOL> if api_call in request_dict : <EOL> logging . info ( "<STR_LIT>" , str ( api_call ) ) <EOL> call = getattr ( api , api_call ) <EOL> try : <EOL> validate . nonce ( request , api_call ) <EOL> confirm_msg = messages . confirmation ( api_call ) <EOL> if not confirm_msg is None : <EOL> validate . confirm_dangerous ( <EOL> request , messages . confirmation ( api_call ) ) <EOL> kwparams = util . query_dict_to_keywords ( request_dict ) <EOL> logging . info ( "<STR_LIT>" , str ( kwparams ) ) <EOL> if '<STR_LIT>' in kwparams : <EOL> del kwparams [ '<STR_LIT>' ] <EOL> first_param = kwparams . pop ( api_call , '<STR_LIT>' ) <EOL> params = list ( ) <EOL> if len ( first_param ) : <EOL> params = ( first_param , ) <EOL> validate . nonce ( request , api_call ) <EOL> kwparams . pop ( '<STR_LIT>' ) <EOL> kwparams . pop ( '<STR_LIT>' , None ) <EOL> kwparams . pop ( '<STR_LIT>' , None ) <EOL> logging . info ( "<STR_LIT>" , str ( ( params , kwparams ) ) ) <EOL> return ( True , call ( request . user , * params , ** kwparams ) ) <EOL> except : <EOL> exception . handle_exception ( request ) <EOL> return ( False , None ) <EOL> def handle_view_action ( request , actions ) : <EOL> """<STR_LIT>""" <EOL> for action in actions . keys ( ) : <EOL> called , ret = call_api_from_request ( request , action ) <EOL> if called : <EOL> redirect = actions [ action ] <EOL> return util . RedirectFlash ( redirect , messages . flash ( action ) ) <EOL> return None <EOL> def common_design_update ( request , nick ) : <EOL> view = api . actor_get ( api . ROOT , nick ) <EOL> if request . POST : <EOL> try : <EOL> validate . nonce ( request , '<STR_LIT>' ) <EOL> color = request . POST . get ( '<STR_LIT>' ) <EOL> repeat = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not repeat : <EOL> repeat = '<STR_LIT>' <EOL> img = request . FILES . get ( '<STR_LIT>' ) <EOL> img_url = None <EOL> if img : <EOL> img_url = api . background_upload ( request . user , <EOL> nick , <EOL> img . read ( ) ) <EOL> api . background_set_actor ( request . user , <EOL> nick , <EOL> img_url , <EOL> color , <EOL> repeat ) <EOL> return util . RedirectFlash ( view . url ( ) + '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> except : <EOL> exception . handle_exception ( request ) <EOL> if request . GET and '<STR_LIT>' in request . GET : <EOL> api . background_clear_actor ( request . user , nick ) <EOL> return util . RedirectFlash ( view . url ( ) + '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> return None </s>
