From 1d2f5ab0dd0c08a881cf5f8f8bba428bdfdd19bf Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Mon, 2 Oct 2017 17:18:07 +0200 Subject: [PATCH] added import of order data --- ordr2/scripts/initializedb.py | 43 +++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/ordr2/scripts/initializedb.py b/ordr2/scripts/initializedb.py index 289543d..d9993a5 100644 --- a/ordr2/scripts/initializedb.py +++ b/ordr2/scripts/initializedb.py @@ -21,7 +21,7 @@ from ..models import ( get_session_factory, get_tm_session, ) -from ..models import Category, Consumable, User, Role +from ..models import Category, Consumable, Order, OrderStatus, User, Role def usage(argv): @@ -38,9 +38,9 @@ def read_exported_file(*paths): return items -def parse_dt(value, default=None): - if value.startswith('0000'): - return default or datetime.utcnow() +def parse_dt(value, default=False): + if not value or value.startswith('0000'): + return datetime.utcnow() if default is False else default return datetime.strptime(value + ' -0000', '%Y-%m-%d %H:%M:%S %z') @@ -101,4 +101,37 @@ def main(argv=sys.argv): category=category ) dbsession.add(consumable) - dbsession.flush() + dbsession.flush() + + + order_list = read_exported_file(base_dir, 'export_orders.yml') + for data in tqdm(order_list, desc='Orders'): + work_status = data.get('work_status', 'approval') + status = OrderStatus[work_status.upper()] + category = Category[data['category'].upper()] + date_created = parse_dt(data['date_created']) + order = Order( + id=data['id'], + vendor=data['vendor'], + catalog_nr=data['catalog_number'], + cas_description=data['CAS_description'], + package_size=data['package_size'], + unit_price=data['price_unit'], + amount=data['quantity'], + total_price=data['price_total'], + account=data['account'], + currency=data['currency'], + comment=data['comment'], + created_date=date_created, + created_by=data['username'], + approval_date=parse_dt(data['date_modified'], None), + approval_by='', + ordered_date=parse_dt(data['date_ordered'], None), + ordered_by='', + completed_date=parse_dt(data['date_completed'], None), + completed_by='', + category=category, + status=status + ) + dbsession.add(order) + dbsession.flush()