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 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'])