def get_exploration_summary_from_model(exp_summary_model): """Returns an ExplorationSummary domain object. Args: exp_summary_model: ExplorationSummary. An ExplorationSummary model instance. Returns: ExplorationSummary. The summary domain object correspoding to the given exploration summary model. """ return exp_domain.ExplorationSummary( exp_summary_model.id, exp_summary_model.title, exp_summary_model.category, exp_summary_model.objective, exp_summary_model.language_code, exp_summary_model.tags, exp_summary_model.ratings, exp_summary_model.scaled_average_rating, exp_summary_model.status, exp_summary_model.community_owned, exp_summary_model.owner_ids, exp_summary_model.editor_ids, exp_summary_model.voice_artist_ids, exp_summary_model.viewer_ids, exp_summary_model.contributor_ids, exp_summary_model.contributors_summary, exp_summary_model.version, exp_summary_model.exploration_model_created_on, exp_summary_model.exploration_model_last_updated, exp_summary_model.first_published_msec, exp_summary_model.deleted )
def get_summary_of_exploration(exploration): """Create ExplorationSummary domain object for a given Exploration domain object and return it. """ exp_rights = exp_models.ExplorationRightsModel.get_by_id(exploration.id) exploration_model_last_updated = exploration.last_updated exploration_model_created_on = exploration.created_on exp_summary = exp_domain.ExplorationSummary( exploration.id, exploration.title, exploration.category, exploration.objective, exploration.language_code, exploration.skill_tags, exp_rights.status, exp_rights.community_owned, exp_rights.owner_ids, exp_rights.editor_ids, exp_rights.viewer_ids, exploration.version, exploration_model_created_on, exploration_model_last_updated ) return exp_summary
def get_summary_of_exploration(exploration): """Create ExplorationSummary domain object for a given Exploration domain object and return it. """ exp_rights = exp_models.ExplorationRightsModel.get_by_id(exploration.id) exp_summary_model = exp_models.ExpSummaryModel.get_by_id(exploration.id) if exp_summary_model: old_exp_summary = get_exploration_summary_from_model(exp_summary_model) ratings = old_exp_summary.ratings or feconf.get_empty_ratings() else: ratings = feconf.get_empty_ratings() exploration_model_last_updated = exploration.last_updated exploration_model_created_on = exploration.created_on exp_summary = exp_domain.ExplorationSummary( exploration.id, exploration.title, exploration.category, exploration.objective, exploration.language_code, exploration.tags, ratings, exp_rights.status, exp_rights.community_owned, exp_rights.owner_ids, exp_rights.editor_ids, exp_rights.viewer_ids, exploration.version, exploration_model_created_on, exploration_model_last_updated ) return exp_summary
def get_exploration_summary_from_model(exp_summary_model): return exp_domain.ExplorationSummary( exp_summary_model.id, exp_summary_model.title, exp_summary_model.category, exp_summary_model.objective, exp_summary_model.language_code, exp_summary_model.skill_tags, exp_summary_model.status, exp_summary_model.community_owned, exp_summary_model.owner_ids, exp_summary_model.editor_ids, exp_summary_model.viewer_ids, exp_summary_model.version, exp_summary_model.exploration_model_created_on, exp_summary_model.exploration_model_last_updated)
def _run_batch_job_once_and_verify_output( self, exp_specs, default_title='A title', default_category='A category', default_status=rights_manager.ACTIVITY_STATUS_PUBLIC): """Run batch job for creating exploration summaries once and verify its output. exp_specs is a list of dicts with exploration specifications. Allowed keys are category, status, title. If a key is not specified, the default value is used. """ with self.swap(jobs_registry, 'ONE_OFF_JOB_MANAGERS', self.ONE_OFF_JOB_MANAGERS_FOR_TESTS): default_spec = { 'title': default_title, 'category': default_category, 'status': default_status } self.signup(self.ADMIN_EMAIL, self.ADMIN_USERNAME) self.login(self.ADMIN_EMAIL) admin_id = self.get_user_id_from_email(self.ADMIN_EMAIL) self.set_admins([self.ADMIN_USERNAME]) admin = user_services.UserActionsInfo(admin_id) # Create and delete an exploration (to make sure job handles # deleted explorations correctly). exp_id = '100' self.save_new_valid_exploration(exp_id, admin_id, title=default_spec['title'], category=default_spec['category']) exploration = exp_services.get_exploration_by_id(exp_id) exp_services.delete_exploration(admin_id, exp_id) # Get dummy explorations. num_exps = len(exp_specs) expected_job_output = {} for ind in range(num_exps): exp_id = str(ind) spec = default_spec spec.update(exp_specs[ind]) self.save_new_valid_exploration(exp_id, admin_id, title=spec['title'], category=spec['category']) exploration = exp_services.get_exploration_by_id(exp_id) # Publish exploration. if spec['status'] == rights_manager.ACTIVITY_STATUS_PUBLIC: rights_manager.publish_exploration(admin, exp_id) # Do not include user_id here, so all explorations are not # editable for now (will be updated depending on user_id # in galleries). exp_rights_model = exp_models.ExplorationRightsModel.get( exp_id) exploration = exp_services.get_exploration_by_id(exp_id) exploration_model_last_updated = exploration.last_updated exploration_model_created_on = exploration.created_on first_published_msec = (exp_rights_model.first_published_msec) # Manually create the expected summary specifying title, # category, etc. expected_job_output[exp_id] = exp_domain.ExplorationSummary( exp_id, spec['title'], spec['category'], exploration.objective, exploration.language_code, exploration.tags, feconf.get_empty_ratings(), feconf.EMPTY_SCALED_AVERAGE_RATING, spec['status'], exp_rights_model.community_owned, exp_rights_model.owner_ids, exp_rights_model.editor_ids, exp_rights_model.translator_ids, exp_rights_model.viewer_ids, [admin_id], {admin_id: 1}, exploration.version, exploration_model_created_on, exploration_model_last_updated, first_published_msec) # Note: Calling constructor for fields that are not required # and have no default value does not work, because # unspecified fields will be empty list in # expected_job_output but will be unspecified in # actual_job_output. if exploration.tags: expected_job_output[exp_id].tags = exploration.tags if exp_rights_model.owner_ids: expected_job_output[exp_id].owner_ids = ( exp_rights_model.owner_ids) if exp_rights_model.editor_ids: expected_job_output[exp_id].editor_ids = ( exp_rights_model.editor_ids) if exp_rights_model.translator_ids: expected_job_output[exp_id].translator_ids = ( exp_rights_model.translator_ids) if exp_rights_model.viewer_ids: expected_job_output[exp_id].viewer_ids = ( exp_rights_model.viewer_ids) if exploration.version: expected_job_output[exp_id].version = (exploration.version) # Run batch job. job_id = ( exp_jobs_one_off.ExpSummariesCreationOneOffJob.create_new()) exp_jobs_one_off.ExpSummariesCreationOneOffJob.enqueue(job_id) self.process_and_flush_pending_tasks() # Get and check job output. actual_job_output = exp_services.get_all_exploration_summaries() self.assertEqual(actual_job_output.keys(), expected_job_output.keys()) # Note: 'exploration_model_last_updated' is not expected to be the # same, because it is now read from the version model representing # the exploration's history snapshot, and not the ExplorationModel. simple_props = [ 'id', 'title', 'category', 'objective', 'language_code', 'tags', 'ratings', 'status', 'community_owned', 'owner_ids', 'editor_ids', 'translator_ids', 'viewer_ids', 'contributor_ids', 'contributors_summary', 'version', 'exploration_model_created_on' ] for exp_id in actual_job_output: for prop in simple_props: self.assertEqual( getattr(actual_job_output[exp_id], prop), getattr(expected_job_output[exp_id], prop))
def _run_batch_job_once_and_verify_output( self, exp_specs, default_title='A title', default_category='A category', default_status=rights_manager.EXPLORATION_STATUS_PUBLICIZED): """Run batch job for creating exploration summaries once and verify its output. exp_specs is a list of dicts with exploration specifications. Allowed keys are category, status, title. If a key is not specified, the default value is taken. """ from core.domain import exp_services with self.swap(jobs_registry, 'ONE_OFF_JOB_MANAGERS', self.ONE_OFF_JOB_MANAGERS_FOR_TESTS): # default specs default_specs = { 'title': default_title, 'category': default_category, 'status': default_status } self.signup(self.ADMIN_EMAIL, self.ADMIN_USERNAME) self.login(self.ADMIN_EMAIL) self.ADMIN_ID = self.get_user_id_from_email(self.ADMIN_EMAIL) self.set_admins([self.ADMIN_EMAIL]) # create and delete an exploration (to make sure job handles # deleted explorations correctly) exp_id = '100' self.save_new_valid_exploration(exp_id, self.ADMIN_ID, title=default_specs['title'], category=default_specs['category']) exploration = exp_services.get_exploration_by_id(exp_id) exp_services.delete_exploration(self.ADMIN_ID, exp_id) # get dummy explorations num_exps = len(exp_specs) expected_job_output = {} for ind in range(num_exps): exp_id = str(ind) spec = default_specs spec.update(exp_specs[ind]) self.save_new_valid_exploration(exp_id, self.ADMIN_ID, title=spec['title'], category=spec['category']) exploration = exp_services.get_exploration_by_id(exp_id) # publish or publicize exploration if spec['status'] == rights_manager.EXPLORATION_STATUS_PUBLIC: rights_manager.publish_exploration(self.ADMIN_ID, exp_id) elif (spec['status'] == rights_manager.EXPLORATION_STATUS_PUBLICIZED): rights_manager.publish_exploration(self.ADMIN_ID, exp_id) rights_manager.publicize_exploration(self.ADMIN_ID, exp_id) # do not include user_id here, so all explorations are not # editable for now (will be updated depending on user_id # in galleries) exp_rights_model = exp_models.ExplorationRightsModel.get( exp_id) exploration = exp_services.get_exploration_by_id(exp_id) exploration_model_last_updated = exploration.last_updated exploration_model_created_on = exploration.created_on # manually create the expectated summary specifying title, # category, etc expected_job_output[exp_id] = exp_domain.ExplorationSummary( exp_id, spec['title'], spec['category'], exploration.objective, exploration.language_code, exploration.tags, feconf.get_empty_ratings(), spec['status'], exp_rights_model.community_owned, exp_rights_model.owner_ids, exp_rights_model.editor_ids, exp_rights_model.viewer_ids, exploration.version, exploration_model_created_on, exploration_model_last_updated) # calling constructor for fields that are not required # and have no default value does not work b/c # unspecified fields will be empty list in # expected_job_output but will be unspecified in # actual_job_output if exploration.tags: expected_job_output[exp_id].tags = exploration.tags if exp_rights_model.owner_ids: expected_job_output[exp_id].owner_ids = ( exp_rights_model.owner_ids) if exp_rights_model.editor_ids: expected_job_output[exp_id].editor_ids = ( exp_rights_model.editor_ids) if exp_rights_model.viewer_ids: expected_job_output[exp_id].viewer_ids = ( exp_rights_model.viewer_ids) if exploration.version: expected_job_output[exp_id].version = (exploration.version) # run batch job job_id = exp_jobs.ExpSummariesCreationOneOffJob.create_new() exp_jobs.ExpSummariesCreationOneOffJob.enqueue(job_id) self.process_and_flush_pending_tasks() # get job output actual_job_output = exp_services.get_all_exploration_summaries() # check job output self.assertEqual(actual_job_output.keys(), expected_job_output.keys()) simple_props = [ 'id', 'title', 'category', 'objective', 'language_code', 'tags', 'ratings', 'status', 'community_owned', 'owner_ids', 'editor_ids', 'viewer_ids', 'version', 'exploration_model_created_on', 'exploration_model_last_updated' ] for exp_id in actual_job_output: for prop in simple_props: self.assertEqual( getattr(actual_job_output[exp_id], prop), getattr(expected_job_output[exp_id], prop))