def test_get_thumbnail_icon_url_for_category(self) -> None: self.assertEqual( utils.get_thumbnail_icon_url_for_category('Architecture'), '/subjects/Architecture.svg') self.assertEqual( utils.get_thumbnail_icon_url_for_category('Graph Theory'), '/subjects/GraphTheory.svg') self.assertEqual( utils.get_thumbnail_icon_url_for_category('Nonexistent'), '/subjects/Lightbulb.svg')
def _get_displayable_collection_summary_dicts(collection_summaries): """Gets a summary of collections in human readable form. Args: collection_summaries: list(CollectionSummary). List of collection summary domain object. Returns: list(dict). A list of exploration summary dicts in human readable form. Example: [ { 'category': u'A category', 'community_owned': False, 'id': 'eid2', 'language_code': 'en', 'num_views': 0, 'objective': u'An objective', 'status': 'public', 'tags': [], 'thumbnail_bg_color': '#a33f40', 'thumbnail_icon_url': self.get_static_asset_url( '/images/subjects/Lightbulb.svg'), 'title': u'Exploration 2 Albert title', }, ] """ displayable_collection_summaries = [] for collection_summary in collection_summaries: if collection_summary and collection_summary.status != ( rights_domain.ACTIVITY_STATUS_PRIVATE): displayable_collection_summaries.append({ 'id': collection_summary.id, 'title': collection_summary.title, 'category': collection_summary.category, 'activity_type': constants.ACTIVITY_TYPE_COLLECTION, 'objective': collection_summary.objective, 'language_code': collection_summary.language_code, 'tags': collection_summary.tags, 'node_count': collection_summary.node_count, 'last_updated_msec': utils.get_time_in_millisecs( collection_summary.collection_model_last_updated), 'thumbnail_icon_url': (utils.get_thumbnail_icon_url_for_category( collection_summary.category)), 'thumbnail_bg_color': utils.get_hex_color_for_category(collection_summary.category) }) return displayable_collection_summaries
def get(self): """Handles GET requests.""" def _round_average_ratings(rating): """Returns the rounded average rating to display on the creator dashboard. Args: rating: float. The rating of the lesson. Returns: float. The rounded average value of rating. """ return round(rating, feconf.AVERAGE_RATINGS_DASHBOARD_PRECISION) subscribed_exploration_summaries = ( exp_fetchers.get_exploration_summaries_subscribed_to(self.user_id)) subscribed_collection_summaries = ( collection_services.get_collection_summaries_subscribed_to( self.user_id)) exploration_ids_subscribed_to = [ summary.id for summary in subscribed_exploration_summaries ] exp_summary_dicts = summary_services.get_displayable_exp_summary_dicts( subscribed_exploration_summaries) collection_summary_dicts = [] feedback_thread_analytics = ( feedback_services.get_thread_analytics_multi( exploration_ids_subscribed_to)) # TODO(bhenning): Update this to use unresolved answers from # stats_services once the training interface is enabled and it's cheaper # to retrieve top answers from stats_services. for ind, exploration in enumerate(exp_summary_dicts): exploration.update(feedback_thread_analytics[ind].to_dict()) exp_summary_dicts = sorted( exp_summary_dicts, key=lambda x: (x['num_open_threads'], x['last_updated_msec']), reverse=True) topic_summaries = topic_fetchers.get_all_topic_summaries() topic_summary_dicts = [ summary.to_dict() for summary in topic_summaries ] if role_services.ACTION_CREATE_COLLECTION in self.user.actions: for collection_summary in subscribed_collection_summaries: # TODO(sll): Reuse _get_displayable_collection_summary_dicts() # in summary_services, instead of replicating it like this. collection_summary_dicts.append({ 'id': collection_summary.id, 'title': collection_summary.title, 'category': collection_summary.category, 'objective': collection_summary.objective, 'language_code': collection_summary.language_code, 'last_updated_msec': utils.get_time_in_millisecs( collection_summary.collection_model_last_updated), 'created_on': utils.get_time_in_millisecs( collection_summary.collection_model_created_on), 'status': collection_summary.status, 'node_count': collection_summary.node_count, 'community_owned': collection_summary.community_owned, 'thumbnail_icon_url': (utils.get_thumbnail_icon_url_for_category( collection_summary.category)), 'thumbnail_bg_color': utils.get_hex_color_for_category( collection_summary.category), }) dashboard_stats = user_services.get_dashboard_stats(self.user_id) dashboard_stats.update({ 'total_open_feedback': feedback_services.get_total_open_threads(feedback_thread_analytics) }) if dashboard_stats and dashboard_stats.get('average_ratings'): dashboard_stats['average_ratings'] = (_round_average_ratings( dashboard_stats['average_ratings'])) last_week_stats = (user_services.get_last_week_dashboard_stats( self.user_id)) if last_week_stats and len(list(last_week_stats.keys())) != 1: logging.exception( '\'last_week_stats\' should contain only one key-value pair' ' denoting last week dashboard stats of the user keyed by a' ' datetime string.') last_week_stats = None if last_week_stats: # 'last_week_stats' is a dict with only one key-value pair denoting # last week dashboard stats of the user keyed by a datetime string. datetime_of_stats = list(last_week_stats.keys())[0] last_week_stats_average_ratings = (list( last_week_stats.values())[0].get('average_ratings')) if last_week_stats_average_ratings: last_week_stats[datetime_of_stats]['average_ratings'] = ( _round_average_ratings(last_week_stats_average_ratings)) subscriber_ids = subscription_services.get_all_subscribers_of_creator( self.user_id) subscribers_settings = user_services.get_users_settings(subscriber_ids) subscribers_list = [] for index, subscriber_settings in enumerate(subscribers_settings): subscriber_summary = { 'subscriber_picture_data_url': (subscriber_settings.profile_picture_data_url), 'subscriber_username': subscriber_settings.username, 'subscriber_impact': (user_services.get_user_impact_score(subscriber_ids[index])) } subscribers_list.append(subscriber_summary) user_settings = user_services.get_user_settings(self.user_id, strict=False) creator_dashboard_display_pref = ( user_settings.creator_dashboard_display_pref) suggestions_created_by_user = suggestion_services.query_suggestions([ ('author_id', self.user_id), ('suggestion_type', feconf.SUGGESTION_TYPE_EDIT_STATE_CONTENT) ]) suggestions_which_can_be_reviewed = ( suggestion_services. get_all_suggestions_that_can_be_reviewed_by_user(self.user_id)) for s in suggestions_created_by_user: s.populate_old_value_of_change() for s in suggestions_which_can_be_reviewed: s.populate_old_value_of_change() suggestion_dicts_created_by_user = ([ s.to_dict() for s in suggestions_created_by_user ]) suggestion_dicts_which_can_be_reviewed = ([ s.to_dict() for s in suggestions_which_can_be_reviewed ]) ids_of_suggestions_created_by_user = ([ s['suggestion_id'] for s in suggestion_dicts_created_by_user ]) ids_of_suggestions_which_can_be_reviewed = ([ s['suggestion_id'] for s in suggestion_dicts_which_can_be_reviewed ]) threads_linked_to_suggestions_by_user = ([ t.to_dict() for t in feedback_services.get_multiple_threads( ids_of_suggestions_created_by_user) ]) threads_linked_to_suggestions_which_can_be_reviewed = ([ t.to_dict() for t in feedback_services.get_multiple_threads( ids_of_suggestions_which_can_be_reviewed) ]) self.values.update({ 'explorations_list': exp_summary_dicts, 'collections_list': collection_summary_dicts, 'dashboard_stats': dashboard_stats, 'last_week_stats': last_week_stats, 'subscribers_list': subscribers_list, 'display_preference': creator_dashboard_display_pref, 'threads_for_created_suggestions_list': (threads_linked_to_suggestions_by_user), 'threads_for_suggestions_to_review_list': (threads_linked_to_suggestions_which_can_be_reviewed), 'created_suggestions_list': suggestion_dicts_created_by_user, 'suggestions_to_review_list': (suggestion_dicts_which_can_be_reviewed), 'topic_summary_dicts': topic_summary_dicts }) self.render_json(self.values)
def get_displayable_exp_summary_dicts(exploration_summaries): """Gets a summary of explorations in human readable form. Given a list of exploration summary domain objects, returns a list, with the same number of elements, of the corresponding human-readable exploration summary dicts. This assumes that all the exploration summary domain objects passed in are valid (i.e., none of them are None). Args: exploration_summaries: list(ExplorationSummary). List of exploration summary objects. Returns: list(dict). A list of exploration summary dicts in human readable form. Example: [ { 'category': u'A category', 'community_owned': False, 'id': 'eid2', 'language_code': 'en', 'num_views': 0, 'objective': u'An objective', 'status': 'public', 'tags': [], 'thumbnail_bg_color': '#a33f40', 'thumbnail_icon_url': self.get_static_asset_url( '/images/subjects/Lightbulb.svg'), 'title': u'Exploration 2 Albert title', }, ] """ exp_version_references = [ exp_domain.ExpVersionReference(exp_summary.id, exp_summary.version) for exp_summary in exploration_summaries] exp_stats_list = stats_services.get_exploration_stats_multi( exp_version_references) view_counts = [exp_stats.num_starts for exp_stats in exp_stats_list] displayable_exp_summaries = [] for ind, exploration_summary in enumerate(exploration_summaries): if exploration_summary: summary_dict = { 'id': exploration_summary.id, 'title': exploration_summary.title, 'activity_type': constants.ACTIVITY_TYPE_EXPLORATION, 'category': exploration_summary.category, 'created_on_msec': utils.get_time_in_millisecs( exploration_summary.exploration_model_created_on), 'objective': exploration_summary.objective, 'language_code': exploration_summary.language_code, 'last_updated_msec': utils.get_time_in_millisecs( exploration_summary.exploration_model_last_updated ), 'human_readable_contributors_summary': ( get_human_readable_contributors_summary( exploration_summary.contributors_summary) ), 'status': exploration_summary.status, 'ratings': exploration_summary.ratings, 'community_owned': exploration_summary.community_owned, 'tags': exploration_summary.tags, 'thumbnail_icon_url': utils.get_thumbnail_icon_url_for_category( exploration_summary.category), 'thumbnail_bg_color': utils.get_hex_color_for_category( exploration_summary.category), 'num_views': view_counts[ind], } displayable_exp_summaries.append(summary_dict) return displayable_exp_summaries