<s> """<STR_LIT>""" <EOL> from core . domain import feedback_domain <EOL> from core . domain import feedback_jobs_continuous <EOL> from core . domain import subscription_services <EOL> from core . platform import models <EOL> import feconf <EOL> ( feedback_models , ) = models . Registry . import_models ( [ models . NAMES . feedback ] ) <EOL> DEFAULT_SUGGESTION_THREAD_SUBJECT = '<STR_LIT>' <EOL> DEFAULT_SUGGESTION_THREAD_INITIAL_MESSAGE = '<STR_LIT>' <EOL> def _create_models_for_thread_and_first_message ( <EOL> exploration_id , state_name , original_author_id , subject , text , <EOL> has_suggestion ) : <EOL> """<STR_LIT>""" <EOL> thread_id = feedback_models . FeedbackThreadModel . generate_new_thread_id ( <EOL> exploration_id ) <EOL> thread = feedback_models . FeedbackThreadModel . create ( <EOL> exploration_id , thread_id ) <EOL> thread . exploration_id = exploration_id <EOL> thread . state_name = state_name <EOL> thread . original_author_id = original_author_id <EOL> thread . status = feedback_models . STATUS_CHOICES_OPEN <EOL> thread . subject = subject <EOL> thread . has_suggestion = has_suggestion <EOL> thread . put ( ) <EOL> create_message ( <EOL> exploration_id , thread_id , original_author_id , <EOL> feedback_models . STATUS_CHOICES_OPEN , subject , text ) <EOL> return thread_id <EOL> def create_thread ( <EOL> exploration_id , state_name , original_author_id , subject , text ) : <EOL> """<STR_LIT>""" <EOL> _create_models_for_thread_and_first_message ( <EOL> exploration_id , state_name , original_author_id , subject , text , False ) <EOL> def create_message ( <EOL> exploration_id , thread_id , author_id , updated_status , updated_subject , <EOL> text ) : <EOL> """<STR_LIT>""" <EOL> from core . domain import event_services <EOL> full_thread_id = ( <EOL> feedback_models . FeedbackThreadModel . generate_full_thread_id ( <EOL> exploration_id , thread_id ) ) <EOL> thread = feedback_models . FeedbackThreadModel . get ( full_thread_id ) <EOL> message_id = feedback_models . FeedbackMessageModel . get_message_count ( <EOL> exploration_id , thread_id ) <EOL> msg = feedback_models . FeedbackMessageModel . create ( <EOL> exploration_id , thread_id , message_id ) <EOL> msg . thread_id = full_thread_id <EOL> msg . message_id = message_id <EOL> msg . author_id = author_id <EOL> if updated_status : <EOL> if message_id == <NUM_LIT:0> : <EOL> event_services . FeedbackThreadCreatedEventHandler . record ( <EOL> thread . exploration_id ) <EOL> else : <EOL> event_services . FeedbackThreadStatusChangedEventHandler . record ( <EOL> thread . exploration_id , thread . status , updated_status ) <EOL> msg . updated_status = updated_status <EOL> if updated_subject : <EOL> msg . updated_subject = updated_subject <EOL> msg . text = text <EOL> msg . put ( ) <EOL> if message_id != <NUM_LIT:0> and ( updated_status or updated_subject ) : <EOL> if updated_status and updated_status != thread . status : <EOL> thread . status = updated_status <EOL> if updated_subject and updated_subject != thread . subject : <EOL> thread . subject = updated_subject <EOL> thread . put ( ) <EOL> if author_id : <EOL> subscription_services . subscribe_to_thread ( author_id , full_thread_id ) <EOL> return True <EOL> def _get_message_from_model ( message_model ) : <EOL> return feedback_domain . FeedbackMessage ( <EOL> message_model . id , message_model . thread_id , message_model . message_id , <EOL> message_model . author_id , message_model . updated_status , <EOL> message_model . updated_subject , message_model . text , <EOL> message_model . created_on , message_model . last_updated ) <EOL> def get_messages ( exploration_id , thread_id ) : <EOL> return [ <EOL> _get_message_from_model ( m ) <EOL> for m in feedback_models . FeedbackMessageModel . get_messages ( <EOL> exploration_id , thread_id ) ] <EOL> def get_next_page_of_all_feedback_messages ( <EOL> page_size = feconf . FEEDBACK_TAB_PAGE_SIZE , urlsafe_start_cursor = None ) : <EOL> """<STR_LIT>""" <EOL> results , new_urlsafe_start_cursor , more = ( <EOL> feedback_models . FeedbackMessageModel . get_all_messages ( <EOL> page_size , urlsafe_start_cursor ) ) <EOL> result_messages = [ _get_message_from_model ( m ) for m in results ] <EOL> return ( result_messages , new_urlsafe_start_cursor , more ) <EOL> def get_thread_analytics ( exploration_id ) : <EOL> """<STR_LIT>""" <EOL> return feedback_jobs_continuous . FeedbackAnalyticsAggregator . get_thread_analytics ( <EOL> exploration_id ) <EOL> def create_suggestion ( exploration_id , author_id , exploration_version , <EOL> state_name , description , suggestion_content ) : <EOL> """<STR_LIT>""" <EOL> thread_id = _create_models_for_thread_and_first_message ( <EOL> exploration_id , state_name , author_id , description , <EOL> DEFAULT_SUGGESTION_THREAD_INITIAL_MESSAGE , True ) <EOL> feedback_models . SuggestionModel . create ( <EOL> exploration_id , thread_id , author_id , exploration_version , state_name , <EOL> description , suggestion_content ) <EOL> full_thread_id = ( <EOL> feedback_models . FeedbackThreadModel . generate_full_thread_id ( <EOL> exploration_id , thread_id ) ) <EOL> subscription_services . subscribe_to_thread ( author_id , full_thread_id ) <EOL> def _get_suggestion_from_model ( suggestion_model ) : <EOL> return feedback_domain . Suggestion ( <EOL> suggestion_model . id , suggestion_model . author_id , <EOL> suggestion_model . exploration_id , suggestion_model . exploration_version , <EOL> suggestion_model . state_name , suggestion_model . description , <EOL> suggestion_model . state_content ) <EOL> def get_suggestion ( exploration_id , thread_id ) : <EOL> model = feedback_models . SuggestionModel . get_by_exploration_and_thread_id ( <EOL> exploration_id , thread_id ) <EOL> return _get_suggestion_from_model ( model ) if model else None <EOL> def _get_thread_from_model ( thread_model ) : <EOL> return feedback_domain . FeedbackThread ( <EOL> thread_model . id , thread_model . exploration_id , thread_model . state_name , <EOL> thread_model . original_author_id , thread_model . status , <EOL> thread_model . subject , thread_model . summary , thread_model . has_suggestion , <EOL> thread_model . created_on , thread_model . last_updated ) <EOL> def get_threads ( exploration_id ) : <EOL> thread_models = feedback_models . FeedbackThreadModel . get_threads ( <EOL> exploration_id ) <EOL> return [ _get_thread_from_model ( model ) for model in thread_models ] <EOL> def get_open_threads ( exploration_id , has_suggestion ) : <EOL> """<STR_LIT>""" <EOL> threads = get_threads ( exploration_id ) <EOL> open_threads = [ ] <EOL> for thread in threads : <EOL> if ( thread . has_suggestion == has_suggestion and <EOL> thread . status == feedback_models . STATUS_CHOICES_OPEN ) : <EOL> open_threads . append ( thread ) <EOL> return open_threads <EOL> def get_closed_threads ( exploration_id , has_suggestion ) : <EOL> """<STR_LIT>""" <EOL> threads = get_threads ( exploration_id ) <EOL> closed_threads = [ ] <EOL> for thread in threads : <EOL> if ( thread . has_suggestion == has_suggestion and <EOL> thread . status != feedback_models . STATUS_CHOICES_OPEN ) : <EOL> closed_threads . append ( thread ) <EOL> return closed_threads <EOL> def get_all_threads ( exploration_id , has_suggestion ) : <EOL> """<STR_LIT>""" <EOL> threads = get_threads ( exploration_id ) <EOL> all_threads = [ ] <EOL> for thread in threads : <EOL> if thread . has_suggestion == has_suggestion : <EOL> all_threads . append ( thread ) <EOL> return all_threads </s>
