Ejemplo n.º 1
0
def get_responses(id):
    '''Get responses.'''
    serializer = Serializer(QuestionnaireResponseSchema, request.args)
    query = QuestionnaireResponse.query.\
        filter(QuestionnaireResponse.user_id == auth.current_user.id).\
        filter(QuestionnaireResponse.questionnaire_id == id).\
        order_by(QuestionnaireResponse.created_at.desc())
    page = Pagination(request, query=query)
    return serializer.dump_page(page)
Ejemplo n.º 2
0
def get_questionnaires():
    '''Get questionnaires.'''
    serializer = Serializer(QuestionnaireSchema, request.args)
    query = Questionnaire.query.\
        outerjoin(QuestionnaireResponse, and_(
            QuestionnaireResponse.user_id == auth.current_user.id,
            QuestionnaireResponse.questionnaire_id == Questionnaire.id
        )).\
        options(contains_eager(Questionnaire.responses))

    page = Pagination(request, query=query)
    return serializer.dump_page(page)
Ejemplo n.º 3
0
def test_many_pages(session):
    users = [
        User(username='******' % i,
             email='*****@*****.**' % i,
             password='******') for i in xrange(10)
    ]
    session.add_all(users)
    session.commit()
    query = User.query
    request = FakeRequest(args={'per_page': 2})
    page = Pagination(request, query)
    result = Serializer(UserSchema).dump_page(page)
    assert len(result['items']) == 2
Ejemplo n.º 4
0
def get_users():
    '''Get users.'''
    serializer = Serializer(UserSchema, request.args)
    query = User.query
    page = Pagination(request, query=query)
    return serializer.dump_page(page)
Ejemplo n.º 5
0
def test_page_doesnt_exist(session):
    query = User.query
    with pytest.raises(PaginationError):
        Pagination(FakeRequest(args={'page': 2}), query)
Ejemplo n.º 6
0
def test_empty_page(session):
    query = User.query
    page = Pagination(FakeRequest(), query)
    result = Serializer(UserSchema).dump_page(page)
    assert result['items'] == []
Ejemplo n.º 7
0
def get_exercises(favorited_by=None):
    '''Get exercise collection, if favorited_by is set then get the
    collection of favorites of the user.'''

    user_id = auth.current_user.id if auth.current_user else None

    # client requests favorites that are not his.
    if favorited_by and favorited_by != user_id:
        raise AuthorizationError

    search = request.args.get('search')
    category = request.args.get('category')
    order_by = request.args.get('order_by')
    author = request.args.get('author')

    orderfunc = desc
    if order_by and len(order_by) > 0 and order_by[-1] in '+ -'.split():
        if order_by[-1] == '+':
            orderfunc = asc
        order_by = order_by[:-1]

    query = Exercise.query

    if search:
        search_terms = (' | ').join(search.split())
        query = query.add_columns(func.ts_rank(
            Exercise.tsv, func.to_tsquery(search_terms)).label('search_rank')).\
            filter(Exercise.tsv.match(search_terms))

        if order_by == 'relevance':
            query = query.order_by(nullslast(orderfunc('search_rank')))

    if user_id:
        user_rating = aliased(Rating, name='user_rating')

        query = query.add_entity(user_rating).\
            outerjoin(user_rating, and_(user_rating.exercise_id == Exercise.id,
                                        user_rating.user_id == user_id))

        if order_by == 'user_rating':
            query = query.order_by(nullslast(orderfunc(user_rating.rating)))
        elif order_by == 'user_fun_rating':
            query = query.order_by(nullslast(orderfunc(user_rating.fun)))
        elif order_by == 'user_effective_rating':
            query = query.order_by(nullslast(orderfunc(user_rating.effective)))
        elif order_by == 'user_clear_rating':
            query = query.order_by(nullslast(orderfunc(user_rating.clear)))

        # when if favorited_by is not None then we only want the user favorites
        # and isouter will be set to False. Meaning we will do an inner join If
        # favorited_by is None then isouter will be True and we will do an
        # outer join meaning we want to know which exercises the user favorited
        # but we want all of them.
        isouter = not bool(favorited_by)

        # include a column from the UserFavoriteExercise table or `0`.
        # this will get serialized as a Boolean to signify favorited or not.
        query = query.\
            add_columns(func.coalesce(UserFavoriteExercise.exercise_id, 0).label('favorited')).\
            join(UserFavoriteExercise,
                 and_(UserFavoriteExercise.exercise_id == Exercise.id,
                      UserFavoriteExercise.user_id == user_id),
                 isouter=isouter)

    if author:
        query = query.join(
            User, and_(User.id == Exercise.author_id, User.username == author))

    if category:
        category = parse_query_params(request.args, key='category')
        query = query.join(Category).filter(Category.name.in_(category))

    if 'author' in parse_query_params(request.args, key='expand'):
        query = query.options(joinedload(Exercise.author))

    if order_by in ['average_rating', 'rating']:
        query = query.order_by(nullslast(orderfunc(Exercise.avg_rating)))
    elif order_by == 'average_fun_rating':
        query = query.order_by(nullslast(orderfunc(Exercise.avg_fun_rating)))
    elif order_by == 'average_clear_rating':
        query = query.order_by(nullslast(orderfunc(Exercise.avg_clear_rating)))
    elif order_by == 'average_effective_rating':
        query = query.order_by(
            nullslast(orderfunc(Exercise.avg_effective_rating)))
    elif order_by == 'popularity':
        query = query.order_by(nullslast(orderfunc(Exercise.popularity)))
    elif order_by == 'updated_at':
        query = query.order_by(orderfunc(Exercise.updated_at))
    else:
        query = query.order_by(orderfunc(Exercise.created_at))

    page = Pagination(request, query=query)
    return Serializer(ExerciseSchema, request.args).dump_page(page)