def test_convert_to_hash(self): """Test convert_to_hash() method.""" orig_string = 'name_to_convert' full_hash = utils.convert_to_hash(orig_string, 28) abbreviated_hash = utils.convert_to_hash(orig_string, 5) self.assertEqual(len(full_hash), 28) self.assertEqual(len(abbreviated_hash), 5) self.assertEqual(full_hash[:5], abbreviated_hash)
def _generate_id(cls, platform: str, submitted_on_datetime: datetime.datetime) -> str: """Generates key for the instance of AppFeedbackReportModel class in the required format with the arguments provided. Args: platform: str. The platform the user is the report from. submitted_on_datetime: datetime.datetime. The datetime that the report was submitted on in UTC. Returns: str. The generated ID for this entity using platform, submitted_on_sec, and a random string, of the form '[platform].[submitted_on_msec].[random hash]'. """ submitted_datetime_in_msec = utils.get_time_in_millisecs( submitted_on_datetime) for _ in python_utils.RANGE(base_models.MAX_RETRIES): random_hash = utils.convert_to_hash( python_utils.UNICODE( utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH) new_id = '%s.%s.%s' % (platform, int(submitted_datetime_in_msec), random_hash) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for AppFeedbackReportModel is producing too ' 'many collisions.')
def get_new_id(cls, entity_name): """Gets a new id for an entity, based on its name. The returned id is guaranteed to be unique among all instances of this entity. Args: entity_name: The name of the entity. Coerced to a utf-8 encoded string. Defaults to ''. Returns: str. New unique id for this entity class. Raises: Exception: An ID cannot be generated within a reasonable number of attempts. """ try: entity_name = unicode(entity_name).encode(encoding='utf-8') except Exception: entity_name = '' for _ in range(MAX_RETRIES): new_id = utils.convert_to_hash( '%s%s' % (entity_name, utils.get_random_int(RAND_RANGE)), ID_LENGTH) if not cls.get_by_id(new_id): return new_id raise Exception('New id generator is producing too many collisions.')
def _generate_id(cls, exp_id): """Generates a unique id for the training job of the form '[exp_id].[random hash of 16 chars]'. Args: exp_id: str. ID of the exploration. Returns: str. ID of the new ClassifierTrainingJobModel instance. Raises: Exception. The id generator for ClassifierTrainingJobModel is producing too many collisions. """ for _ in python_utils.RANGE(base_models.MAX_RETRIES): new_id = '%s.%s' % (exp_id, utils.convert_to_hash( python_utils.UNICODE( utils.get_random_int( base_models.RAND_RANGE)), base_models.ID_LENGTH)) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for ClassifierTrainingJobModel is producing ' 'too many collisions.')
def _generate_id(cls, intent): """Generates an ID for a new SentEmailModel instance. Args: intent: str. The intent string, i.e. the purpose of the email. Valid intent strings are defined in feconf.py. Returns: str. The newly-generated ID for the SentEmailModel instance. Raises: Exception. The id generator for SentEmailModel is producing too many collisions. """ id_prefix = '%s.' % intent for _ in python_utils.RANGE(base_models.MAX_RETRIES): new_id = '%s.%s' % (id_prefix, utils.convert_to_hash( python_utils.UNICODE( utils.get_random_int( base_models.RAND_RANGE)), base_models.ID_LENGTH)) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for SentEmailModel is producing too many ' 'collisions.')
def _get_new_id(cls, collection_id): """Generates a unique ID for the question of the form {{collection_id}}.{{random_hash_of_16_chars}} Args: collection_id: str. The ID of collection containing the question. Returns: new_id: int. ID of the new QuestionModel instance. Raises: Exception: The ID generator for QuestionModel is producing too many collisions. """ for _ in range(base_models.MAX_RETRIES): new_id = '%s.%s' % ( collection_id, utils.convert_to_hash( str(utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH)) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for QuestionModel is producing too many ' 'collisions.')
def _generate_id(cls, intent): """Generates an ID for a new SentEmailModel instance. Args: intent: str. The intent string, i.e. the purpose of the email. Valid intent strings are defined in feconf.py. Returns: str. The newly-generated ID for the SentEmailModel instance. Raises: Exception: The id generator for SentEmailModel is producing too many collisions. """ id_prefix = '%s.' % intent for _ in range(base_models.MAX_RETRIES): new_id = '%s.%s' % ( id_prefix, utils.convert_to_hash( str(utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH)) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for SentEmailModel is producing too many ' 'collisions.')
def _generate_id(cls, exp_id): """Generates a unique id for the training job of the form {{exp_id}}.{{random_hash_of_16_chars}} Args: exp_id: str. ID of the exploration. Returns: ID of the new ClassifierTrainingJobModel instance. Raises: Exception: The id generator for ClassifierTrainingJobModel is producing too many collisions. """ for _ in range(base_models.MAX_RETRIES): new_id = '%s.%s' % ( exp_id, utils.convert_to_hash( str(utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH)) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for ClassifierTrainingJobModel is producing ' 'too many collisions.')
def get_new_id(cls, entity_name): """Gets a new id for an entity, based on its name. The returned id is guaranteed to be unique among all instances of this entity. Args: entity_name: the name of the entity. Coerced to a utf-8 encoded string. Defaults to ''. Returns: str: a new unique id for this entity class. Raises: - Exception: if an id cannot be generated within a reasonable number of attempts. """ try: entity_name = unicode(entity_name).encode('utf-8') except Exception: entity_name = '' MAX_RETRIES = 10 RAND_RANGE = 127 * 127 ID_LENGTH = 12 for i in range(MAX_RETRIES): new_id = utils.convert_to_hash( '%s%s' % (entity_name, utils.get_random_int(RAND_RANGE)), ID_LENGTH) if not cls.get_by_id(new_id): return new_id raise Exception('New id generator is producing too many collisions.')
def _generate_id(cls, exp_id): """Generates a unique id for the classifier model of the form {{exp_id}}.{{random_hash_of_16_chars}} Args: exp_id: str. ID of the exploration. Returns: ID of the new classifier model. Raises: Exception: The id generator for ClassifierModel is producing too many collisions. """ for _ in range(base_models.MAX_RETRIES): new_id = '%s.%s' % ( exp_id, utils.convert_to_hash( str(utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH)) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for ClassifierModel is producing too many ' 'collisions.')
def map(model): if model.normalized_username is None: yield ('SUCCESS USERNAME NONE', 1) else: yield (utils.convert_to_hash( model.normalized_username, user_models.DeletedUsernameModel.ID_LENGTH), model.normalized_username)
def _generate_hash(cls, recipient_id, email_subject, email_body): """Generate hash for a given recipient_id, email_subject and cleaned email_body. """ hash_value = utils.convert_to_hash( recipient_id + email_subject + email_body, 100) return hash_value
def _generate_unique_reply_to_id(cls): for _ in range(base_models.MAX_RETRIES): new_id = utils.convert_to_hash( '%s' % (utils.get_random_int(base_models.RAND_RANGE)), REPLY_TO_ID_LENGTH) if not cls.get_by_reply_to_id(new_id): return new_id raise Exception('Unique id generator is producing too many collisions.')
def _get_new_model_id(model_class: base_models.BaseModel) -> str: """Generates an ID for a new model. Returns: str. The new ID. """ for _ in python_utils.RANGE(_MAX_ID_GENERATION_ATTEMPTS): new_id = utils.convert_to_hash(uuid.uuid4().hex, 22) if model_class.get(new_id, strict=False) is None: return new_id raise RuntimeError('Failed to generate a unique ID after %d attempts' % (_MAX_ID_GENERATION_ATTEMPTS))
def _generate_id(cls, intent): id_prefix = '%s.' % intent for _ in range(base_models.MAX_RETRIES): new_id = '%s.%s' % ( id_prefix, utils.convert_to_hash( str(utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH)) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for SentEmailModel is producing too many ' 'collisions.')
def _generate_hash(cls, recipient_id, email_subject, email_body): """Generate hash for a given recipient_id, email_subject and cleaned email_body. Args: recipient_id: str. The user ID of the email recipient. email_subject: str. The subject line of the email. email_body: str. The HTML content of the email body. Returns: str. The generated hash value of the given email. """ hash_value = utils.convert_to_hash( recipient_id + email_subject + email_body, 100) return hash_value
def create_hash(): url_param = request.get_json().get('url', None) if url_param is not None: if validators.url(url_param): found_hash = utils.check_url_exists(redis_client, url_param) if found_hash is not None: return jsonify({'result': True, 'hash': found_hash}) hash = utils.convert_to_hash( utils.get_and_increment_index(redis_client)) utils.set_hash(redis_client, hash, url_param) return jsonify({ 'result': True, 'short_url': '{}/{}'.format(request.headers.get('Origin'), hash) }) return jsonify({'result': False})
def _generate_unique_reply_to_id(cls): """Generates the unique reply-to id. Raises: Exception. When unique id generator produces too many collisions. Returns: str. The unique reply-to id if there are no collisions. """ for _ in python_utils.RANGE(base_models.MAX_RETRIES): new_id = utils.convert_to_hash( '%s' % (utils.get_random_int(base_models.RAND_RANGE)), REPLY_TO_ID_LENGTH) if not cls.get_by_reply_to_id(new_id): return new_id raise Exception('Unique id generator is producing too many collisions.')
def generate_new_blog_post_id(cls): """Generates a new blog post ID which is unique and is in the form of random hash of 12 chars. Returns: str. A blog post ID that is different from the IDs of all the existing blog posts. Raises: Exception. There were too many collisions with existing blog post IDs when attempting to generate a new blog post ID. """ for _ in python_utils.RANGE(base_models.MAX_RETRIES): blog_post_id = utils.convert_to_hash( python_utils.UNICODE( utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH) if not cls.get_by_id(blog_post_id): return blog_post_id raise Exception( 'New blog post id generator is producing too many collisions.')
def get_machine_translation(cls, source_language_code, target_language_code, source_text): """Gets MachineTranslationModel by language codes and source text. Args: source_language_code: str. The language code for the source text language. Must be different from target_language_code. target_language_code: str. The language code for the target translation language. Must be different from source_language_code. source_text: str. The untranslated source text. Returns: MachineTranslationModel|None. The MachineTranslationModel instance corresponding to the given inputs, if such a translation exists, or None if no translation is found. """ hashed_source_text = utils.convert_to_hash(source_text, 50) instance_id = cls._generate_id(source_language_code, target_language_code, hashed_source_text) return cls.get(instance_id, strict=False)
def _get_new_id(cls): """Generates a unique ID for the question of the form {{random_hash_of_12_chars}} Returns: new_id: int. ID of the new QuestionModel instance. Raises: Exception: The ID generator for QuestionModel is producing too many collisions. """ for _ in python_utils.RANGE(base_models.MAX_RETRIES): new_id = utils.convert_to_hash( python_utils.STR(utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for QuestionModel is producing too many ' 'collisions.')
def _get_new_id(cls) -> str: """Generates a unique ID for the question in the form of random hash of 12 chars. Returns: new_id: str. ID of the new QuestionModel instance. Raises: Exception. The ID generator for QuestionModel is producing too many collisions. """ for _ in python_utils.RANGE(base_models.MAX_RETRIES): new_id = utils.convert_to_hash( python_utils.UNICODE( utils.get_random_int(base_models.RAND_RANGE)), base_models.ID_LENGTH) if not cls.get_by_id(new_id): return new_id raise Exception( 'The id generator for QuestionModel is producing too many ' 'collisions.')
def hash_answer(answer): return utils.convert_to_hash(answer, MAX_ANSWER_HASH_LEN)
def test_validate_convert_to_hash(self): # type: () -> None with self.assertRaisesRegexp( # type: ignore[no-untyped-call] Exception, 'Expected string, received 1 of type %s' % type(1)): utils.convert_to_hash(1, 10) # type: ignore[arg-type]
def test_validate_convert_to_hash(self): with self.assertRaisesRegexp( Exception, 'Expected string, received 1 of type %s' % type(1)): utils.convert_to_hash(1, 10)