#NEWLINE# def set_rating( request , obj_type , obj_id , vote_type ) : #NEWLINE# #INDENT# error = '' #NEWLINE# if ( obj_type in RATINGS_CONFIG ) : #NEWLINE# #INDENT# model_name = RATINGS_CONFIG [ obj_type ] [ 'model' ] #NEWLINE# model = str_to_class ( model_name ) #NEWLINE# try : #NEWLINE# #INDENT# obj = model . objects . get ( pk = obj_id ) #NEWLINE# if ( obj . rating == None ) : #NEWLINE# #INDENT# rating = Rating . objects . create ( ) #NEWLINE# rating . save ( ) #NEWLINE# obj . rating = rating #NEWLINE# obj . save ( ) #NEWLINE# #UNINDENT# ( vote , is_new ) = RatingVote . objects . get_or_create ( user = request . user , rating = obj . rating ) #NEWLINE# if is_new : #NEWLINE# #INDENT# vote . vote_type = model #NEWLINE# vote . save ( ) #NEWLINE# if ( vote_type == VOTE_LIKE ) : #NEWLINE# #INDENT# obj . rating . likes += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# obj . rating . dislikes += 1 #NEWLINE# #UNINDENT# obj . rating . save ( ) #NEWLINE# tasks . task_compute_object_rating ( obj_type , obj ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# error = _ ( 'You already voted' ) #NEWLINE# #UNINDENT# #UNINDENT# except model . DoesNotExist : #NEWLINE# #INDENT# error = _ ( "You can't vote for this object" ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# error = _ ( "You can't vote for this object" ) #NEWLINE# #UNINDENT# if request . is_ajax ( ) : #NEWLINE# #INDENT# if error : #NEWLINE# #INDENT# return json_response ( { #NEWLINE# #INDENT# 'status' : 'error' , #NEWLINE# 'text' : error , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return json_response ( { #NEWLINE# #INDENT# 'status' : 'ok' , #NEWLINE# 'text' : _ ( 'Your vote successfully counted' ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# return redirect ( request . META . get ( 'HTTP_REFERER' , '/' ) )