<s> """<STR_LIT>""" <EOL> from django . http import HttpResponse , HttpResponseRedirect , Http404 <EOL> from django . utils import simplejson <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . views . decorators . cache import never_cache <EOL> from critic . models import RatingData <EOL> from critic . utils import render <EOL> POST_ADD_FIELDS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ login_required <EOL> @ never_cache <EOL> def add_rating ( request , redirect_field_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not request . method == '<STR_LIT:POST>' : <EOL> raise Http404 <EOL> if request . is_ajax ( ) : <EOL> response = HttpResponse ( ) <EOL> else : <EOL> next_url = request . GET . get ( redirect_field_name , '<STR_LIT:/>' ) <EOL> response = HttpResponseRedirect ( next_url ) <EOL> data_dict = request . POST <EOL> try : <EOL> ct_id = int ( data_dict . get ( '<STR_LIT>' ) ) <EOL> obj_id = int ( data_dict . get ( '<STR_LIT>' ) ) <EOL> option = int ( data_dict . get ( '<STR_LIT>' ) ) <EOL> except Exception : <EOL> raise Http404 <EOL> ctype = get_object_or_404 ( ContentType , pk = ct_id ) <EOL> obj = get_object_or_404 ( ctype . model_class ( ) , pk = obj_id ) <EOL> RatingData . objects . add ( obj , request . user , option ) <EOL> return response <EOL> def render_rating ( request , content_type_id , object_id ) : <EOL> """<STR_LIT>""" <EOL> ctype = get_object_or_404 ( ContentType , pk = content_type_id ) <EOL> obj = get_object_or_404 ( ctype . model_class ( ) , pk = object_id ) <EOL> user_rating = None <EOL> if request . user . is_authenticated ( ) : <EOL> user_rating = RatingData . objects . user_rating ( obj , request . user ) <EOL> rendered_content = render ( obj , ** { '<STR_LIT>' : user_rating } ) <EOL> return HttpResponse ( rendered_content ) <EOL> def user_rating_json ( request , content_type_id , object_id ) : <EOL> """<STR_LIT>""" <EOL> ctype = get_object_or_404 ( ContentType , pk = content_type_id ) <EOL> obj = get_object_or_404 ( ctype . model_class ( ) , pk = object_id ) <EOL> if request . user . is_authenticated ( ) : <EOL> user_rating = RatingData . objects . user_rating ( obj , request . user ) <EOL> else : <EOL> user_rating = None <EOL> json = simplejson . dumps ( { '<STR_LIT>' : user_rating } ) <EOL> return HttpResponse ( json , mimetype = '<STR_LIT:application/json>' ) <EOL> def rating_data_json ( request , content_type_id , object_id , option = None ) : <EOL> """<STR_LIT>""" <EOL> ctype = get_object_or_404 ( ContentType , pk = content_type_id ) <EOL> obj = get_object_or_404 ( ctype . model_class ( ) , pk = object_id ) <EOL> if option : <EOL> option = int ( option ) <EOL> average = RatingData . objects . average ( obj ) or <NUM_LIT:0> <EOL> total = RatingData . objects . total ( obj , option ) or <NUM_LIT:0> <EOL> json = simplejson . dumps ( { '<STR_LIT>' : average , '<STR_LIT>' : total , <EOL> '<STR_LIT>' : int ( round ( float ( average ) ) ) } ) <EOL> return HttpResponse ( json , mimetype = '<STR_LIT:application/json>' ) </s>
