示例#1
0
文件: banner.py 项目: yyywang/quota
def get_banners():
    form = PaginationForm().validate_for_api()
    per_page = current_app.config.get('COUNT_DEFAULT')

    banners = Banner.query.filter_by(delete_time=None).order_by(
        Banner._create_time.desc()).paginate(form.page.data, per_page)

    return jsonify(json_paginate(banners))
示例#2
0
文件: quota.py 项目: yyywang/quota
def get_quotas():
    """获取语录,分页返回"""
    form = PaginationForm().validate_for_api()
    per_page = current_app.config.get('COUNT_DEFAULT')

    paginate = Quota.query.filter_by(delete_time=None).order_by(
        Quota._create_time.desc()).paginate(form.page.data, per_page)

    return jsonify(json_paginate(paginate))
示例#3
0
def get_ads():
    form = GetAdsForm().validate_for_api()
    per_page = current_app.config['COUNT_DEFAULT']
    if form.category.data == '全部':
        ads = Ad.query.filter_by(delete_time=None).order_by(
            Ad._create_time.desc()).paginate(form.page.data, per_page)
    else:
        category = Category.query.filter_by(
            content=form.category.data).first_or_404()
        ads = Ad.query.with_parent(category).filter_by(
            delete_time=None).order_by(Ad._create_time.desc()).paginate(
                form.page.data, per_page)

    return jsonify(json_paginate(ads))
示例#4
0
文件: quota.py 项目: yyywang/quota
def get_quotas():
    """
    获取短句,9个短句+1个广告
    :return: 
    """
    form = GetQuotasForm().validate_for_api()
    per_page = 9

    # 随机获取短句
    if form.order_by.data == 'random':
        if form.category.data != '全部':
            category = Category.query.filter_by(
                content=form.category.data).first_or_404()
            items = Quota.query.with_parent(category).filter_by(
                delete_time=None).order_by(func.rand()).limit(9).all()
        else:
            items = Quota.query.filter_by(delete_time=None).order_by(
                func.rand()).limit(9).all()
        # 将语录格式整理为分页格式
        quotas = {
            'items': items,
            'has_next': True,
            'page': form.page.data,
            'next_page': form.page.data + 1
        }
    # 按发布时间降序获取短句
    elif form.order_by.data == 'desc':
        if form.category.data != '全部':
            category = Category.query.filter_by(
                content=form.category.data).first_or_404()
            # 若有分类参数,则按类别查数据
            quotas = Quota.query.with_parent(category).filter_by(
                delete_time=None).order_by(Quota._create_time.desc()).paginate(
                    form.page.data, per_page)
        else:
            quotas = Quota.query.filter_by(delete_time=None).order_by(
                Quota._create_time.desc()).paginate(form.page.data, per_page)
        quotas = json_paginate(quotas)
    else:
        raise ParameterException()

    ad = Ad.query.filter_by(delete_time=None).order_by(
        func.rand()).limit(1).first()
    if ad is not None:
        quotas['items'].append(ad)

    return jsonify(quotas)