def _get_external_id_relationships(cls, item): field_name_to_external_model_references = [ base_model_validators.ExternalModelFetcherDetails( 'feedback_thread_ids', feedback_models.GeneralFeedbackThreadModel, [item.id]) ] if user_services.is_user_id_valid(item.author_id): field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( 'author_ids', user_models.UserSettingsModel, [item.author_id])) if item.target_type in TARGET_TYPE_TO_TARGET_MODEL: field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( '%s_ids' % item.target_type, TARGET_TYPE_TO_TARGET_MODEL[item.target_type], [item.target_id])) if item.final_reviewer_id and user_services.is_user_id_valid( item.final_reviewer_id): # Bot rejects suggestions when the suggestion's targeted entity gets # removed from the topic. The bot doesn't have a UserSettingsModel # for their user_id. Exclude external model validation for bot. if item.final_reviewer_id != feconf.SUGGESTION_BOT_USER_ID: field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( 'reviewer_ids', user_models.UserSettingsModel, [item.final_reviewer_id])) return field_name_to_external_model_references
def _get_external_id_relationships(cls, item): field_name_to_external_model_references = [] if user_services.is_user_id_valid(item.author_id): field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( 'author_ids', user_models.UserSettingsModel, [item.author_id] ) ) if item.target_type in TARGET_TYPE_TO_TARGET_MODEL: field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( '%s_ids' % item.target_type, TARGET_TYPE_TO_TARGET_MODEL[item.target_type], [item.target_id])) if ( item.final_reviewer_id and user_services.is_user_id_valid(item.final_reviewer_id) ): field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( 'final_reviewer_ids', user_models.UserSettingsModel, [item.final_reviewer_id])) return field_name_to_external_model_references
def _get_external_id_relationships(cls, item): field_name_to_external_model_references = [ base_model_validators.ExternalModelFetcherDetails( 'message_ids', feedback_models.GeneralFeedbackMessageModel, [ '%s.%s' % (item.id, i) for i in python_utils.RANGE(item.message_count) ]) ] if (item.original_author_id and user_services.is_user_id_valid(item.original_author_id)): field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( 'author_ids', user_models.UserSettingsModel, [item.original_author_id])) if item.has_suggestion: field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( 'suggestion_ids', suggestion_models.GeneralSuggestionModel, [item.id])) if item.entity_type in TARGET_TYPE_TO_TARGET_MODEL: field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( '%s_ids' % item.entity_type, TARGET_TYPE_TO_TARGET_MODEL[item.entity_type], [item.entity_id])) if (item.last_nonempty_message_author_id and user_services.is_user_id_valid( item.last_nonempty_message_author_id)): field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( 'last_nonempty_message_author_ids', user_models.UserSettingsModel, [item.last_nonempty_message_author_id])) return field_name_to_external_model_references
def _migrate_user_id(snapshot_model): """Fix the assignee_id in commit_cmds in snapshot metadata and commit log models. This is only run on models that have commit_cmds of length two. This is stuff that was missed in the user ID migration. Args: snapshot_model: BaseSnapshotMetadataModel. Snapshot metadata model to migrate. Returns: (str, str). Result info, first part is result message, second is additional info like IDs. """ # Only commit_cmds of length 2 are processed by this method. assert len(snapshot_model.commit_cmds) == 2 new_user_ids = [None, None] for i, commit_cmd in enumerate(snapshot_model.commit_cmds): assignee_id = commit_cmd['assignee_id'] if (commit_cmd['cmd'] == rights_domain.CMD_CHANGE_ROLE and not user_services.is_user_id_valid(assignee_id)): user_settings = user_services.get_user_settings_by_gae_id( assignee_id) if user_settings is None: return ('MIGRATION_FAILURE', (snapshot_model.id, assignee_id)) new_user_ids[i] = user_settings.user_id # This loop is used for setting the actual commit_cmds and is separate # because if the second commit results in MIGRATION_FAILURE we do not # want to set the first one either. We want to either set the correct # user IDs in both commits or we don't want to set it at all. for i in python_utils.RANGE(len(snapshot_model.commit_cmds)): if new_user_ids[i] is not None: snapshot_model.commit_cmds[i]['assignee_id'] = new_user_ids[i] commit_log_model = ( exp_models.ExplorationCommitLogEntryModel.get_by_id( 'rights-%s-%s' % (snapshot_model.get_unversioned_instance_id(), snapshot_model.get_version_string()))) if commit_log_model is None: snapshot_model.put(update_last_updated_time=False) return ('MIGRATION_SUCCESS_MISSING_COMMIT_LOG', snapshot_model.id) commit_log_model.commit_cmds = snapshot_model.commit_cmds def _put_both_models(): """Put both models into the datastore together.""" snapshot_model.put(update_last_updated_time=False) commit_log_model.put(update_last_updated_time=False) transaction_services.run_in_transaction(_put_both_models) return ('MIGRATION_SUCCESS', snapshot_model.id)
def _get_external_id_relationships(cls, item): field_name_to_external_model_references = [ base_model_validators.ExternalModelFetcherDetails( 'feedback_thread_ids', feedback_models.GeneralFeedbackThreadModel, [item.thread_id]) ] if (item.author_id and user_services.is_user_id_valid(item.author_id)): field_name_to_external_model_references.append( base_model_validators.ExternalModelFetcherDetails( 'author_ids', user_models.UserSettingsModel, [item.author_id])) return field_name_to_external_model_references
def validate(self): """Validates the BaseSuggestion object. Each subclass must implement this function. The subclasses must validate the change and score_category fields. Raises: ValidationError. One or more attributes of the BaseSuggestion object are invalid. """ if (self.suggestion_type not in suggestion_models.SUGGESTION_TYPE_CHOICES): raise utils.ValidationError( 'Expected suggestion_type to be among allowed choices, ' 'received %s' % self.suggestion_type) if self.target_type not in suggestion_models.TARGET_TYPE_CHOICES: raise utils.ValidationError( 'Expected target_type to be among allowed choices, ' 'received %s' % self.target_type) if not isinstance(self.target_id, python_utils.BASESTRING): raise utils.ValidationError( 'Expected target_id to be a string, received %s' % type(self.target_id)) if not isinstance(self.target_version_at_submission, int): raise utils.ValidationError( 'Expected target_version_at_submission to be an int, ' 'received %s' % type(self.target_version_at_submission)) if self.status not in suggestion_models.STATUS_CHOICES: raise utils.ValidationError( 'Expected status to be among allowed choices, ' 'received %s' % self.status) if not isinstance(self.author_id, python_utils.BASESTRING): raise utils.ValidationError( 'Expected author_id to be a string, received %s' % type(self.author_id)) if (self.author_id is not None and not user_services.is_user_id_valid(self.author_id)): raise utils.ValidationError( 'Expected author_id to be in a valid user ID format, ' 'received %s' % self.author_id) if self.final_reviewer_id is not None: if not isinstance(self.final_reviewer_id, python_utils.BASESTRING): raise utils.ValidationError( 'Expected final_reviewer_id to be a string, received %s' % type(self.final_reviewer_id)) if (not user_services.is_user_id_valid(self.final_reviewer_id) and self.final_reviewer_id != feconf.SUGGESTION_BOT_USER_ID): raise utils.ValidationError( 'Expected final_reviewer_id to be in a valid user ID ' 'format, received %s' % self.final_reviewer_id) if not isinstance(self.score_category, python_utils.BASESTRING): raise utils.ValidationError( 'Expected score_category to be a string, received %s' % type(self.score_category)) if (suggestion_models.SCORE_CATEGORY_DELIMITER not in self.score_category): raise utils.ValidationError( 'Expected score_category to be of the form' ' score_type%sscore_sub_type, received %s' % (suggestion_models.SCORE_CATEGORY_DELIMITER, self.score_category)) if (len( self.score_category.split( suggestion_models.SCORE_CATEGORY_DELIMITER))) != 2: raise utils.ValidationError( 'Expected score_category to be of the form' ' score_type%sscore_sub_type, received %s' % (suggestion_models.SCORE_CATEGORY_DELIMITER, self.score_category)) if self.get_score_type() not in suggestion_models.SCORE_TYPE_CHOICES: raise utils.ValidationError( 'Expected the first part of score_category to be among allowed' ' choices, received %s' % self.get_score_type())