def parse_invoices(entries, account): for entry in entries: i = entry['invoices'] invoice = db.query(Document).filter_by(uid=i['id']).first() partner = db.query(Partner).filter_by(uid=i['client_id']).first() if invoice is None: invoice = Document(uid=i['id'], account=account, tags=[typetags['invoice']]) db.add(invoice) else: u = list(set(invoice.tags).difference(set(tags.values()))) if u: invoice.tags = u invoice.name = i['subject'] invoice.value = i['amount'] invoice.date = datetime.strptime(i['issued_at'], '%Y-%m-%d') invoice.updated_at = datetime.strptime(i['updated_at'], '%Y-%m-%dT%H:%M:%SZ') invoice.meta = json.dumps(i) invoice.partner = partner invoice.tags.append(tags[i['state']]) if i['state'] == 'open' and datetime.strptime( i['due_at'], '%Y-%m-%d') < datetime.now(): invoice.tags.append(tags['due'])
def add_new_client(): body = json.loads(request.data) name = body.get('name', None) username = body.get('username', None) password = body.get('password', None) courriel = body.get('courriel', None) if name is None: return jsonify({'error': 'name cannot be null'}) if username is None: return jsonify({'error': 'username cannot be null'}) if password is None: return jsonify({'error': 'password cannot be null'}) if courriel is None: return jsonify({'error': 'courriel cannot be null'}) new_clients = Clients(name=name, username=username, password=password, courriel=courriel) db.add(new_client) return jsonify({'result': new_client._dump()})
def parse_document(record, account, typestring): document = db.query(Document).filter_by(uid=record['uid']).first() partner = get_or_create(Partner, name=record['meta']['client_name']) if document is None: document = Document(**record) document.account = account document.tags = [typetags[typestring]] db.add(document) else: u = list(set(document.tags).difference(set(tags.values()))) if u: document.tags = u document.name = record['name'] document.value = record['value'] document.date = record['date'] document.updated_at = datetime.now() document.meta = CustomJSONEncoder().encode(record['meta']) document.partner = partner document.tags.append(tags[record['meta']['state']]) if record['meta'][ 'state'] == 'open' and record['meta']['due_at'] < datetime.now(): document.tags.append(tags['due'])
def post(self): args = parser.parse_args() profile = db.query(Profile).filter_by(id=args['profile_id']).first() miner = db.query(Profile).filter_by(id=args['miner_id']).first() previous = db.query(Block).order_by('-id').first() data = { "message": profile.message, "feedback": args["feedback"], "initiator": profile.email, "miner": miner.email, "reward": 30 } nonce, hash, timestamp = Block.pow(data=json.dumps(data), previous_hash=previous.hash) block = Block(data=json.dumps(data), hash=hash, previous_hash=previous.hash, nonce=nonce, creation_date=timestamp) miner.thelmies += 30 profile.thelmies -= 5 # remove message from profile profile.message = "" db.add(block) db.commit() return jsonify(block)
def edit_task(): body = json.loads(request.data) task_key = body.get('_key', None) task = db.query(Tasks).by_key(task_key) if task is None: return jsonify({'message':'task not found'}) name = body.get('name',None) description = body.get('description', None) task.name = name task.description = description db.update(task) category_key = body.get('category_key', None) category = db.query(Categories).by_key(category_key) if category is not None: # find old category relation and delete it old_category_relation = db.query(Category_Relation).filter('_to==@_to',_to=task._id).first() db.delete(old_category_relation) # add new category relation db.add(task_graph.relation(relation_from=category, relation=Category_Relation(), relation_to=task)) assignee_key = body.get('assignee_key', None) assignee = db.query(People).by_key(assignee_key) if assignee is not None: # find old assignee relation and delete it old_assignee_relation = db.query(Assignee_Relation).filter('_from==@_from',_from=task._id).first() db.delete(old_assignee_relation) # add new assignee relation db.add(task_graph.relation(relation_from=task, relation=Assignee_Relation(), relation_to=assignee)) return jsonify({'result':task._dump()}), 200
def add_new_person(): body = json.loads(request.data) name = body.get('name',None) if name is None: return jsonify({'error':'name cannot be null'}) new_person = People(name=name) db.add(new_person) return jsonify({'result':new_person._dump()})
def add_new_category(): body = json.loads(request.data) name = body.get('name', None) if name is None: return jsonify({'error': 'name cannot be null'}) description = body.get('description', None) new_category = Categories(name=name, description=description) db.add(new_category) return jsonify({'result': new_category._dump()})
def parse_partners(entries): for entry in entries: c = entry['client'] partner = db.query(Partner).filter_by(uid=c['id']).first() if partner is None: partner = Partner(uid=c['id']) db.add(partner) partner.name = c['name'] partner.meta = json.dumps(c)
def edit_product(): body = json.loads(request.data) key = body.get('_key', None) if key is None: return jsonify({'error': 'key cannot be null'}) product = db.query(Products).by_key(key) if product is None: return jsonify({'error': 'product not found'}) # TODO this but programmatically if things change name = body.get('name', None) if name is not None: product.name = name description = body.get('description', None) if description is not None: product.description = description imageLocation = body.get('imageLocation', None) if imageLocation is not None: product.imageLocation = imageLocation price = body.get('price', None) if price is not None: product.price = price discount = body.get('discount', None) if discount is not None: product.discount = discount db.update(product) #relations category_key = body.get('category_key', None) category = db.query(Categories).by_key(category_key) if category is not None: # find old category relation and delete it old_category_relation = db.query(Category_Relation).filter( '_to==@_to', _to=product._id).first() db.delete(old_category_relation) # add new category relation db.add( product_graph.relation(relation_from=category, relation=Category_Relation(), relation_to=product)) shop_key = body.get('shop_key', None) shop = db.query(Shops).by_key(shop_key) if shop is not None: # find old assignee relation and delete it old_shop_relation = db.query(Shop_Relation).filter( '_from==@_from', _from=product._id).first() db.delete(old_shop_relation) # add new assignee relation db.add( product_graph.relation(relation_from=product, relation=Shop_Relation(), relation_to=shop)) return jsonify({'result': product._dump()})
def post(self): args = parser.parse_args() profile = Profile(email=args['email'], name=args['name'], password=hashlib.sha224( args['password'].encode('utf-8')).hexdigest(), thelmies=100) db.add(profile) db.commit() return jsonify(profile)
def generateFirst(self): data = '{"message": "TEX Event Genesis block"}' nonce, hash, timestamp = Block.pow(data=data, previous_hash="0") first = Block(data=data, hash=hash, previous_hash="0", nonce=nonce, creation_date=timestamp) # genesis = blockchain.genesis() db.add(first) db.commit()
def add_new_shop(): body = json.loads(request.data) name = body.get('name', None) if name is None: return jsonify({'error': 'name cannot be null'}) slogan = body.get('slogan', None) description = body.get('description', None) logoLocation = body.get('logoLocation', None) geolocation = body.get('geolocation', None) foundingDate = body.get('foundingDate', None) new_shop = Shops(name=name, slogan=slogan, description=description, logoLocation=logoLocation, geolocation=geolocation, foundingDate=foundingDate) db.add(new_shop) return jsonify({'result': new_shop._dump()})
def add_new_product(): body = json.loads(request.data) name = body.get('name', None) if name is None: return jsonify({'error': 'name cannot be null'}) description = body.get('description', None) imageLocation = body.get('imageLocation', None) price = body.get('price', None) discount = body.get('discount', None) new_product = Products(name=name, description=description, imageLocation=imageLocation, price=price, discount=discount) db.add(new_product) #add relations shop_key = body.get('shop_key', None) if shop_key is not None: shop = db.query(Shops).by_key(shop_key) if shop is not None: db.add(product_graph.relation(new_product, Shop_Relation(), shop)) category_key = body.get('category_key', None) if category_key is not None: category = db.query(Categories).by_key(category_key) if category is not None: db.add( product_graph.relation(relation_from=category, relation=Shop_Relation(), relation_to=new_product)) return jsonify({'result': new_product._dump()})
def add_new_task(): body = json.loads(request.data) title = body.get('title', None) description = body.get('description', None) due_date = body.get('due_date', None) if due_date is not None: due_date = datetime.strptime(due_date , '%Y-%m-%d').date() new_task = Tasks(title=title, description=description, due_date=due_date) db.add(new_task) # Add assignee relation assignee_key = body.get('assignee_key',None) assignee = db.query(People).by_key(assignee_key) if assignee_key is not None: db.add(task_graph.relation(new_task, Assignee_Relation(), assignee)) # Add category relation category_key = body.get('category_key', None) category = db.query(Categories).by_key(category_key) if category is not None: db.add(task_graph.relation(relation_from=category, relation=Category_Relation(), relation_to=new_task)) return jsonify({'result': new_task._dump()})
def add_categories(): body = json.loads(request.data) category_name = body.get('name', None) category = Categories(name=category_name) db.add(category) return jsonify({'result': category._dump()}) , 200