From d9315c2a259586f376e082a520733ef02c872902 Mon Sep 17 00:00:00 2001 From: Big Lebowski Date: Sat, 29 Feb 2020 22:58:49 +0100 Subject: [PATCH] Create, get, list collection for Deliverynote --- .gitignore | 4 +++ .../resources/deliverynote/__init__.py | 5 ++- .../resources/deliverynote/models.py | 34 ++++++++++++++----- .../resources/deliverynote/schemas.py | 4 ++- .../resources/deliverynote/views.py | 12 +++++-- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index e5012c76..91073ab6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,10 @@ __pycache__/ *.py[cod] *$py.class +# Vim swap files +*.swp +*.swo + # C extensions *.so diff --git a/ereuse_devicehub/resources/deliverynote/__init__.py b/ereuse_devicehub/resources/deliverynote/__init__.py index 75a70f75..b1c0d130 100644 --- a/ereuse_devicehub/resources/deliverynote/__init__.py +++ b/ereuse_devicehub/resources/deliverynote/__init__.py @@ -11,8 +11,8 @@ from ereuse_devicehub.resources.deliverynote.views import DeliverynoteView class DeliverynoteDef(Resource): SCHEMA = schemas.Deliverynote VIEW = DeliverynoteView - # AUTH = True - AUTH = False + AUTH = True + # AUTH = False ID_CONVERTER = Converters.uuid def __init__(self, app, @@ -25,6 +25,5 @@ class DeliverynoteDef(Resource): url_defaults=None, root_path=None, cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()): - import pdb; pdb.set_trace() super().__init__(app, import_name, static_folder, static_url_path, template_folder, url_prefix, subdomain, url_defaults, root_path, cli_commands) diff --git a/ereuse_devicehub/resources/deliverynote/models.py b/ereuse_devicehub/resources/deliverynote/models.py index 18333e7a..221b3478 100644 --- a/ereuse_devicehub/resources/deliverynote/models.py +++ b/ereuse_devicehub/resources/deliverynote/models.py @@ -15,17 +15,25 @@ from teal.resource import url_for_resource from ereuse_devicehub.db import create_view, db, exp, f from ereuse_devicehub.resources.models import Thing from ereuse_devicehub.resources.user.models import User +from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.enums import TransferState class Deliverynote(Thing): id = db.Column(UUID(as_uuid=True), primary_key=True) # uuid is generated on init by default - documentID = db.Column(CIText(), nullable=False) - creator_id = Column(Integer, ForeignKey(User.id)) + document_id = db.Column(CIText(), nullable=False) + creator_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), + nullable=False, + default=lambda: g.user.id) creator = db.relationship(User, primaryjoin=creator_id == User.id) - supplier_id = Column(Integer, ForeignKey(User.id)) - supplier = db.relationship(User, primaryjoin=supplier_id == User.id) - date = db.Column(db.DateTime, nullable=False) + supplier_email = db.Column(CIText(), + db.ForeignKey(User.email), + nullable=False, + default=lambda: g.user.email) + supplier = db.relationship(User, primaryjoin=lambda: Deliverynote.supplier_email == User.email) + # supplier = db.relationship(User) + date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) # deposit = db.Column(db.Integer, check_range('deposit', min=0, max=100), default=0) deposit = db.Column(CIText(), nullable=False) # The following fiels are supposed to be 0:N relationships @@ -36,16 +44,26 @@ class Deliverynote(Thing): transferred_devices = db.Column(CIText(), nullable=False) transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False) transfer_state.comment = TransferState.__doc__ + lot_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(Lot.id), + nullable=False) lots = db.relationship(Lot, backref=db.backref('deliverynotes', lazy=True, collection_class=set), - secondary=lambda: LotDevice.__table__, lazy=True, + primaryjoin=Lot.id == lot_id, collection_class=set) - def __init__(self) -> None: + def __init__(self, document_id: str, deposit: str, + supplier_email: str, + expected_devices: str, + transferred_devices: str) -> None: """Initializes a delivery note """ - super().__init__(id=uuid.uuid4()) + super().__init__(id=uuid.uuid4(), + document_id=document_id, deposit=deposit, + supplier_email=supplier_email, + expected_devices=expected_devices, + transferred_devices=transferred_devices) @property def type(self) -> str: diff --git a/ereuse_devicehub/resources/deliverynote/schemas.py b/ereuse_devicehub/resources/deliverynote/schemas.py index c6f9bf2a..7cabb6bd 100644 --- a/ereuse_devicehub/resources/deliverynote/schemas.py +++ b/ereuse_devicehub/resources/deliverynote/schemas.py @@ -6,13 +6,15 @@ from ereuse_devicehub.resources.deliverynote import models as m from ereuse_devicehub.resources.user import schemas as s_user from ereuse_devicehub.resources.models import STR_SIZE from ereuse_devicehub.resources.schemas import Thing +from ereuse_devicehub.resources.enums import TransferState class Deliverynote(Thing): id = f.UUID(dump_only=True) - documentID = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True) + document_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True) url = URL(dump_only=True, description=m.Deliverynote.url.__doc__) creator = NestedOn(s_user.User,only_query='id') + supplier_email = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True) supplier = NestedOn(s_user.User,only_query='id') # deposit = f.Integer(validate=f.validate.Range(min=0, max=100), # description=m.Lot.deposit.__doc__) diff --git a/ereuse_devicehub/resources/deliverynote/views.py b/ereuse_devicehub/resources/deliverynote/views.py index 75610cd9..c0786134 100644 --- a/ereuse_devicehub/resources/deliverynote/views.py +++ b/ereuse_devicehub/resources/deliverynote/views.py @@ -15,6 +15,7 @@ from sqlalchemy.orm import joinedload from ereuse_devicehub.db import db from ereuse_devicehub.query import things_response from ereuse_devicehub.resources.deliverynote.models import Deliverynote +from ereuse_devicehub.resources.lot.models import Lot class DeliverynoteView(View): @@ -25,8 +26,14 @@ class DeliverynoteView(View): search = f.Str(missing=None) def post(self): - l = request.get_json() - dlvnote = Deliverynote(**l) + # Create delivery note + dn = request.get_json() + dlvnote = Deliverynote(**dn) + # Create a lot + lot_name = dlvnote.supplier_email + "_" + datetime.datetime.utcnow().strftime("%B-%d-%Y") + new_lot = Lot(name=lot_name) + dlvnote.lot_id = new_lot.id + db.session.add(new_lot) db.session.add(dlvnote) db.session().final_flush() ret = self.schema.jsonify(dlvnote) @@ -50,7 +57,6 @@ class DeliverynoteView(View): def one(self, id: uuid.UUID): """Gets one action.""" - import pdb; pdb.set_trace() deliverynote = Deliverynote.query.filter_by(id=id).one() # type: Deliverynote return self.schema.jsonify(deliverynote)