Пример #1
0
def get_exploration_summaries_where_user_has_role(
    user_id: str
) -> List[exp_domain.ExplorationSummary]:
    """Returns a list of ExplorationSummary domain objects where the user has
    some role.

    Args:
        user_id: str. The id of the user.

    Returns:
        list(ExplorationSummary). List of ExplorationSummary domain objects
        where the user has some role.
    """
    exp_summary_models: Sequence[exp_models.ExpSummaryModel] = (
        exp_models.ExpSummaryModel.query(
            datastore_services.any_of(
                exp_models.ExpSummaryModel.owner_ids == user_id,
                exp_models.ExpSummaryModel.editor_ids == user_id,
                exp_models.ExpSummaryModel.voice_artist_ids == user_id,
                exp_models.ExpSummaryModel.viewer_ids == user_id,
                exp_models.ExpSummaryModel.contributor_ids == user_id
            )
        ).fetch()
    )
    return [
        get_exploration_summary_from_model(exp_summary_model)
        for exp_summary_model in exp_summary_models
    ]
Пример #2
0
    def get_private_at_least_viewable(
            cls,
            user_id: str
    ) -> Sequence['ExpSummaryModel']:
        """Fetches private exp summaries that are at least viewable by the
        given user.

        Args:
            user_id: str. The id of the given user.

        Returns:
            iterable. An iterable with private exp summaries that are at least
            viewable by the given user.
        """
        return ExpSummaryModel.query().filter(
            ExpSummaryModel.status == constants.ACTIVITY_STATUS_PRIVATE
        ).filter(
            datastore_services.any_of(
                ExpSummaryModel.owner_ids == user_id,
                ExpSummaryModel.editor_ids == user_id,
                ExpSummaryModel.voice_artist_ids == user_id,
                ExpSummaryModel.viewer_ids == user_id)
        ).filter(
            ExpSummaryModel.deleted == False  # pylint: disable=singleton-comparison
        ).fetch(feconf.DEFAULT_QUERY_LIMIT)
Пример #3
0
    def test_query_with_or_filter_raises_type_error(self) -> None:
        query = datastore_services.Query(filters=datastore_services.any_of(
            BarModel.prop == 1, BarModel.prop == 2))

        with self.assertRaisesRegexp(
                TypeError,
                'forbidden filter'):  # type: ignore[no-untyped-call]
            job_utils.get_beam_query_from_ndb_query(query)
Пример #4
0
    def has_reference_to_user_id(cls, user_id: str) -> bool:
        """Check whether GeneralVoiceoverApplicationModel exists for the user.

        Args:
            user_id: str. The ID of the user whose data should be checked.

        Returns:
            bool. Whether any models refer to the given user ID.
        """
        return cls.query(
            datastore_services.any_of(cls.author_id == user_id,
                                      cls.final_reviewer_id == user_id)).get(
                                          keys_only=True) is not None
Пример #5
0
    def has_reference_to_user_id(cls, user_id: str) -> bool:
        """Check whether SentEmailModel exists for user.

        Args:
            user_id: str. The ID of the user whose data should be checked.

        Returns:
            bool. Whether any models refer to the given user ID.
        """
        return cls.query(datastore_services.any_of(
            cls.recipient_id == user_id,
            cls.sender_id == user_id,
        )).get(keys_only=True) is not None
Пример #6
0
    def has_reference_to_user_id(cls, user_id: str) -> bool:
        """Check whether GeneralFeedbackThreadModel exists for user.

        Args:
            user_id: str. The ID of the user whose data should be checked.

        Returns:
            bool. Whether any models refer to the given user ID.
        """
        return cls.query(datastore_services.any_of(
            cls.original_author_id == user_id,
            cls.last_nonempty_message_author_id == user_id
        )).get(keys_only=True) is not None
Пример #7
0
    def has_reference_to_user_id(cls, user_id: str) -> bool:
        """Check whether CollectionSummaryModel references user.

        Args:
            user_id: str. The ID of the user whose data should be checked.

        Returns:
            bool. Whether any models refer to the given user ID.
        """
        return cls.query(datastore_services.any_of(
            cls.owner_ids == user_id,
            cls.editor_ids == user_id,
            cls.viewer_ids == user_id,
            cls.contributor_ids == user_id)).get(keys_only=True) is not None
Пример #8
0
    def has_reference_to_user_id(cls, user_id: str) -> bool:
        """Check whether LearnerGroupModel contains data of a given user.

        Args:
            user_id: str. The ID of the user whose data should be checked.

        Returns:
            bool. Whether any models refer to the given user ID.
        """
        return (cls.query(
            datastore_services.any_of(
                cls.student_user_ids == user_id,
                cls.invited_student_user_ids == user_id,
                cls.facilitator_user_ids == user_id)).get(keys_only=True)
                is not None)
Пример #9
0
    def get_at_least_editable(cls, user_id: str) -> Sequence[ExpSummaryModel]:
        """Fetches exp summaries that are at least editable by the given user.

        Args:
            user_id: str. The id of the given user.

        Returns:
            iterable. An iterable with exp summaries that are at least
            editable by the given user.
        """
        return ExpSummaryModel.query().filter(
            datastore_services.any_of(
                ExpSummaryModel.owner_ids == user_id,
                ExpSummaryModel.editor_ids == user_id)).filter(
                    ExpSummaryModel.deleted == False  # pylint: disable=singleton-comparison
                ).fetch(feconf.DEFAULT_QUERY_LIMIT)
Пример #10
0
    def has_reference_to_user_id(cls, user_id):
        # type: (Text) -> bool
        """Check whether CollectionRightsModel references the given user.

        Args:
            user_id: str. The ID of the user whose data should be checked.

        Returns:
            bool. Whether any models refer to the given user ID.
        """
        return cls.query(datastore_services.any_of(
            cls.owner_ids == user_id,
            cls.editor_ids == user_id,
            cls.voice_artist_ids == user_id,
            cls.viewer_ids == user_id
        )).get(keys_only=True) is not None
Пример #11
0
    def get_at_least_editable(
            cls, user_id: str) -> Sequence['CollectionSummaryModel']:
        """Returns an iterable with collection summary models that are at least
        editable by the given user.

        Args:
            user_id: str. The id of the given user.

        Returns:
            iterable. An iterable with collection summary models that are at
            least viewable by the given user.
        """
        return CollectionSummaryModel.get_all().filter(
            datastore_services.any_of(
                CollectionSummaryModel.owner_ids == user_id,
                CollectionSummaryModel.editor_ids == user_id)).fetch(
                    feconf.DEFAULT_QUERY_LIMIT)
Пример #12
0
    def get_by_facilitator_id(
            cls, facilitator_id: str) -> Sequence[LearnerGroupModel]:
        """Returns a list of all LearnerGroupModels that have the given
        facilitator id.

        Args:
            facilitator_id: str. The id of the facilitator.

        Returns:
            list(LearnerGroupModel)|None. A list of all LearnerGroupModels that
            have the given facilitator id or None if no such learner group
            models exist.
        """
        found_models: Sequence[LearnerGroupModel] = cls.get_all().filter(
            datastore_services.any_of(
                cls.facilitator_user_ids == facilitator_id)).fetch()

        return found_models
Пример #13
0
    def get_private_at_least_viewable(
            cls, user_id: str) -> Sequence['CollectionSummaryModel']:
        """Returns an iterable with private collection summary models that are
        at least viewable by the given user.

        Args:
            user_id: str. The id of the given user.

        Returns:
            iterable. An iterable with private collection summary models that
            are at least viewable by the given user.
        """
        return cls.get_all().filter(
            cls.status == constants.ACTIVITY_STATUS_PRIVATE).filter(
                datastore_services.any_of(cls.owner_ids == user_id,
                                          cls.editor_ids == user_id,
                                          cls.viewer_ids == user_id)).fetch(
                                              feconf.DEFAULT_QUERY_LIMIT)
Пример #14
0
    def export_data(cls, user_id: str) -> Dict[str, LearnerGroupDataDict]:
        """Takeout: Export LearnerGroupModel user-based properties.

        Args:
            user_id: str. The user_id denotes which user's data to extract.

        Returns:
            dict. A dict containing the user-relevant properties of
            LearnerGroupModel.
        """
        found_models = cls.get_all().filter(
            datastore_services.any_of(cls.student_user_ids == user_id,
                                      cls.invited_student_user_ids == user_id,
                                      cls.facilitator_user_ids == user_id))
        user_data = {}
        for learner_group_model in found_models:
            learner_group_data: LearnerGroupDataDict
            if user_id in learner_group_model.student_user_ids:
                learner_group_data = {
                    'title': learner_group_model.title,
                    'description': learner_group_model.description,
                    'role_in_group': 'student',
                    'subtopic_page_ids': learner_group_model.subtopic_page_ids,
                    'story_ids': learner_group_model.story_ids
                }
            elif user_id in learner_group_model.invited_student_user_ids:
                learner_group_data = {
                    'title': learner_group_model.title,
                    'description': learner_group_model.description,
                    'role_in_group': 'invited_student',
                    'subtopic_page_ids': [],
                    'story_ids': []
                }
            elif user_id in learner_group_model.facilitator_user_ids:
                learner_group_data = {
                    'title': learner_group_model.title,
                    'description': learner_group_model.description,
                    'role_in_group': 'facilitator',
                    'subtopic_page_ids': learner_group_model.subtopic_page_ids,
                    'story_ids': learner_group_model.story_ids
                }
            user_data[learner_group_model.id] = learner_group_data

        return user_data
Пример #15
0
    def apply_deletion_policy(cls, user_id: str) -> None:
        """Delete all LearnerGroupModel instances associated with the
        user.

        Args:
            user_id: str. The user_id denotes which user's data to delete.
        """
        found_models = cls.get_all().filter(
            datastore_services.any_of(cls.student_user_ids == user_id,
                                      cls.invited_student_user_ids == user_id,
                                      cls.facilitator_user_ids == user_id))

        learner_group_models_to_put = []

        for learner_group_model in found_models:
            # If the user is the facilitator of the group and there is
            # only one facilitator_user_id, delete the group.
            if (user_id in learner_group_model.facilitator_user_ids
                    and len(learner_group_model.facilitator_user_ids) == 1):
                learner_group_model.delete()
                continue

            # If the user is the facilitator of the group and there are
            # more then one facilitator_user_ids, delete the user from the
            # facilitator_user_ids list.
            if (user_id in learner_group_model.facilitator_user_ids
                    and len(learner_group_model.facilitator_user_ids) > 1):
                learner_group_model.facilitator_user_ids.remove(user_id)

            # If the user is a student, delete the user from the
            # student_user_ids list.
            elif user_id in learner_group_model.student_user_ids:
                learner_group_model.student_user_ids.remove(user_id)

            # If the user has been invited to join the group, delete the
            # user from the invited_student_user_ids list.
            elif user_id in learner_group_model.invited_student_user_ids:
                learner_group_model.invited_student_user_ids.remove(user_id)

            learner_group_models_to_put.append(learner_group_model)

        cls.update_timestamps_multi(learner_group_models_to_put)
        cls.put_multi(learner_group_models_to_put)
Пример #16
0
    def get_at_least_editable(cls,
                              user_id: str) -> List['CollectionSummaryModel']:
        """Returns an iterable with collection summary models that are at least
        editable by the given user.

        Args:
            user_id: str. The id of the given user.

        Returns:
            iterable. An iterable with collection summary models that are at
            least viewable by the given user.
        """
        summary_models = CollectionSummaryModel.query().filter(
            datastore_services.any_of(
                CollectionSummaryModel.owner_ids == user_id,
                CollectionSummaryModel.editor_ids == user_id)).filter(
                    CollectionSummaryModel.deleted == False  # pylint: disable=singleton-comparison
                ).fetch(feconf.DEFAULT_QUERY_LIMIT)

        return cast(List[CollectionSummaryModel], summary_models)
Пример #17
0
    def get_private_at_least_viewable(cls, user_id):
        # type: (Text) -> List[CollectionSummaryModel]
        """Returns an iterable with private collection summary models that are
        at least viewable by the given user.

        Args:
            user_id: str. The id of the given user.

        Returns:
            iterable. An iterable with private collection summary models that
            are at least viewable by the given user.
        """
        summary_models = CollectionSummaryModel.query().filter(
            CollectionSummaryModel.status == constants.ACTIVITY_STATUS_PRIVATE
        ).filter(
            datastore_services.any_of(
                CollectionSummaryModel.owner_ids == user_id,
                CollectionSummaryModel.editor_ids == user_id,
                CollectionSummaryModel.viewer_ids == user_id)
        ).filter(
            CollectionSummaryModel.deleted == False  # pylint: disable=singleton-comparison
        ).fetch(feconf.DEFAULT_QUERY_LIMIT)

        return cast(List[CollectionSummaryModel], summary_models)