Пример #1
0
def get_resources():
    """
    Gets a paginated list of resources.

    If the URL parameters `language` or `category` are found
    in the request, the list will be filtered by these parameters.

    The filters are case insensitive.
    """
    try:
        resource_paginator = Paginator(Config.RESOURCE_PAGINATOR, request)

        # Fetch the filter params from the url, if they were provided.
        language = request.args.get('language')
        category = request.args.get('category')

        # Filter on language
        if language and not category:
            query = Resource.query.filter(
                Resource.languages.any(
                    Language.name.ilike(language)
                )
            )

        # Filter on category
        elif category and not language:
            query = Resource.query.filter(
                Resource.category.has(
                    func.lower(Category.name) == category.lower()
                )
            )

        # Filter on both
        elif category and language:
            query = Resource.query.filter(
                and_(
                    Resource.languages.any(
                        Language.name.ilike(language)
                    ),
                    Resource.category.has(
                        func.lower(Category.name) == category.lower()
                    )
                )
            )

        # No filters
        else:
            query = Resource.query

        resource_list = [
            resource.serialize for resource in resource_paginator.items(query)
        ]

    except Exception as e:
        print_tb(e.__traceback__)
        print(e)
        resource_list = []
    finally:
        return jsonify(resource_list)
Пример #2
0
def get_languages():
    language_paginator = Paginator(Config.LANGUAGE_PAGINATOR, request)
    query = Language.query

    language_list = [
        language.serialize for language in language_paginator.items(query)
    ]

    return standardize_response(language_list, None, "ok")
Пример #3
0
def get_resources():
    """
    Gets a paginated list of resources.

    If the URL parameters `language` or `category` are found
    in the request, the list will be filtered by these parameters.

    The filters are case insensitive.
    """
    resource_paginator = Paginator(Config.RESOURCE_PAGINATOR, request)

    # Fetch the filter params from the url, if they were provided.
    language = request.args.get('language')
    category = request.args.get('category')
    updated_after = request.args.get('updated_after')

    q = Resource.query

    # Filter on language
    if language:
        q = q.filter(Resource.languages.any(Language.name.ilike(language)))

    # Filter on category
    if category:
        q = q.filter(
            Resource.category.has(
                func.lower(Category.name) == category.lower()))

    # Filter on updated_after
    if updated_after:
        try:
            uaDate = parser.parse(updated_after)
            if uaDate > datetime.now():
                raise Exception("updated_after greater than today's date")
            uaDate = uaDate.strftime("%Y-%m-%d")
        except Exception as e:
            logger.error(e)
            message = 'The value for "updated_after" is invalid'
            error = [{"code": "bad-value", "message": message}]
            return standardize_response(None, error, "unprocessable-entity",
                                        422)

        q = q.filter(
            or_(Resource.created_at >= uaDate,
                Resource.last_updated >= uaDate))

    try:
        resource_list = [
            resource.serialize for resource in resource_paginator.items(q)
        ]
    except Exception as e:
        logger.error(e)
        return standardize_response(None, [{
            "code": "bad-request"
        }], "bad request", 400)

    return standardize_response(resource_list, None, "ok")
Пример #4
0
def get_languages():
    language_paginator = Paginator(Config.LANGUAGE_PAGINATOR, request)
    query = Language.query

    try:
        language_list = [
            language.serialize for language in language_paginator.items(query)
        ]
    except Exception as e:
        logger.exception(e)
        return standardize_response(status_code=500)

    return standardize_response(payload=dict(data=language_list))
Пример #5
0
def get_languages():
    language_paginator = Paginator(Config.LANGUAGE_PAGINATOR, request)
    query = Language.query

    try:
        language_list = [
            language.serialize for language in language_paginator.items(query)
        ]
    except Exception as e:
        logger.error(e)
        return standardize_response(None, [{"code": "bad-request"}], "bad request", 400)

    return standardize_response(language_list, None, "ok")
Пример #6
0
def get_categories():
    try:
        category_paginator = Paginator(Config.CATEGORY_PAGINATOR, request)
        query = Category.query

        category_list = [
            category.serialize for category in category_paginator.items(query)
        ]

    except Exception as e:
        logger.exception(e)
        return standardize_response(status_code=500)

    return standardize_response(payload=dict(data=category_list))
Пример #7
0
def get_categories():
    try:
        category_paginator = Paginator(Config.CATEGORY_PAGINATOR, request)
        query = Category.query

        category_list = [
            category.serialize for category in category_paginator.items(query)
        ]

    except Exception as e:
        print_tb(e.__traceback__)
        print(e)
        category_list = []
    finally:
        return standardize_response(category_list, None, "ok")