def test_contribution_msec(self): # Test the contribution time shows up correctly as None. self.signup(self.EMAIL, self.USERNAME) self.login(self.EMAIL) user_id = self.get_user_id_from_email(self.EMAIL) response_dict = self.get_json( '/profilehandler/data/%s' % self.USERNAME) self.assertIsNone(response_dict['first_contribution_msec']) # Update the first_contribution_msec to the current time in # milliseconds. first_time_in_msecs = utils.get_current_time_in_millisecs() user_services.update_first_contribution_msec_if_not_set( user_id, first_time_in_msecs) # Test the contribution date correctly changes to current_time_in_msecs. response_dict = self.get_json( '/profilehandler/data/%s' % self.USERNAME) self.assertEqual( response_dict['first_contribution_msec'], first_time_in_msecs) # Test that the contribution date is not changed after the first time it # is set. second_time_in_msecs = utils.get_current_time_in_millisecs() user_services.update_first_contribution_msec_if_not_set( user_id, second_time_in_msecs) response_dict = self.get_json( '/profilehandler/data/%s' % self.USERNAME) self.assertEqual( response_dict['first_contribution_msec'], first_time_in_msecs)
def generate_new_thread_id(cls, entity_type: str, entity_id: str) -> str: """Generates a new thread ID which is unique. Args: entity_type: str. The type of the entity. entity_id: str. The ID of the entity. Returns: str. A thread ID that is different from the IDs of all the existing threads within the given entity. Raises: Exception. There were too many collisions with existing thread IDs when attempting to generate a new thread ID. """ for _ in python_utils.RANGE(_MAX_RETRIES): thread_id = ( '%s.%s.%s%s' % (entity_type, entity_id, utils.base64_from_int( int(utils.get_current_time_in_millisecs())), utils.base64_from_int(utils.get_random_int(_RAND_RANGE)))) if not cls.get_by_id(thread_id): return thread_id raise Exception( 'New thread id generator is producing too many collisions.')
def update_collection(committer_id, collection_id, change_list, commit_message): """Updates a collection. Commits changes. Args: committer_id: str. The id of the user who is performing the update action. collection_id: str. The collection id. change_list: list(dict). Each entry represents a CollectionChange object. These changes are applied in sequence to produce the resulting collection. commit_message: str or None. A description of changes made to the collection. For published collections, this must be present; for unpublished collections, it may be equal to None. Raises: ValueError. The collection is public but no commit message received. """ is_public = rights_manager.is_collection_public(collection_id) if is_public and not commit_message: raise ValueError( 'Collection is public so expected a commit message but ' 'received none.') collection = apply_change_list(collection_id, change_list) _save_collection(committer_id, collection, commit_message, change_list) regenerate_collection_summary_with_new_contributor(collection.id, committer_id) if (not rights_manager.is_collection_private(collection.id) and committer_id != feconf.MIGRATION_BOT_USER_ID): user_services.update_first_contribution_msec_if_not_set( committer_id, utils.get_current_time_in_millisecs())
def _change_activity_status(committer_id, activity_id, activity_type, new_status, commit_message): """Changes the status of the given activity. Args: committer_id: str. ID of the user who is performing the update action. activity_id: str. ID of the activity. activity_type: str. The type of activity. Possible values: constants.ACTIVITY_TYPE_EXPLORATION, constants.ACTIVITY_TYPE_COLLECTION. new_status: str. The new status of the activity. commit_message: str. The human-written commit message for this change. """ activity_rights = _get_activity_rights(activity_type, activity_id) old_status = activity_rights.status activity_rights.status = new_status if activity_type == constants.ACTIVITY_TYPE_EXPLORATION: cmd_type = rights_domain.CMD_CHANGE_EXPLORATION_STATUS elif activity_type == constants.ACTIVITY_TYPE_COLLECTION: cmd_type = rights_domain.CMD_CHANGE_COLLECTION_STATUS commit_cmds = [{ 'cmd': cmd_type, 'old_status': old_status, 'new_status': new_status }] if new_status != rights_domain.ACTIVITY_STATUS_PRIVATE: activity_rights.viewer_ids = [] if activity_rights.first_published_msec is None: activity_rights.first_published_msec = ( utils.get_current_time_in_millisecs()) _save_activity_rights(committer_id, activity_rights, activity_type, commit_message, commit_cmds) _update_activity_summary(activity_type, activity_rights)
def publish_collection_and_update_user_profiles(committer, collection_id): """Publishes the collection with publish_collection() function in rights_manager.py, as well as updates first_contribution_msec. It is the responsibility of the caller to check that the collection is valid prior to publication. Args: committer: UserActionsInfo. UserActionsInfo object for the committer. collection_id: str. ID of the collection to be published. """ rights_manager.publish_collection(committer, collection_id) contribution_time_msec = utils.get_current_time_in_millisecs() collection_summary = get_collection_summary_by_id(collection_id) contributor_ids = collection_summary.contributor_ids for contributor in contributor_ids: user_services.update_first_contribution_msec_if_not_set( contributor, contribution_time_msec)
def test_get_current_time_in_millisecs_with_current_time(self) -> None: time_instance1 = utils.get_current_time_in_millisecs() time.sleep(2) time_instance2 = utils.get_current_time_in_millisecs() self.assertLess(time_instance1, time_instance2)