<s> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . http import Http404 , HttpResponse , HttpResponseRedirect <EOL> from django . contrib . auth . views import redirect_to_login <EOL> from django . template import loader , RequestContext <EOL> from django . utils import simplejson <EOL> from voting . models import Vote <EOL> VOTE_DIRECTIONS = ( ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , - <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> def vote_on_object ( request , model , direction , post_vote_redirect = None , <EOL> object_id = None , slug = None , slug_field = None , template_name = None , <EOL> template_loader = loader , extra_context = None , context_processors = None , <EOL> template_object_name = '<STR_LIT:object>' , allow_xmlhttprequest = False ) : <EOL> """<STR_LIT>""" <EOL> if ( allow_xmlhttprequest and <EOL> request . META . has_key ( '<STR_LIT>' ) and <EOL> request . META [ '<STR_LIT>' ] == '<STR_LIT>' ) : <EOL> return xmlhttprequest_vote_on_object ( request , model , direction , <EOL> object_id = object_id , slug = slug , <EOL> slug_field = slug_field ) <EOL> if extra_context is None : extra_context = { } <EOL> if not request . user . is_authenticated ( ) : <EOL> return redirect_to_login ( request . path ) <EOL> try : <EOL> vote = dict ( VOTE_DIRECTIONS ) [ direction ] <EOL> except KeyError : <EOL> raise AttributeError ( '<STR_LIT>' % vote_type ) <EOL> lookup_kwargs = { } <EOL> if object_id : <EOL> lookup_kwargs [ '<STR_LIT>' % model . _meta . pk . name ] = object_id <EOL> elif slug and slug_field : <EOL> lookup_kwargs [ '<STR_LIT>' % slug_field ] = slug <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> try : <EOL> obj = model . _default_manager . get ( ** lookup_kwargs ) <EOL> except ObjectDoesNotExist : <EOL> raise Http404 , '<STR_LIT>' % ( model . _meta . app_label , lookup_kwargs ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if post_vote_redirect is not None : <EOL> next = post_vote_redirect <EOL> elif request . REQUEST . has_key ( '<STR_LIT>' ) : <EOL> next = request . REQUEST [ '<STR_LIT>' ] <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> if callable ( getattr ( obj , '<STR_LIT>' ) ) : <EOL> next = obj . get_absolute_url ( ) <EOL> else : <EOL> next = obj . get_absolute_url <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> Vote . objects . record_vote ( obj , request . user , vote ) <EOL> return HttpResponseRedirect ( next ) <EOL> else : <EOL> if not template_name : <EOL> template_name = '<STR_LIT>' % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> template_object_name : obj , <EOL> '<STR_LIT>' : direction , <EOL> } , context_processors ) <EOL> for key , value in extra_context . items ( ) : <EOL> if callable ( value ) : <EOL> c [ key ] = value ( ) <EOL> else : <EOL> c [ key ] = value <EOL> response = HttpResponse ( t . render ( c ) ) <EOL> return response <EOL> def json_error_response ( error_message , * args , ** kwargs ) : <EOL> return HttpResponse ( simplejson . dumps ( dict ( success = False , <EOL> error_message = error_message ) ) ) <EOL> def xmlhttprequest_vote_on_object ( request , model , direction , <EOL> object_id = None , slug = None , slug_field = None ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return json_error_response ( '<STR_LIT>' ) <EOL> if not request . user . is_authenticated ( ) : <EOL> return json_error_response ( '<STR_LIT>' ) <EOL> try : <EOL> vote = dict ( VOTE_DIRECTIONS ) [ direction ] <EOL> except KeyError : <EOL> return json_error_response ( '<STR_LIT>' % direction ) <EOL> lookup_kwargs = { } <EOL> if object_id : <EOL> lookup_kwargs [ '<STR_LIT>' % model . _meta . pk . name ] = object_id <EOL> elif slug and slug_field : <EOL> lookup_kwargs [ '<STR_LIT>' % slug_field ] = slug <EOL> else : <EOL> return json_error_response ( '<STR_LIT>' ) <EOL> try : <EOL> obj = model . _default_manager . get ( ** lookup_kwargs ) <EOL> except ObjectDoesNotExist : <EOL> return json_error_response ( '<STR_LIT>' % ( model . _meta . verbose_name , lookup_kwargs ) ) <EOL> Vote . objects . record_vote ( obj , request . user , vote ) <EOL> return HttpResponse ( simplejson . dumps ( { '<STR_LIT:success>' : True , <EOL> '<STR_LIT>' : Vote . objects . get_score ( obj ) } ) ) </s>
