Esempio n. 1
0
    def get_user_voiceover_applications(
        cls,
        author_id: str,
        status: Optional[str] = None
    ) -> List['GeneralVoiceoverApplicationModel']:
        """Returns a list of voiceover application submitted by the given user.

        Args:
            author_id: str. The id of the user created the voiceover
                application.
            status: str|None. The status of the voiceover application.
                If the status is None, the query will fetch all the
                voiceover applications.

        Returns:
            list(GeneralVoiceoverApplicationModel). The list of voiceover
            applications submitted by the given user.
        """
        if status in STATUS_CHOICES:
            voiceover_application_query = cls.query(
                datastore_services.all_of(cls.author_id == author_id,
                                          cls.status == status))
        else:
            voiceover_application_query = cls.query(cls.author_id == author_id)

        return cast(List[GeneralVoiceoverApplicationModel],
                    voiceover_application_query.fetch())
Esempio n. 2
0
    def get_suggestions_waiting_too_long_for_review(
            cls) -> Sequence[GeneralSuggestionModel]:
        """Returns a list of suggestions that have been waiting for a review
        longer than SUGGESTION_REVIEW_WAIT_TIME_THRESHOLD_IN_DAYS days on the
        Contributor Dashboard. MAX_NUMBER_OF_SUGGESTIONS_TO_EMAIL_ADMIN
        suggestions are returned, sorted in descending order by their review
        wait time.

        Returns:
            list(GeneralSuggestionModel). A list of suggestions, sorted in
            descending order by their review wait time.

        Raises:
            Exception. If there are no suggestion types offered on the
                Contributor Dashboard.
        """
        if not feconf.CONTRIBUTOR_DASHBOARD_SUGGESTION_TYPES:
            raise Exception(
                'Expected the suggestion types offered on the Contributor '
                'Dashboard to be nonempty.')
        threshold_time = (datetime.datetime.utcnow() - datetime.timedelta(
            days=SUGGESTION_REVIEW_WAIT_TIME_THRESHOLD_IN_DAYS))
        return cls.get_all().filter(
            datastore_services.all_of(
                cls.status == STATUS_IN_REVIEW,
                cls.last_updated < threshold_time,
                cls.suggestion_type.IN(
                    feconf.CONTRIBUTOR_DASHBOARD_SUGGESTION_TYPES))).order(
                        cls.last_updated).fetch(
                            MAX_NUMBER_OF_SUGGESTIONS_TO_EMAIL_ADMIN)
Esempio n. 3
0
    def query_new_and_pending_training_jobs(
            cls, offset: int
    ) -> Tuple[Sequence['ClassifierTrainingJobModel'], int]:
        """Gets the next 10 jobs which are either in status "new" or "pending",
        ordered by their next_scheduled_check_time attribute.

        Args:
            offset: int. Number of query results to skip.

        Returns:
            tuple(list(ClassifierTrainingJobModel), int).
            A tuple containing the list of the ClassifierTrainingJobModels
            with status new or pending and the offset value.
        """
        query = (
            cls.get_all()
            .filter(
                datastore_services.all_of(
                    cls.status.IN([
                        feconf.TRAINING_JOB_STATUS_NEW,
                        feconf.TRAINING_JOB_STATUS_PENDING
                    ]),
                    cls.next_scheduled_check_time <= datetime.datetime.utcnow()
                )
            )
            .order(cls.next_scheduled_check_time)
        )

        classifier_job_models: Sequence[ClassifierTrainingJobModel] = (
            query.fetch(
                NEW_AND_PENDING_TRAINING_JOBS_FETCH_LIMIT, offset=offset))
        offset = offset + len(classifier_job_models)
        return classifier_job_models, offset
Esempio n. 4
0
    def get_in_review_suggestions_in_score_categories(
            cls, score_categories: List[str],
            user_id: str) -> Sequence[GeneralSuggestionModel]:
        """Gets all suggestions which are in review in the given
        score_categories.

        Args:
            score_categories: list(str). List of score categories to query for.
            user_id: str. The id of the user trying to make this query.
                As a user cannot review their own suggestions, suggestions
                authored by the user will be excluded.

        Returns:
            list(SuggestionModel). A list of suggestions that are in the given
            score categories, which are in review, but not created by the
            given user.

        Raises:
            Exception. Given list of score categories is empty.
        """
        if len(score_categories) == 0:
            raise Exception('Received empty list of score categories')

        return cls.get_all().filter(
            datastore_services.all_of(
                cls.status == STATUS_IN_REVIEW,
                cls.score_category.IN(score_categories),
                cls.author_id != user_id)).fetch(
                    feconf.DEFAULT_SUGGESTION_QUERY_LIMIT)
Esempio n. 5
0
    def get_translation_suggestion_ids_with_exp_ids(
            cls, exp_ids: List[str]) -> List[str]:
        """Gets the ids of translation suggestions corresponding to
        explorations with the given exploration ids.

        Args:
            exp_ids: list(str). List of exploration ids to query for.

        Returns:
            list(str). A list of translation suggestion ids that
            correspond to the given exploration ids. Note: it is not
            guaranteed that the suggestion ids returned are ordered by the
            exploration ids in exp_ids.
        """
        query = cls.get_all().filter(
            datastore_services.all_of(
                cls.suggestion_type ==
                feconf.SUGGESTION_TYPE_TRANSLATE_CONTENT,
                cls.target_id.IN(exp_ids)))
        suggestion_models: List[GeneralSuggestionModel] = []
        offset, more = (0, True)
        while more:
            results: Sequence[GeneralSuggestionModel] = (query.fetch(
                feconf.DEFAULT_SUGGESTION_QUERY_LIMIT, offset=offset))
            if len(results):
                offset = offset + len(results)
                suggestion_models.extend(results)
            else:
                more = False
        return [suggestion_model.id for suggestion_model in suggestion_models]
Esempio n. 6
0
    def test_query_with_range_like_filter(self) -> None:
        query = datastore_services.Query(filters=datastore_services.all_of(
            BarModel.prop >= 3, BarModel.prop < 6))

        beam_query = job_utils.get_beam_query_from_ndb_query(query)

        self.assertEqual(beam_query.filters,
                         (('prop', '>=', 3), ('prop', '<', 6)))
Esempio n. 7
0
    def get_by_url_fragment(cls, url_fragment: str) -> Optional[BlogPostModel]:
        """Gets BlogPostModel by url_fragment. Returns None if the blog post
        with the given url_fragment doesn't exist.

        Args:
            url_fragment: str. The url fragment of the blog post.

        Returns:
            BlogPostModel | None. The blog post model of the Blog or None if not
            found.
        """
        return BlogPostModel.query(
            datastore_services.all_of(cls.url_fragment == url_fragment,
                                      cls.deleted == False)  # pylint: disable=singleton-comparison
        ).get()
Esempio n. 8
0
    def get_reviewable_voiceover_applications(
            cls, user_id: str) -> Sequence[GeneralVoiceoverApplicationModel]:
        """Returns a list of voiceover application which a given user can
        review.

        Args:
            user_id: str. The id of the user trying to make this query.
                As a user cannot review their own voiceover application, so the
                voiceover application created by the user will be excluded.

        Returns:
            list(GeneralVoiceoverApplicationModel). The list of voiceover
            applications which the given user can review.
        """
        return cls.query(
            datastore_services.all_of(cls.author_id != user_id,
                                      cls.status == STATUS_IN_REVIEW)).fetch()
Esempio n. 9
0
    def get_question_suggestions_waiting_longest_for_review(
            cls) -> Sequence[GeneralSuggestionModel]:
        """Returns MAX_QUESTION_SUGGESTIONS_TO_FETCH_FOR_REVIEWER_EMAILS number
        of question suggestions, sorted in descending order by review wait
        time.

        Returns:
            list(GeneralSuggestionModel). A list of question suggestions,
            sorted in descending order based on how long the suggestions have
            been waiting for review.
        """
        return cls.get_all().filter(
            datastore_services.all_of(
                cls.status == STATUS_IN_REVIEW, cls.suggestion_type ==
                feconf.SUGGESTION_TYPE_ADD_QUESTION)).order(
                    cls.last_updated).fetch(
                        MAX_QUESTION_SUGGESTIONS_TO_FETCH_FOR_REVIEWER_EMAILS)
Esempio n. 10
0
    def get_user_created_suggestions_of_suggestion_type(
            cls, suggestion_type: str,
            user_id: str) -> Sequence[GeneralSuggestionModel]:
        """Gets all suggestions of suggestion_type which the user has created.

        Args:
            suggestion_type: str. The type of suggestion to query for.
            user_id: str. The id of the user trying to make this query.

        Returns:
            list(SuggestionModel). A list of suggestions that are of the given
            type, which the given user has created.
        """
        return cls.get_all().filter(
            datastore_services.all_of(
                cls.suggestion_type == suggestion_type,
                cls.author_id == user_id)).order(-cls.created_on).fetch(
                    feconf.DEFAULT_SUGGESTION_QUERY_LIMIT)
Esempio n. 11
0
    def get_in_review_question_suggestions(
            cls, user_id: str) -> Sequence['GeneralSuggestionModel']:
        """Gets all question suggestions which are in review.

        Args:
            user_id: str. The id of the user trying to make this query.
                As a user cannot review their own suggestions, suggestions
                authored by the user will be excluded.

        Returns:
            list(SuggestionModel). A list of suggestions that are of the given
            type, which are in review, but not created by the given user.
        """
        return cls.get_all().filter(
            datastore_services.all_of(
                cls.status == STATUS_IN_REVIEW,
                cls.suggestion_type == feconf.SUGGESTION_TYPE_ADD_QUESTION,
                cls.author_id != user_id)).fetch(feconf.DEFAULT_QUERY_LIMIT)
Esempio n. 12
0
    def get_voiceover_applications(
            cls, target_type: str, target_id: str,
            language_code: str) -> Sequence[GeneralVoiceoverApplicationModel]:
        """Returns a list of voiceover applications submitted for a give entity
        in a given language.

        Args:
            target_type: str. The type of entity.
            target_id: str. The ID of the targeted entity.
            language_code: str. The code of the language in which the voiceover
                application is submitted.

        Returns:
            list(GeneralVoiceoverApplicationModel). The list of voiceover
            application which is submitted to a give entity in a given language.
        """
        return cls.query(
            datastore_services.all_of(
                cls.target_type == target_type, cls.target_id == target_id,
                cls.language_code == language_code)).fetch()
Esempio n. 13
0
    def get_translation_suggestions_waiting_longest_for_review(
            cls, language_code: str) -> Sequence[GeneralSuggestionModel]:
        """Returns MAX_TRANSLATION_SUGGESTIONS_TO_FETCH_FOR_REVIEWER_EMAILS
        number of translation suggestions in the specified language code,
        sorted in descending order by review wait time.

        Args:
            language_code: str. The ISO 639-1 language code of the translation
                suggestions.

        Returns:
            list(GeneralSuggestionModel). A list of translation suggestions,
            sorted in descending order based on how long the suggestions have
            been waiting for review.
        """
        return cls.get_all().filter(
            datastore_services.all_of(
                cls.status == STATUS_IN_REVIEW, cls.suggestion_type ==
                feconf.SUGGESTION_TYPE_TRANSLATE_CONTENT, cls.language_code ==
                language_code)).order(cls.last_updated).fetch(
                    MAX_TRANSLATION_SUGGESTIONS_TO_FETCH_FOR_REVIEWER_EMAILS)
Esempio n. 14
0
    def get_in_review_translation_suggestions(
            cls, user_id: str,
            language_codes: List[str]) -> Sequence[GeneralSuggestionModel]:
        """Gets all translation suggestions which are in review.

        Args:
            user_id: str. The id of the user trying to make this query.
                As a user cannot review their own suggestions, suggestions
                authored by the user will be excluded.
            language_codes: list(str). The list of language codes.

        Returns:
            list(SuggestionModel). A list of suggestions that are of the given
            type, which are in review, but not created by the given user.
        """
        return cls.get_all().filter(
            datastore_services.all_of(
                cls.status == STATUS_IN_REVIEW, cls.suggestion_type ==
                feconf.SUGGESTION_TYPE_TRANSLATE_CONTENT,
                cls.author_id != user_id,
                cls.language_code.IN(language_codes))).fetch(
                    feconf.DEFAULT_SUGGESTION_QUERY_LIMIT)
Esempio n. 15
0
    def get_translation_suggestions_in_review_with_exp_id(
            cls, exp_id: str,
            language_code: str) -> Sequence['GeneralSuggestionModel']:
        """Returns translation suggestions which are in review with target_id
        == exp_id.

        Args:
            exp_id: str. Exploration ID matching the target ID of the
                translation suggestions.
            language_code: str. Language code.

        Returns:
            list(SuggestionModel). A list of translation suggestions in review
            with target_id of exp_id. The number of returned results is capped
            by feconf.DEFAULT_QUERY_LIMIT.
        """
        return cls.get_all().filter(
            datastore_services.all_of(
                cls.status == STATUS_IN_REVIEW,
                cls.language_code == language_code, cls.suggestion_type ==
                feconf.SUGGESTION_TYPE_TRANSLATE_CONTENT,
                cls.target_id == exp_id)).fetch(feconf.DEFAULT_QUERY_LIMIT)
Esempio n. 16
0
    def get_in_review_translation_suggestions_by_exp_ids(
            cls, exp_ids: List[str],
            language_code: str) -> Sequence[GeneralSuggestionModel]:
        """Gets all in-review translation suggestions matching the supplied
        exp_ids and language_code.

        Args:
            exp_ids: list(str). Exploration IDs matching the target ID of the
                translation suggestions.
            language_code: str. The ISO 639-1 language code of the translation
                suggestions.

        Returns:
            list(SuggestionModel). A list of suggestions matching the supplied
            exp_ids and language_code.
        """
        return cls.get_all().filter(
            datastore_services.all_of(
                cls.status == STATUS_IN_REVIEW, cls.suggestion_type ==
                feconf.SUGGESTION_TYPE_TRANSLATE_CONTENT,
                cls.target_id.IN(exp_ids),
                cls.language_code == language_code)).fetch(
                    feconf.DEFAULT_SUGGESTION_QUERY_LIMIT)