def trans_legal_verify(): wallet_address = request.args.get('remittee_address') session = DBSession() result = session.query(Project).filter( and_(Project.wallet_address == wallet_address, Project.status == 'In process')).first() session.close() if result: res = 'true' else: res = 'false' return jsonify(res)
def list(): type = request.args['type'] wallet_address = request.args['wallet_address'] session = DBSession() if type == 'all': # result = session.query(Transaction, Recipient).outerjoin(Recipient, # Transaction.remittee == Recipient.wallet_address).filter( # or_(Transaction.payer == wallet_address, Transaction.remittee == wallet_address)).all() result = session.query(Transaction).filter( or_(Transaction.payer == wallet_address, Transaction.remittee == wallet_address)).all() elif type == 'payer': # result = session.query(Transaction, Recipient).outerjoin(Recipient, # Transaction.remittee == Recipient.wallet_address).filter( # Transaction.payer == wallet_address).all() result = session.query(Transaction).filter( Transaction.payer == wallet_address).all() elif type == 'remittee': result = session.query(Transaction).filter( Transaction.remittee == wallet_address).all() else: session.close() return 'Wrong Transation Type', 400 session.close() print(result) # res = sorted([{'amount': trans[0].amount, # 'walletAddress': wallet_address, # 'payer': trans[0].payer, # 'remittee': trans[0].remittee, # 'height': trans[0].height, # 'trans_time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(trans[0].trans_time)), # 'txhash': trans[0].txhash, # # 'legal': 'true' if trans[1] else 'false' # 'legal': trans[0].legal # } for trans in result], key=lambda x: x['trans_time'], reverse=True) res = sorted([{ 'amount': trans.amount, 'walletAddress': wallet_address, 'payer': trans.payer, 'remittee': trans.remittee, 'height': trans.height, 'trans_time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(trans.trans_time)), 'txhash': trans.txhash, 'legal': trans.legal } for trans in result], key=lambda x: x['trans_time'], reverse=True) return jsonify(res)
def vested(): session = DBSession() try: result = session.query(Transaction).all() institusion = [ charity[0] for charity in session.query(Charity.wallet_address).all() ] except: session.close() return 'Wrong Transation Type', 400 session.close() trans = [] for tran in result: if tran.payer in institusion and tran.remittee not in institusion: trans.append(tran) res = sorted([{ 'amount': tran.amount, 'payer': tran.payer, 'remittee': tran.remittee, 'height': tran.height, 'trans_time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(tran.trans_time)), 'txhash': tran.txhash, 'legal': tran.legal } for tran in trans], key=lambda x: x['trans_time'], reverse=True) return jsonify(res)
def register(): wallet_address = request.json['wallet_address'] passwd = request.json['password'] session = DBSession() try: session.query(Recipient).filter( Recipient.wallet_address == wallet_address).update( {"password": passwd}) session.commit() session.close() return "Register Success", 201 except: session.close() return 'Error user_type', 400
def vested(): session = DBSession() try: result = session.query(Transaction, Charity).outerjoin( Charity, Transaction.remittee == Charity.wallet_address).filter( Charity.role == None).all() except: session.close() return 'Wrong Transation Type', 400 session.close() res = sorted([{ 'amount': trans[0].amount, 'payer': trans[0].payer, 'remittee': trans[0].remittee, 'height': trans[0].height, 'trans_time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime( trans[0].trans_time)), 'txhash': trans[0].txhash, 'legal': 'true' if trans[1] else 'false' } for trans in result], key=lambda x: x['trans_time'], reverse=True) return jsonify(res)
def confirm(): session = DBSession() try: project_id = request.args.get('project_id') confirm_project = session.query(Project).filter( Project.id == project_id).update({"status": 'In process'}) session.commit() session.close() if confirm_project: return "Confirm Project Success", 201 else: return "Confirm Project Failed", 400 except: session.close() return "Confirm Project Failed", 400
def get_project_detail(): project_id = request.args.get('project_id') session = DBSession() result = session.query(Project).filter(Project.id == project_id).first() session.close() if result: res = { 'id': result.id, 'name': result.name, 'walletAddress': result.wallet_address, 'money': result.money, 'title': result.title, 'state': result.status, 'current_money': result.current_money, 'content': result.content } return jsonify(res) else: return 'Project id not found', 400
def info(): user_type = request.args['role'] wallet_address = request.args.get('walletAddress') session = DBSession() if user_type == 'donator': result = session.query(Donator).filter( Donator.wallet_address == wallet_address).first() res = { 'role': 'donator', 'walletAddress': result.wallet_address, 'name': result.name, 'gender': result.gender, 'profession': result.profession, 'phone': result.phone, 'homeAddress': result.address } elif user_type == 'recipient': result = session.query(Recipient).filter( Recipient.wallet_address == wallet_address).first() res = { 'role': 'recipient', 'walletAddress': result.wallet_address, 'name': result.name, 'gender': result.gender, 'profession': result.profession, 'phone': result.phone, 'homeAddress': result.address } elif user_type == 'charity' or user_type == 'actuator' or user_type == 'provider': result = session.query(Charity).filter( Charity.wallet_address == wallet_address).first() res = { 'role': user_type, 'walletAddress': result.wallet_address, 'name': result.name, 'phone': result.phone, 'address': result.address, 'description': result.description } else: session.close() return 'Wrong UserType', 400 session.close() if result: return jsonify(res) else: return "Wallet_address not found", 401
def login(): wallet_address = request.args.get('walletAddress') role = request.args.get('role') session = DBSession() if role == 'donator': result = session.query(Donator).filter( Donator.wallet_address == wallet_address).first() elif role == 'recipient': result = session.query(Recipient).filter( Recipient.wallet_address == wallet_address).first() elif role == 'charity' or role == 'actuator' or role == 'provider': result = session.query(Charity).filter( Charity.wallet_address == wallet_address).first() else: session.close() return "Wrong Role", 400 if result: if role == 'donator' or role == 'recipient': res = { 'registered': 'true', 'info': { 'role': role, 'walletAddress': result.wallet_address, 'name': result.name, 'gender': result.gender, 'profession': result.profession, 'phone': result.phone, 'homeAddress': result.address } } else: res = { 'registered': 'true', 'info': { 'role': role, 'walletAddress': result.wallet_address, 'name': result.name, 'phone': result.phone, 'homeAddress': result.address, 'decription': result.description } } else: res = {'registered': 'false', 'info': {}} session.close() return jsonify(res)
def create(): session = DBSession() try: name = request.json['name'] money = request.json['money'] title = request.json['title'] content = request.json['content'] wallet_address = request.json['wallet_address'] project = Project(name=name, money=money, title=title, content=content, wallet_address=wallet_address, current_money=0, status='Create') session.add(project) session.commit() session.close() return "Create Project Success", 201 except: session.close() return "Create Project Failed", 400
def list(): type = request.args['type'] wallet_address = request.args['wallet_address'] session = DBSession() if type == 'all': result = session.query(Transaction).filter( or_(Transaction.payer == wallet_address, Transaction.remittee == wallet_address)).all() elif type == 'payer': result = session.query(Transaction).filter( Transaction.payer == wallet_address).all() elif type == 'remittee': result = session.query(Transaction).filter( Transaction.remittee == wallet_address).all() else: session.close() return 'Wrong Transation Type', 400 session.close() res = sorted([{ 'amount': trans.amount, 'payer': trans.payer, 'remittee': trans.remittee, 'height': trans.height, 'trans_time': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(trans.trans_time)), 'txhash': trans.txhash, 'legal': trans.legal } for trans in result], key=lambda x: x['trans_time'], reverse=True) return jsonify(res)
def list(): search_type = request.args['type'] session = DBSession() if search_type == 'all': result = session.query(Project).all() elif search_type == 'single': wallet_address = request.args['address'] result = session.query(Project).filter( Project.wallet_address == wallet_address).all() elif search_type == 'create': result = session.query(Project).filter( Project.status == 'Create').all() elif search_type == 'in_process': result = session.query(Project).filter( Project.status == 'In process').all() elif search_type == 'finish': result = session.query(Project).filter( Project.status == 'Finish').all() else: session.close() return 'Wrong Seaarch Type', 400 session.close() res = [{ 'id': project.id, 'name': project.name, 'walletAddress': project.wallet_address, 'money': project.money, 'title': project.title, 'current_money': project.current_money, 'state': project.status } for project in result] return jsonify(res)
def register(): user_type = request.json['role'] session = DBSession() if user_type == 'donator': donator = Donator(name=request.json['name'], gender=request.json['gender'], profession=request.json['profession'], phone=request.json['phone'], wallet_address=request.json['wallet_address'], address=request.json['address']) session.add(donator) session.commit() elif user_type == 'recipient': recipient = Recipient(name=request.json['name'], gender=request.json['gender'], profession=request.json['profession'], phone=request.json['phone'], wallet_address=request.json['wallet_address'], address=request.json['address']) session.add(recipient) session.commit() else: session.close() return 'Error user_type', 400 session.close() return "Register Success", 201
def update_trans_from_block(): try: pre_height = int(Config().get_value('CHAIN', 'height')) now_height = Network('local').get_block_count() for height in range(pre_height + 1, now_height): try: session = DBSession() timestamp = Network('local').get_block_by_height( height)['Header']['Timestamp'] trans_list = Network('local').get_contract_info_by_height( height) trans_orm_list = [] for trans in trans_list: notify = trans['Notify'] try: for trans_info in notify: if trans_info[ 'ContractAddress'] == '0100000000000000000000000000000000000000': project = session.query(Project).filter( Project.wallet_address == trans_info['States'][2]).first() institusion = session.query(Charity).filter( Charity.wallet_address == trans_info['States'][2]).first() if project: current_money = float( project.current_money) + float( trans_info['States'][3]) if current_money >= float(project.money): status = 'Finish' else: status = 'In process' session.query(Project).filter( Project.wallet_address == trans_info['States'][2]).update({ "current_money": current_money, 'status': status }) transaction = Transaction( txhash=trans['TxHash'], height=height, trans_time=timestamp, payer=trans_info['States'][1], remittee=trans_info['States'][2], amount=float(trans_info['States'][3]), legal='true' if project or institusion else 'false') trans_orm_list.append(transaction) except Exception as e: print('*' * 20) print(e) print(trans['TxHash']) print(height) session.add_all(trans_orm_list) session.commit() session.close() Config().set_value('CHAIN', 'height', str(height)) except Exception as e: print('=' * 20) print(e) print(height) except Exception as e: print(e)
from flask import Blueprint, request from tools.db import DBSession, Donator, Charity, Recipient, Project session = DBSession() session.query(Project).filter( Project.wallet_address == 'A15NzM9iE3VT9X8SGk5h3dii6GPFQh2vme').update( {"current_money": 11}) session.commit() session.close()