def test_is_pseudonymous_id(self): # type: () -> None self.assertTrue(utils.is_pseudonymous_id('pid_' + 'a' * 32)) self.assertFalse(utils.is_pseudonymous_id('uid_' + 'a' * 32)) self.assertFalse(utils.is_pseudonymous_id('uid_' + 'a' * 31 + 'A')) self.assertFalse(utils.is_pseudonymous_id('uid_' + 'a' * 31)) self.assertFalse(utils.is_pseudonymous_id('a' * 36))
def __init__( self, field_name, model_ids, may_contain_system_ids, may_contain_pseudonymous_ids): """Initializes the UserSettingsModelFetcherDetails domain object. Args: field_name: str. A specific name used as an identifier by the storage model which is used to identify the user settings model reference. For example: `'committer_id': UserSettingsModel` means that committer_id is a field which contains a user_id used to identify the external model UserSettingsModel. model_ids: list(str). The list of user settings model IDs for which to fetch the UserSettingsModels. may_contain_system_ids: bool. Whether the model IDs contain system IDs which should be omitted before attempting to fetch the corresponding models. Set may_contain_system_ids to True if and only if this field can contain admin or bot IDs. may_contain_pseudonymous_ids: bool. Whether the model ids contain pseudonymous IDs which should be omitted before attempting to fetch the corresponding models. Set may_contain_pseudonymous_ids to True if and only if this field can contain user IDs that are pseudonymized as part of Wipeout. In other words, these fields can only be in models that have LOCALLY_PSEUDONYMIZE as their DELETION_POLICY. """ filtered_model_ids = model_ids if may_contain_system_ids: filtered_model_ids = list( set(filtered_model_ids) - set(feconf.SYSTEM_USERS.values())) else: if set(filtered_model_ids) & set(feconf.SYSTEM_USERS.values()): raise utils.ValidationError( 'The field \'%s\' should not contain ' 'system IDs' % field_name) if may_contain_pseudonymous_ids: filtered_model_ids = [ model_id for model_id in filtered_model_ids if not utils.is_pseudonymous_id(model_id) ] else: if any( utils.is_pseudonymous_id(model_id) for model_id in filtered_model_ids): raise utils.ValidationError( 'The field \'%s\' should not contain ' 'pseudonymous IDs' % field_name) self.field_name = field_name self.model_class = user_models.UserSettingsModel self.model_ids = filtered_model_ids
def convert_to_valid_dict(model_dict): """Replace invalid fields and values in the ExplorationRightsModel dict. Some old ExplorationRightsSnapshotContentModels can contain fields and field values that are no longer supported and would cause an exception when we try to reconstitute a ExplorationRightsModel from them. We need to remove or replace these fields and values. Args: model_dict: dict. The content of the model. Some fields and field values might no longer exist in the ExplorationRightsModel schema. Returns: dict. The content of the model. Only valid fields and values are present. """ # The status field could historically take the value 'publicized', this # value is now equivalent to 'public'. if model_dict['status'] == 'publicized': model_dict['status'] = constants.ACTIVITY_STATUS_PUBLIC # The voice_artist_ids field was previously named translator_ids. We # need to move the values from translator_ids field to voice_artist_ids # and delete translator_ids. if 'translator_ids' in model_dict and model_dict['translator_ids']: model_dict['voice_artist_ids'] = model_dict['translator_ids'] model_dict['translator_ids'] = [] # The all_viewer_ids field was previously used in some versions of the # model, we need to remove it. if 'all_viewer_ids' in model_dict: del model_dict['all_viewer_ids'] if 'translator_ids' in model_dict: del model_dict['translator_ids'] # We need to remove pseudonymous IDs from all the fields that contain # user IDs. for field_name in ('owner_ids', 'editor_ids', 'voice_artist_ids', 'viewer_ids'): model_dict[field_name] = [ user_id for user_id in model_dict[field_name] if not utils.is_pseudonymous_id(user_id) ] return model_dict