def create_proposal_log(proposal_id, event_key, op_user_id, creator_id, op_time=None, from_value=None, to_value=None): proposal = Proposal.query.filter_by(id=proposal_id).first() if proposal == None: raise Exception("proposal id is not exist.") if op_time == None: op_time = datetime.utcnow() new_proposal_log = ProposalLog( proposal_id=proposal_id, event=ProposalLogEvent[event_key].value, op_user_id=op_user_id, op_time=op_time, from_value=from_value, to_value=to_value, creator_id=creator_id, ) save_changes(new_proposal_log)
def save_new_proposal_zone(data): proposal_zone = ProposalZone.query.filter_by(name=data['name']).first() if not proposal_zone: try: new_proposal_zone = ProposalZone( name=data['name'], token=data['token'], title=data['title'], summary=data['summary'], detail=data['detail'], theme_style=data['theme_style'], cover=data['cover'], vote_rule=data['vote_rule'], vote_addr_weight_json=data['vote_addr_weight_json'], creator_id=data['creator_id'], ) save_changes(new_proposal_zone) response_object = { 'status': 'success', 'message': 'Successfully create a new proposal zone.', } return response_object, 201 except Exception as e: response_object = {'status': 'fail', 'message': str(e)} return response_object, 400 else: response_object = { 'status': 'fail', 'message': 'Proposal zone name already exists.', } return response_object, 409
def save_new_proposal_zone(data): proposal_zone = ProposalZone.query.filter_by(name=data['name']).first() if not proposal_zone: try: new_proposal_zone = ProposalZone( name=data['name'], # token=data['token'], title=data['title'], summary=data['summary'], detail=data['detail'], theme_style=data.get('theme_style', None), theme_color=data.get('theme_color', None), cover=data.get('cover', None), vote_rule=data['vote_rule'], vote_addr_weight_json=data['vote_addr_weight_json'], creator_id=data['creator_id'], vote_duration_hours_min=data.get('vote_duration_hours_min', 24), vote_duration_hours_max=data.get('vote_duration_hours_max', 120), ) currency_ids = data.get('currency_ids', []) # add currency for c_id in currency_ids: currency = Currency.query.get(c_id) if currency is not None: new_proposal_zone.currencies.append(currency) save_changes(new_proposal_zone) response_object = { 'status': 'success', 'message': 'Successfully create a new proposal zone.', } return response_object, 201 except Exception as e: response_object = {'status': 'fail', 'message': str(e)} return response_object, 400 else: response_object = { 'status': 'fail', 'message': 'Proposal zone name already exists.', 'code': 409 } return response_object, 200
def save_new_user(data): user = User.query.filter((User.email == data['email'].lower()) | (User.username == data['username'])).first() if not user: new_user = User(public_id=str(uuid.uuid4()), email=data['email'].lower(), username=data['username'].lower(), password=data['password'], registered_on=datetime.datetime.utcnow()) save_changes(new_user) return generate_token(new_user) else: response_object = { 'status': 'fail', 'message': 'User email or username already exists. Please Log in.', } return response_object, 409
def add_user_wallet_addr(data): user_id = data.get('user_id', None) zone_id = data.get('zone_id', None) currency_id = data.get('currency_id', None) address = data.get('address', None) try: validate_res = validate_user_wallet_data(data) if validate_res[1] == False: return validate_res[0], 200 wallet = Wallet.query.filter_by(zone_id=zone_id, user_id=user_id, currency_id=currency_id).first() # check exist same record if wallet: response_object = { 'status': 'fail', 'message': 'already exist same zone and currency record.', } return response_object, 200 new_wallet = Wallet( user_id=user_id, zone_id=zone_id, currency_id=currency_id, address=address, ) save_changes(new_wallet) response_object = { 'status': 'success', 'message': 'add user wallet success.', } return response_object, 200 except Exception as e: print(e) response_object = {'status': 'fail', 'message': str(e)} return response_object, 401
def save_new_proposal(data): proposal_zone = ProposalZone.query.filter_by(id=data['zone_id']).first() if not proposal_zone: response_object = { 'status': 'fail', 'message': 'relate zone_id is not exists.', } return response_object, 404 try: # 当前 zone 内的 proposal 数量 zone_proposal_count = len(get_all_proposal_in_zone(data['zone_id'])) # 新的 proposal zone id new_zone_proposal_id = zone_proposal_count + 1 new_proposal = Proposal( zone_id=data['zone_id'], zone_proposal_id=new_zone_proposal_id, title=data['title'], amount=data['amount'], summary=data['summary'], status=100, # 创建成功,新创建的提案都是这个状态 detail=data['detail'], creator_id=data['creator_id'], currency_id=data['currency_id'], tag=data['tag'], ) save_changes(new_proposal) response_object = { 'status': 'success', 'message': 'Successfully create a new proposal.', } return response_object, 201 except Exception as e: print(e) response_object = {'status': 'fail', 'message': str(e)} return response_object, 401
def save_new_comment(data): # 判断 comment 关联的 proposal.id 是否存在 proposal = Proposal.query.filter_by(id=data['proposal_id']).first() if not proposal: response_object = { 'status': 'fail', 'message': 'relate proposal_id is not exists.', } return response_object, 404 try: if 'parent_id' in data: new_comment = Comment( proposal_id=data['proposal_id'], text=data['text'], creator_id=data['creator_id'], parent_id=data['parent_id'], ) else: # reply new_comment = Comment( proposal_id=data['proposal_id'], text=data['text'], creator_id=data['creator_id'], ) save_changes(new_comment) response_object = { 'status': 'success', 'message': 'Successfully create a new comment.', 'data': { 'id': new_comment.id, 'created': datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'), }, } return response_object, 201 except Exception as e: print(e) response_object = {'status': 'fail', 'message': str(e)} return response_object, 400
def save_new_category(_name, _name_en, _order, _creator_id): if not _name: response_object = { 'status': 'fail', 'message': 'name is required.', } return response_object, 404 try: validate_category_name(_name, _name_en) category = Category( name=_name, name_en=_name_en, order=_order, creator_id=_creator_id, ) save_changes(category) response_object = { 'status': 'success', 'message': 'Successfully add a new proposal category.', 'data': { 'id': category.id, 'name': category.name, 'name_en': category.name_en, } } return response_object, 200 except Exception as e: response_object = {'status': 'fail', 'message': str(e)} return response_object, 401
def save_new_proposal(data): # check 'zone_id' is exist or not proposal_zone = ProposalZone.query.filter_by(id=data['zone_id']).first() if not proposal_zone: response_object = { 'status': 'fail', 'message': 'relate zone_id is not exists.', } return response_object, 404 # check 'category_id' is exist or not category = Category.query.filter_by(id=data['category_id']).first() if not category: response_object = { 'status': 'fail', 'message': 'relate category_id is not exists.', } return response_object, 404 try: # 当前 zone 内的 proposal 数量 zone_proposal_count = get_all_proposals_count_in_zone(data['zone_id']) # 新的 proposal zone id new_zone_proposal_id = zone_proposal_count + 1 new_proposal = Proposal( zone_id=data['zone_id'], zone_proposal_id=new_zone_proposal_id, title=data['title'], category_id=data['category_id'], amount=data['amount'], summary=data['summary'], status=100, # 创建成功,新创建的提案都是这个状态 detail=data['detail'], creator_id=data['creator_id'], currency_id=data['currency_id'], tag=data['tag'], estimated_hours=data.get('estimated_hours', 0), vote_start_time=data.get('vote_start_time', datetime.utcnow()), vote_duration_hours=data.get('vote_duration_hours', 0), ) # create a onchain hash new_proposal.set_onchain_hash() save_changes(new_proposal) # add proposal log, create create_proposal_log(new_proposal.id, 'create', new_proposal.creator_id, new_proposal.creator_id) response_object = { 'status': 'success', 'data': new_proposal.id, 'message': 'Successfully create a new proposal.', } return response_object, 201 except Exception as e: print(e) response_object = {'status': 'fail', 'message': str(e)} return response_object, 400