Beispiel #1
0
def pay_to_user():
    """The user account transfer

    Args:
        is_developer:Is it a developer
    """
    is_developer = g.form.is_developer.data
    if is_developer is True:
        send_user_wallet = g.user.username
        pay_password = g.user.password_hash
    else:
        send_user_wallet = g.form.send_user.data
        pay_password = g.form.pay_password.data

    recv_wallet_username = g.form.recv_user.data
    amount = g.form.amount.data

    try:
        server = get_jsonrpc_server()
        result = server.pay(send_user_wallet, pay_password,
                            recv_wallet_username, amount)
        if result.get('errcode') != 0:
            return result
    except:
        app.logger.error(formatter_error(traceback.format_exc()))
        return return_result(20206)
    return return_result()
Beispiel #2
0
def balance():
    """Check balances"""
    is_developer = g.form.is_developer.data
    if is_developer:
        username_wallet = g.user.username
        pay_password = g.user.password_hash
    else:
        username_wallet = g.form.username.data
        pay_password = g.form.pay_password.data

    try:
        server = get_jsonrpc_server()
        result = server.getbalance(username_wallet, pay_password)
        if result.get('errcode') != 0:
            return result
    except:
        app.logger.error(formatter_error(traceback.format_exc()))
        return return_result(20203)
    result = result.get('result')
    confirmed = result.get('confirmed', '0')
    unconfirmed = result.get('unconfirmed', '0')
    unmatured = result.get('unmatured', '0')
    total = result.get('total', '0')
    return return_result(result=dict(total=total,
                                     confirmed=confirmed,
                                     unconfirmed=unconfirmed,
                                     unmatured=unmatured))
Beispiel #3
0
def create_wallet():
    """Generate wallets for app users."""
    appkey = g.appkey
    username = g.form.username.data
    pay_password = g.form.pay_password.data
    try:
        server = get_jsonrpc_server()
        result = server.create(username, pay_password)
        if result.get('errcode') != 0:
            return result
        app_user = StatisticsAppUser(appkey=appkey, app_username=username)
        db.session.add(app_user)
        db.session.commit()
    except IntegrityError as ie:
        app.logger.error(formatter_error(ie))
    except:
        app.logger.error(formatter_error(traceback.format_exc()))
        return return_result(20204)

    return return_result()
Beispiel #4
0
def publish():
    """Release resources

    Args:
        status: 1:add, 2:update, 3:delete(enabled=False)
    """
    data = copy.deepcopy(g.form.data)
    data['appkey'] = g.appkey
    username_wallet = get_wallet_name(data['author'])
    pay_password = data.pop('pay_password')
    data['currency'] = app.config['PUBLISH_CURRENCY']
    data['bid'] = app.config['PUBLISH_BID']
    data['claim_name'] = generate_appkey()

    metadata = dict(title=data['title'],
                    author=data['author'],
                    tag=data['tags'],
                    description=data['des'],
                    language=data['language'] or 'en',
                    license=data['license'],
                    licenseUrl=data['license_url'],
                    nsfw=False,
                    preview=data['preview'],
                    thumbnail=data['thumbnail'])
    try:
        server = get_jsonrpc_server()
        result = server.publish(username_wallet, pay_password,
                                data['claim_name'], metadata,
                                data['content_type'], data['udfs_hash'],
                                data['currency'], data['price'], data['bid'],
                                None, None, True)
        if result.get('errcode') != 0:
            return result
    except:
        app.logger.error(formatter_error(traceback.format_exc()))
        return return_result(20201)
    result = result.get('result')
    data['fee'] = float(result.get('fee', 0))
    data['claim_id'] = result.get('claim_id')
    data['txid'] = result.get('txid')
    data['nout'] = int(result.get('nout', 0))
    if len(data['txid']) != 64:
        return return_result(20201, result=result)
    data['status'] = 1
    data['tags'] = save_tag(data['tags'])
    content = Content(**data)
    db.session.add(content)
    data.pop('tags')
    history = ContentHistory(**data)
    db.session.add(history)
    db.session.commit()
    return return_result(result=dict(id=content.id, claim_id=content.claim_id))
Beispiel #5
0
def delete():
    """Update published resources

    Args:
        status: 1:add, 2:update, 3:delete(DB: enabled=False; CHAIN:metadata['nsfw']=True)
    """

    content = Content.query.filter_by(id=g.form.id.data,
                                      appkey=g.appkey,
                                      enabled=True).first()
    if not content:
        return return_result(20007)

    metadata = dict(title=content.title,
                    author=content.author,
                    tag=[tag.name for tag in content.tags],
                    description=content.des,
                    language=content.language or 'en',
                    license=content.license,
                    licenseUrl=content.license_url,
                    nsfw=True,
                    preview=content.preview,
                    thumbnail=content.preview)
    try:
        server = get_jsonrpc_server()
        result = server.update_claim(
            get_wallet_name(content.author), g.form.pay_password.data,
            content.claim_name, content.claim_id, content.txid, content.nout,
            metadata, content.content_type, content.udfs_hash,
            content.currency, content.price, content.bid, None, None)
        if result.get('errcode') != 0:
            return result
    except:
        app.logger.error(formatter_error(traceback.format_exc()))
        return return_result(20208)

    result = result.get('result')
    content.txid = result.get('txid')
    content.nout = int(result.get('nout', 0))
    content.fee = float(result.get('fee', 0))
    content.status = 3
    content.enabled = False

    data = contenthistory_schema.dump(content).data
    data['appkey'] = g.appkey
    history = ContentHistory(**data)
    db.session.add(history)
    db.session.commit()
    return return_result(result=dict(num=1))
Beispiel #6
0
def consume():
    appkey = g.appkey
    customer = g.form.customer.data
    wallet_username = get_wallet_name(customer)
    claim_id = g.form.claim_id.data
    # Normal resource consumption
    customer_pay_password = g.form.customer_pay_password.data
    # Ad click
    author_pay_password = g.form.author_pay_password.data

    content = g.form.content
    price = content.price
    if content.author != customer and content.price != 0:  # Non-free resources
        cs = Consume.query.filter_by(claim_id=claim_id,
                                     customer=customer,
                                     appkey=appkey).first()
        if not cs:
            try:
                server = get_jsonrpc_server()
                if price >= 0:  # Normal
                    result = server.consume(wallet_username,
                                            customer_pay_password, claim_id)
                else:  # Ad
                    send_wallet_username = get_wallet_name(content.author)
                    abs_price = abs(float(price))  # decimal to float
                    result = server.pay(send_wallet_username,
                                        author_pay_password, wallet_username,
                                        abs_price)
                if result.get('errcode') != 0:
                    return result
            except:
                app.logger.error(formatter_error(traceback.format_exc()))
                return return_result(20202)
            # app.logger.debug(result)
            result = result.get('result')
            txid = result.get('txid')
            if len(txid) != 64:
                return return_result(20202, result=result)
            c = Consume(txid=txid,
                        claim_id=claim_id,
                        customer=customer,
                        appkey=appkey,
                        price=price)
            db.session.add(c)
            db.session.commit()
    return return_result(result=dict(udfs_hash=content.udfs_hash))
Beispiel #7
0
def change_password():
    user = g.user
    old_password_hash = user.password_hash

    password = g.form.password.data
    if not user.check_password(password):
        return return_result(20004)

    new = g.form.new_password.data
    new_password_hash = generate_password_hash(new)
    try:
        server = get_jsonrpc_server()
        result = server.password(user.username, old_password_hash,
                                 new_password_hash)
        if result.get('errcode') != 0:
            return result
    except:
        app.logger.error(formatter_error(traceback.format_exc()))
        return return_result(20207)

    user.password = new_password_hash  # update password, auto commit
    db.session.commit()
    return return_result()
Beispiel #8
0
def reg():
    """User register"""
    form = g.form
    # The user login password is also the wallet's payment password.
    password = form.password.data
    pay_password = generate_password_hash(password)
    form.password.data = pay_password

    try:
        server = get_jsonrpc_server()
        result = server.create(form.username.data, pay_password)
        if result.get('errcode') != 0:
            return result
    except:
        app.logger.error(formatter_error(traceback.format_exc()))
        return return_result(20205)

    role = Role.query.filter_by(name='normal').first()
    user = User(**form.data)
    user.role_id = role.id
    db.session.add(user)
    db.session.commit()
    token = user.generate_auth_token(expiration=app.config['EXPIRATION'])
    return return_result(result=dict(id=user.id, token=token))
Beispiel #9
0
def update():
    """Update published resources

    Args:
        status: 1:add, 2:update, 3:delete(enabled=False)
    """
    data = copy.deepcopy(g.form.data)
    _id = data.pop('id')
    pay_password = data.pop('pay_password')
    content = Content.query.filter_by(id=_id, appkey=g.appkey,
                                      enabled=True).first()
    if not content:
        return return_result(20007)
    content.currency = app.config['PUBLISH_CURRENCY']
    content.bid = app.config['PUBLISH_BID']

    for k, v in data.items():
        if k in ['id', 'pay_password']:
            continue
        if v:
            if k == 'tags':
                v = save_tag(v)
            setattr(content, k, v)

    metadata = dict(title=content.title,
                    author=content.author,
                    tag=[tag.name for tag in content.tags],
                    description=content.des,
                    language=content.language or 'en',
                    license=content.license,
                    licenseUrl=content.license_url,
                    nsfw=False,
                    preview=content.preview,
                    thumbnail=content.thumbnail)
    try:
        server = get_jsonrpc_server()
        result = server.update_claim(
            get_wallet_name(content.author), pay_password, content.claim_name,
            content.claim_id, content.txid, content.nout, metadata,
            content.content_type, content.udfs_hash, content.currency,
            content.price, content.bid, None, None)
        if result.get('errcode') != 0:
            return result
    except:
        app.logger.error(formatter_error(traceback.format_exc()))
        return return_result(20208)

    result = result.get('result')
    content.txid = result.get('txid')
    content.nout = int(result.get('nout', 0))
    content.fee = float(result.get('fee', 0))
    content.status = 2
    if len(content.txid) != 64:
        return return_result(20208, result=result)

    data = contenthistory_schema.dump(content).data
    data['appkey'] = g.appkey
    history = ContentHistory(**data)
    db.session.add(history)
    db.session.commit()
    return return_result(result=dict(id=content.id, claim_id=content.claim_id))