#NEWLINE# def assign_rating_to_exploration( user_id , exploration_id , new_rating ) : #NEWLINE# #INDENT# "Records the rating awarded by the user to the exploration in both the\n    user-specific data and exploration summary.\n\n    It validates the exploration id but not the user id.\n\n     - 'new_rating' should be an integer between 1 and 5\n    " #NEWLINE# if ( not isinstance ( exploration_id , int ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Expected the rating to be an integer, received %s' % new_rating ) ) #NEWLINE# #UNINDENT# if ( new_rating not in ALLOWED_RATINGS ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Expected a rating 1-5, received %s.' % new_rating ) ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# exp_services . get_exploration_by_id ( exploration_id ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# raise Exception ( ( 'Invalid exploration id %s' % exploration_id ) ) #NEWLINE# #UNINDENT# def _update_user_rating( ) : #NEWLINE# #INDENT# exp_user_data_model = user_models . ExplorationUserDataModel . get ( user_id , exploration_id ) #NEWLINE# if exp_user_data_model : #NEWLINE# #INDENT# old_rating = exp_user_data_model . rating #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# old_rating = None #NEWLINE# exp_user_data_model = user_models . ExplorationUserDataModel . create ( user_id , exploration_id ) #NEWLINE# #UNINDENT# exp_user_data_model . rating = new_rating #NEWLINE# exp_user_data_model . rated_on = datetime . datetime . utcnow ( ) #NEWLINE# exp_user_data_model . put ( ) #NEWLINE# return old_rating #NEWLINE# #UNINDENT# old_rating = transaction_services . run_in_transaction ( _update_user_rating ) #NEWLINE# exploration_summary = exp_services . get_exploration_summary_by_id ( exploration_id ) #NEWLINE# if ( not exploration_summary . ratings ) : #NEWLINE# #INDENT# exploration_summary . ratings = feconf . get_empty_ratings ( ) #NEWLINE# #UNINDENT# exploration_summary . ratings [ str ( new_rating ) ] += 1 #NEWLINE# if old_rating : #NEWLINE# #INDENT# exploration_summary . ratings [ str ( old_rating ) ] -= 1 #NEWLINE# #UNINDENT# exp_services . save_exploration_summary ( exploration_summary )