def pre_delete_user(user_id): """Prepare user for the full deletion. 1. Mark all the activities that are private and solely owned by the user being deleted as deleted. 2. Disable all the email preferences. 3. Mark the user as to be deleted. 4. Create PendingDeletionRequestModel for the user. Args: user_id: str. The id of the user to be deleted. """ subscribed_exploration_summaries = ( exp_fetchers.get_exploration_summaries_subscribed_to(user_id)) explorations_to_be_deleted_ids = [ exp_summary.id for exp_summary in subscribed_exploration_summaries if exp_summary.is_private() and exp_summary.is_solely_owned_by_user(user_id)] exp_services.delete_explorations(user_id, explorations_to_be_deleted_ids) subscribed_collection_summaries = ( collection_services.get_collection_summaries_subscribed_to(user_id)) collections_to_be_deleted_ids = [ col_summary.id for col_summary in subscribed_collection_summaries if col_summary.is_private() and col_summary.is_solely_owned_by_user(user_id)] collection_services.delete_collections( user_id, collections_to_be_deleted_ids) # Set all the user's email preferences to False in order to disable all # ordinary emails that could be sent to the users. user_services.update_email_preferences(user_id, False, False, False, False) email = user_services.get_user_settings(user_id, strict=True).email user_services.mark_user_for_deletion(user_id) save_pending_deletion_request( wipeout_domain.PendingDeletionRequest.create_default( user_id, email, explorations_to_be_deleted_ids, collections_to_be_deleted_ids ) )
def pre_delete_user(user_id): """Prepare user for the full deletion. 1. Mark all the activities that are private and solely owned by the user being deleted as deleted. 2. Disable all the email preferences. 3. Mark the user as to be deleted. 4. Create PendingDeletionRequestModel for the user. Args: user_id: str. The id of the user to be deleted. """ subscribed_exploration_summaries = ( exp_fetchers.get_exploration_summaries_subscribed_to(user_id)) explorations_to_be_deleted_ids = [ exp_summary.id for exp_summary in subscribed_exploration_summaries if exp_summary.is_private() and exp_summary.is_solely_owned_by_user(user_id) ] # TODO(#8301): Implement delete_explorations to make this efficient. for exp_id in explorations_to_be_deleted_ids: exp_services.delete_exploration(user_id, exp_id) subscribed_collection_summaries = ( collection_services.get_collection_summaries_subscribed_to(user_id)) collections_to_be_deleted_ids = [ col_summary.id for col_summary in subscribed_collection_summaries if col_summary.is_private() and col_summary.is_solely_owned_by_user(user_id) ] # TODO(#8301): Implement delete_collections to make this efficient. for col_id in collections_to_be_deleted_ids: collection_services.delete_collection(user_id, col_id) # Set all the user's email preferences to False in order to disable all # ordinary emails that could be sent to the users. user_services.update_email_preferences(user_id, False, False, False, False) user_services.mark_user_for_deletion( user_id, explorations_to_be_deleted_ids, collections_to_be_deleted_ids, )
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 python_utils.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) if constants.ENABLE_NEW_STRUCTURE_PLAYERS: topic_summaries = topic_services.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_jobs_continuous.UserStatsAggregator.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.error( '\'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', suggestion_models.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 }) if constants.ENABLE_NEW_STRUCTURE_PLAYERS: self.values.update({'topic_summary_dicts': topic_summary_dicts}) self.render_json(self.values)
def _remove_user_from_activities_with_associated_rights_models( user_id, use_user_subscriptions_ids): """Remove the user from exploration, collection, and topic models. Args: user_id: str. The ID of the user for which to remove the user from explorations, collections, and topics. use_user_subscriptions_ids: bool. Whether to use the IDs from user's UserSubscriptionsModel. When False the IDs are gathered via datastore queries. """ if use_user_subscriptions_ids: subscribed_exploration_summaries = ( exp_fetchers.get_exploration_summaries_subscribed_to(user_id)) else: subscribed_exploration_summaries = ( exp_fetchers.get_exploration_summaries_where_user_has_role(user_id) ) explorations_to_be_deleted_ids = [ exp_summary.id for exp_summary in subscribed_exploration_summaries if exp_summary.is_private() and exp_summary.is_solely_owned_by_user(user_id) ] exp_services.delete_explorations(user_id, explorations_to_be_deleted_ids, force_deletion=True) # Release ownership of explorations that are public and are solely owned # by the to-be-deleted user. explorations_to_release_ownership_ids = [ exp_summary.id for exp_summary in subscribed_exploration_summaries if not exp_summary.is_private() and exp_summary. is_solely_owned_by_user(user_id) and not exp_summary.community_owned ] for exp_id in explorations_to_release_ownership_ids: rights_manager.release_ownership_of_exploration( user_services.get_system_user(), exp_id) explorations_to_remove_user_from_ids = [ exp_summary.id for exp_summary in subscribed_exploration_summaries if not exp_summary.is_solely_owned_by_user(user_id) and exp_summary.does_user_have_any_role(user_id) ] for exp_id in explorations_to_remove_user_from_ids: rights_manager.deassign_role_for_exploration( user_services.get_system_user(), exp_id, user_id) if use_user_subscriptions_ids: subscribed_collection_summaries = ( collection_services.get_collection_summaries_subscribed_to(user_id) ) else: subscribed_collection_summaries = ( collection_services.get_collection_summaries_where_user_has_role( user_id)) collections_to_be_deleted_ids = [ col_summary.id for col_summary in subscribed_collection_summaries if col_summary.is_private() and col_summary.is_solely_owned_by_user(user_id) ] collection_services.delete_collections(user_id, collections_to_be_deleted_ids, force_deletion=True) # Release ownership of collections that are public and are solely owned # by the to-be-deleted user. collections_to_release_ownership_ids = [ col_summary.id for col_summary in subscribed_collection_summaries if not col_summary.is_private() and col_summary. is_solely_owned_by_user(user_id) and not col_summary.community_owned ] for col_id in collections_to_release_ownership_ids: rights_manager.release_ownership_of_collection( user_services.get_system_user(), col_id) collections_to_remove_user_from_ids = [ col_summary.id for col_summary in subscribed_collection_summaries if not col_summary.is_solely_owned_by_user(user_id) and col_summary.does_user_have_any_role(user_id) ] for col_id in collections_to_remove_user_from_ids: rights_manager.deassign_role_for_collection( user_services.get_system_user(), col_id, user_id) topic_services.deassign_user_from_all_topics( user_services.get_system_user(), user_id)
def test_get_exploration_summaries_subscribed_to(self): summaries = exp_fetchers.get_exploration_summaries_subscribed_to( self.owner_id) self.assertEqual(summaries[0].title, self.exploration_1.title) self.assertEqual(summaries[1].title, self.exploration_2.title) self.assertEqual(summaries[2].title, self.exploration_3.title)
def pre_delete_user(user_id): """Prepare user for the full deletion. 1. Mark all the activities that are private and solely owned by the user being deleted as deleted. 2. Disable all the email preferences. 3. Mark the user as to be deleted. 4. Create PendingDeletionRequestModel for the user. Args: user_id: str. The id of the user to be deleted. If the user_id corresponds to a profile user then only that profile is deleted. For a full user, all of its associated profile users are deleted too. """ pending_deletion_requests = [] user_settings = user_services.get_user_settings(user_id, strict=True) linked_profile_user_ids = [ user.user_id for user in user_services.get_all_profiles_auth_details_by_parent_user_id(user_id) ] profile_users_settings_list = user_services.get_users_settings( linked_profile_user_ids) for profile_user_settings in profile_users_settings_list: profile_id = profile_user_settings.user_id user_services.mark_user_for_deletion(profile_id) pending_deletion_requests.append( wipeout_domain.PendingDeletionRequest.create_default( profile_id, profile_user_settings.email, profile_user_settings.role, [], [])) explorations_to_be_deleted_ids = [] collections_to_be_deleted_ids = [] if user_settings.role != feconf.ROLE_ID_LEARNER: subscribed_exploration_summaries = ( exp_fetchers.get_exploration_summaries_subscribed_to(user_id)) explorations_to_be_deleted_ids = [ exp_summary.id for exp_summary in subscribed_exploration_summaries if exp_summary.is_private() and exp_summary.is_solely_owned_by_user(user_id) ] exp_services.delete_explorations(user_id, explorations_to_be_deleted_ids) # Release ownership of explorations that are public and are solely owned # by the to-be-deleted user. explorations_to_release_ownership_ids = [ exp_summary.id for exp_summary in subscribed_exploration_summaries if not exp_summary.is_private() and exp_summary.is_solely_owned_by_user(user_id) ] for exp_id in explorations_to_release_ownership_ids: rights_manager.release_ownership_of_exploration( user_services.get_system_user(), exp_id) explorations_to_remove_user_from_ids = [ exp_summary.id for exp_summary in subscribed_exploration_summaries if not exp_summary.is_solely_owned_by_user(user_id) ] for exp_id in explorations_to_remove_user_from_ids: rights_manager.deassign_role_for_exploration( user_services.get_system_user(), exp_id, user_id) subscribed_collection_summaries = ( collection_services.get_collection_summaries_subscribed_to(user_id) ) collections_to_be_deleted_ids = [ col_summary.id for col_summary in subscribed_collection_summaries if col_summary.is_private() and col_summary.is_solely_owned_by_user(user_id) ] collection_services.delete_collections(user_id, collections_to_be_deleted_ids) # Release ownership of collections that are public and are solely owned # by the to-be-deleted user. collections_to_release_ownership_ids = [ col_summary.id for col_summary in subscribed_collection_summaries if not col_summary.is_private() and col_summary.is_solely_owned_by_user(user_id) ] for col_id in collections_to_release_ownership_ids: rights_manager.release_ownership_of_collection( user_services.get_system_user(), col_id) collections_to_remove_user_from_ids = [ col_summary.id for col_summary in subscribed_collection_summaries if not col_summary.is_solely_owned_by_user(user_id) ] for col_id in collections_to_remove_user_from_ids: rights_manager.deassign_role_for_collection( user_services.get_system_user(), col_id, user_id) topic_services.deassign_user_from_all_topics( user_services.get_system_user(), user_id) # Set all the user's email preferences to False in order to disable all # ordinary emails that could be sent to the users. user_services.update_email_preferences(user_id, False, False, False, False) user_services.mark_user_for_deletion(user_id) pending_deletion_requests.append( wipeout_domain.PendingDeletionRequest.create_default( user_id, user_settings.email, user_settings.role, explorations_to_be_deleted_ids, collections_to_be_deleted_ids)) save_pending_deletion_requests(pending_deletion_requests)