Ejemplo n.º 1
0
def app_list(page, num):
    uapps = g.user.app.order_by(Application.create_timed.desc()).paginate(
        page, num, error_out=False)
    records = apps_schema.dump(uapps.items).data
    records = add_timestamp(records)
    return return_result(
        result=dict(total=uapps.total, pages=uapps.pages, records=records))
Ejemplo n.º 2
0
def gets():
    appkey = g.appkey
    ids = g.form.ids.data
    query = Content.query.filter(Content.appkey == appkey,
                                 Content.enabled == True)
    if ids:
        query = query.filter(Content.id.in_(ids))
    contents = query.all()
    records = contents_schema.dump(contents).data
    records = add_timestamp(records)
    return return_result(result=records)
Ejemplo n.º 3
0
def claim_id_consume(page, num):
    """All relevant consumption records of a resource"""
    appkey = g.appkey
    claim_id = g.form.claim_id.data

    query = Consume.query.filter_by(appkey=appkey, claim_id=claim_id)
    consumes = query.order_by(Consume.create_timed.desc()).paginate(
        page, num, error_out=False)
    total = consumes.total
    pages = consumes.pages
    records = consumes_schema.dump(consumes.items).data
    records = add_timestamp(records)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 4
0
def userlist(page, num):
    username = g.form.username.data
    if username is None:
        users = User.query
    else:
        users = User.query.filter(User.username.like('%{}%'.format(username)))

    users = users.order_by(User.id.desc()).paginate(page, num, error_out=False)
    total = users.total
    pages = users.pages
    result = users_schema.dump(users.items).data
    records = add_timestamp(result)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 5
0
def published(page, num):
    """List of published resources"""
    appkey = g.appkey
    author = g.form.author.data
    contents = Content.query.filter_by(author=author,
                                       appkey=appkey,
                                       enabled=True).paginate(page,
                                                              num,
                                                              error_out=False)
    total = contents.total
    pages = contents.pages
    records = contents_schema.dump(contents.items).data
    records = add_timestamp(records)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 6
0
def account_inout(page, num):
    """用户收支账单(将account_in与account_out合二为一)"""
    appkey = g.appkey
    username = request.json.get('username')
    records = Content.query.with_entities(Content.claim_id, Content.author, Content.title, Consume.txid,
                    Consume.customer, Consume.price, Consume.create_timed). \
                    join(Consume, Content.claim_id == Consume.claim_id). \
                    filter(Content.appkey==appkey). \
                    filter((Content.author == username)|(Consume.customer==username)). \
                    order_by(Consume.create_timed.desc()). \
                    paginate(page, num, error_out=False)
    total = records.total
    pages = records.pages
    records = add_timestamp(records.items)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 7
0
def account_out(page, num):
    """支出 (分为 消费者消费支出/发布者广告支出)"""
    appkey = g.appkey
    username = request.json.get('username')
    # with_entities的field可以使用lable指定别名
    records = Content.query.with_entities(Content.claim_id, Content.author, Content.title, Consume.txid,
                    Consume.customer, db.func.abs(Consume.price).label('price'), Consume.create_timed). \
                    join(Consume, Content.claim_id == Consume.claim_id). \
                    filter(Content.appkey==appkey). \
                    filter(db.and_(Content.author == username,Consume.price<0)|
                           db.and_(Consume.customer==username,Consume.price>0)). \
                    order_by(Consume.create_timed.desc()). \
                    paginate(page, num, error_out=False)
    total = records.total
    pages = records.pages
    records = add_timestamp(records.items)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 8
0
def account_out(page, num):
    """expenditure statement

    Consumer Resource Expenditure and Publisher Ad Spending
    """
    appkey = g.appkey
    username = request.json.get('username')
    records = Content.query.with_entities(Content.claim_id, Content.author, Content.title, Consume.txid,
                    Consume.customer, db.func.abs(Consume.price).label('price'), Consume.create_timed). \
                    join(Consume, Content.claim_id == Consume.claim_id). \
                    filter(Content.appkey==appkey). \
                    filter(db.and_(Content.author == username,Consume.price<0)|
                           db.and_(Consume.customer==username,Consume.price>0)). \
                    order_by(Consume.create_timed.desc()). \
                    paginate(page, num, error_out=False)
    total = records.total
    pages = records.pages
    records = add_timestamp(records.items)
    return return_result(result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 9
0
def content_list(page, num):
    """因为price使用了Numeric类型, 在转换为python对象时,对应 Decimal 类型
    Decimal类型,在使用jsonify进行json转换时会发生异常, 这是因为标准库json不支持转换Decimal

    解决办法: 安装simplejson, 此库的dumps有一个use_decimal参数(默认True,可以转换Decimal)
    当你安装了simplejson, flask的jsonify会自动使用它,而放弃系统标准库中的json库
    ref: https://github.com/pallets/flask/issues/835
    """
    current_app.logger.info('aaaa')
    appkey = g.appkey
    contents = Content.query.filter(
        Content.appkey == appkey, Content.enabled == True).order_by(
            Content.create_timed.desc()).paginate(page, num, error_out=False)
    total = contents.total
    pages = contents.pages
    result = contents_schema.dump(contents.items).data
    records = add_timestamp(result)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 10
0
def published(page, num):
    """作者已发布资源被消费记录"""
    appkey = g.appkey
    author = request.json.get('author')
    category = request.json.get('category')  # 0: 资源收入 1: 广告支出 其他:all

    query = Content.query.with_entities(Content.id, Content.title, Consume.txid, Content.enabled,
                                        Content.claim_id, Consume.customer,Consume.price, Consume.create_timed). \
                        join(Consume,Content.claim_id==Consume.claim_id). \
                        filter(Content.appkey == appkey, Content.author==author)
    if category == 0:
        query = query.filter(Consume.price > 0)
    if category == 1:
        query = query.filter(Consume.price < 0)

    records = query.order_by(Consume.create_timed.desc()).paginate(
        page, num, error_out=False)
    total = records.total
    pages = records.pages
    records = add_timestamp(records.items)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 11
0
def account_inout(page, num):
    """Income and expenditure statement

    account_in and account_out in one
    """
    appkey = g.appkey
    username = g.form.username.data
    records = Content.query.with_entities(
        Content.claim_id, Content.author, Content.title, Consume.txid,
        Consume.customer, Consume.price, Consume.create_timed).join(
            Consume, Content.claim_id == Consume.claim_id).filter(
                Content.appkey == appkey).filter(
                    (Content.author == username)
                    | (Consume.customer == username)).order_by(
                        Consume.create_timed.desc()).paginate(page,
                                                              num,
                                                              error_out=False)
    total = records.total
    pages = records.pages
    records = add_timestamp(records.items)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 12
0
def published(page, num):
    """作者已发布资源被消费记录"""
    appkey = g.appkey
    author = g.form.author.data
    category = g.form.category.data  # 0: Publish income 1: Advertising spending other:all

    query = Content.query.with_entities(Content.id, Content.title, Consume.txid, Content.enabled,
                                        Content.claim_id, Consume.customer,Consume.price, Consume.create_timed). \
                        join(Consume,Content.claim_id==Consume.claim_id). \
                        filter(Content.appkey == appkey, Content.author==author)
    if category == 0:
        query = query.filter(Consume.price > 0)
    if category == 1:
        query = query.filter(Consume.price < 0)

    records = query.order_by(Consume.create_timed.desc()).paginate(
        page, num, error_out=False)
    total = records.total
    pages = records.pages
    records = add_timestamp(records.items)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 13
0
def account_in(page, num):
    """income statement

    Publisher resource income and consumer advertising revenue
    """
    appkey = g.appkey
    username = g.form.username.data
    records = Content.query.with_entities(
        Content.claim_id, Content.author, Content.title, Consume.txid,
        Consume.customer,
        db.func.abs(Consume.price).label('price'), Consume.create_timed).join(
            Consume, Content.claim_id == Consume.claim_id).filter(
                Content.appkey == appkey).filter(
                    db.and_(Content.author == username, Consume.price > 0)
                    | db.and_(Consume.customer == username, Consume.price < 0)
                ).order_by(Consume.create_timed.desc()).paginate(
                    page, num, error_out=False)
    total = records.total
    pages = records.pages
    records = add_timestamp(records.items)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 14
0
def account_out(page, num):
    """expenditure statement

    Consumer Resource Expenditure and Publisher Ad Spending

    Args:
        page: Show the page
        num: How many pages per page
        category: 0 - Resource spending, 1 - Advertising income
    """
    appkey = g.appkey
    username = g.form.username.data
    category = g.form.category.data
    sdate = g.form.sdate.data
    edate = g.form.edate.data
    query = Content.query.with_entities(
        Content.claim_id, Content.author, Content.title, Consume.txid,
        Consume.customer,
        db.func.abs(Consume.price).label('price'), Consume.create_timed).join(
            Consume, Content.claim_id == Consume.claim_id).filter(
                Content.appkey == appkey).filter(Consume.create_timed >= sdate,
                                                 Consume.create_timed <= edate)
    if category == 0:
        query = query.filter(Consume.customer == username, Consume.price > 0)
    elif category == 1:
        query = query.filter(Content.author == username, Consume.price < 0)
    else:
        query = query.filter(
            db.and_(Content.author == username, Consume.price < 0)
            | db.and_(Consume.customer == username, Consume.price > 0))
    records = query.order_by(Consume.create_timed.desc()).paginate(
        page, num, error_out=False)
    total = records.total
    pages = records.pages
    records = add_timestamp(records.items)
    return return_result(
        result=dict(total=total, pages=pages, records=records))
Ejemplo n.º 15
0
def consumed(page, num):
    """用户已消费的资源记录"""
    appkey = g.appkey
    customer = request.json.get('customer')
    category = request.json.get('category')  # 0: 消费支出 1: 广告收入 其他:all
    query = Content.query.with_entities(Content.id, Content.author, Content.title, Consume.txid, Content.enabled,
                                        Content.claim_id, Consume.price,
                                        Consume.create_timed). \
                        join(Consume,Content.claim_id==Consume.claim_id). \
                        filter(Content.appkey == appkey, Consume.customer == customer)
    if category == 0:
        query = query.filter(Consume.price > 0)
    if category == 1:
        query = query.filter(Consume.price < 0)

    records = query.order_by(Consume.create_timed.desc()).paginate(
        page, num, error_out=False)
    total = records.total
    pages = records.pages
    # 联表查询的结果是sqlalchemy.util._collections.result对象, 不是一个model对象
    # 所以不能够想content_list接口那样格式化数据
    records = add_timestamp(records.items)
    return return_result(
        result=dict(total=total, pages=pages, records=records))