diff --git a/ordr2/models/__init__.py b/ordr2/models/__init__.py index 616e3ac..4a3497d 100644 --- a/ordr2/models/__init__.py +++ b/ordr2/models/__init__.py @@ -6,6 +6,7 @@ import zope.sqlalchemy # import or define all models here to ensure they are attached to the # Base.metadata prior to any initialization routines from .user import User, Role # flake8: noqa +from .orders import Category, Consumable # run configure_mappers after defining all of the models to ensure # all relationships can be setup diff --git a/ordr2/models/orders.py b/ordr2/models/orders.py new file mode 100644 index 0000000..fe2624c --- /dev/null +++ b/ordr2/models/orders.py @@ -0,0 +1,53 @@ +import bcrypt +import enum +import uuid + +from collections import namedtuple +from datetime import datetime +from sqlalchemy import ( + Column, + Date, + Enum, + Float, + Integer, + Text, + ) + +from .meta import Base + + +class Category(enum.Enum): + ''' Categories of consumables and orders ''' + CHEMICAL = 'chemical' + DISPOSABLE = 'disposable' + SOLVENT = 'solvent' + BIOLAB = 'biolab' + + +class Consumable(Base): + ''' A consumable ''' + + __tablename__ = 'consumables' + + id = Column(Integer, primary_key=True) + + cas_description = Column(Text, nullable=False, unique=True) + category = Column(Enum(Category), nullable=False) + catalog_nr = Column(Text, nullable=False) + vendor = Column(Text, nullable=False) + package_size = Column(Text, nullable=False) + unit_price = Column(Float, nullable=False) + currency = Column(Text, nullable=False, default='EUR') + comment = Column(Text, nullable=False, default='') + date_created = Column(Date, nullable=False, default=datetime.utcnow) + date_modified = Column( + Date, + nullable=False, + default=datetime.utcnow, + onupdate=datetime.utcnow + ) + + def __str__(self): + ''' string representation ''' + return '{!s} ({!s})'.format(self.cas_description, self.vendor) + diff --git a/ordr2/scripts/initializedb.py b/ordr2/scripts/initializedb.py index d82ec29..289543d 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 User, Role +from ..models import Category, Consumable, User, Role def usage(argv): @@ -83,3 +83,22 @@ def main(argv=sys.argv): dbsession.add(user) dbsession.flush() + cat_list = read_exported_file(base_dir, 'export_consumables.yml') + for data in tqdm(cat_list, desc='Consumables'): + category = Category[data['category'].upper()] + date_created = parse_dt(data['date_created']) + consumable = Consumable( + 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'], + currency=data['currency'], + comment=data['comment'], + date_created=date_created, + date_modified=parse_dt(data['date_modified'], date_created), + category=category + ) + dbsession.add(consumable) + dbsession.flush()