Create, get, list collection for Deliverynote

This commit is contained in:
Big Lebowski 2020-02-29 22:58:49 +01:00
parent 48f7201e3f
commit d9315c2a25
5 changed files with 44 additions and 15 deletions

4
.gitignore vendored
View File

@ -3,6 +3,10 @@ __pycache__/
*.py[cod] *.py[cod]
*$py.class *$py.class
# Vim swap files
*.swp
*.swo
# C extensions # C extensions
*.so *.so

View File

@ -11,8 +11,8 @@ from ereuse_devicehub.resources.deliverynote.views import DeliverynoteView
class DeliverynoteDef(Resource): class DeliverynoteDef(Resource):
SCHEMA = schemas.Deliverynote SCHEMA = schemas.Deliverynote
VIEW = DeliverynoteView VIEW = DeliverynoteView
# AUTH = True AUTH = True
AUTH = False # AUTH = False
ID_CONVERTER = Converters.uuid ID_CONVERTER = Converters.uuid
def __init__(self, app, def __init__(self, app,
@ -25,6 +25,5 @@ class DeliverynoteDef(Resource):
url_defaults=None, url_defaults=None,
root_path=None, root_path=None,
cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()): 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, super().__init__(app, import_name, static_folder, static_url_path, template_folder,
url_prefix, subdomain, url_defaults, root_path, cli_commands) url_prefix, subdomain, url_defaults, root_path, cli_commands)

View File

@ -15,17 +15,25 @@ from teal.resource import url_for_resource
from ereuse_devicehub.db import create_view, db, exp, f from ereuse_devicehub.db import create_view, db, exp, f
from ereuse_devicehub.resources.models import Thing from ereuse_devicehub.resources.models import Thing
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.lot.models import Lot
from ereuse_devicehub.resources.enums import TransferState from ereuse_devicehub.resources.enums import TransferState
class Deliverynote(Thing): class Deliverynote(Thing):
id = db.Column(UUID(as_uuid=True), primary_key=True) # uuid is generated on init by default id = db.Column(UUID(as_uuid=True), primary_key=True) # uuid is generated on init by default
documentID = db.Column(CIText(), nullable=False) document_id = db.Column(CIText(), nullable=False)
creator_id = Column(Integer, ForeignKey(User.id)) 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) creator = db.relationship(User, primaryjoin=creator_id == User.id)
supplier_id = Column(Integer, ForeignKey(User.id)) supplier_email = db.Column(CIText(),
supplier = db.relationship(User, primaryjoin=supplier_id == User.id) db.ForeignKey(User.email),
date = db.Column(db.DateTime, nullable=False) 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(db.Integer, check_range('deposit', min=0, max=100), default=0)
deposit = db.Column(CIText(), nullable=False) deposit = db.Column(CIText(), nullable=False)
# The following fiels are supposed to be 0:N relationships # The following fiels are supposed to be 0:N relationships
@ -36,16 +44,26 @@ class Deliverynote(Thing):
transferred_devices = db.Column(CIText(), nullable=False) transferred_devices = db.Column(CIText(), nullable=False)
transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False) transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False)
transfer_state.comment = TransferState.__doc__ transfer_state.comment = TransferState.__doc__
lot_id = db.Column(UUID(as_uuid=True),
db.ForeignKey(Lot.id),
nullable=False)
lots = db.relationship(Lot, lots = db.relationship(Lot,
backref=db.backref('deliverynotes', lazy=True, collection_class=set), backref=db.backref('deliverynotes', lazy=True, collection_class=set),
secondary=lambda: LotDevice.__table__,
lazy=True, lazy=True,
primaryjoin=Lot.id == lot_id,
collection_class=set) 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 """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 @property
def type(self) -> str: def type(self) -> str:

View File

@ -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.user import schemas as s_user
from ereuse_devicehub.resources.models import STR_SIZE from ereuse_devicehub.resources.models import STR_SIZE
from ereuse_devicehub.resources.schemas import Thing from ereuse_devicehub.resources.schemas import Thing
from ereuse_devicehub.resources.enums import TransferState
class Deliverynote(Thing): class Deliverynote(Thing):
id = f.UUID(dump_only=True) 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__) url = URL(dump_only=True, description=m.Deliverynote.url.__doc__)
creator = NestedOn(s_user.User,only_query='id') 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') supplier = NestedOn(s_user.User,only_query='id')
# deposit = f.Integer(validate=f.validate.Range(min=0, max=100), # deposit = f.Integer(validate=f.validate.Range(min=0, max=100),
# description=m.Lot.deposit.__doc__) # description=m.Lot.deposit.__doc__)

View File

@ -15,6 +15,7 @@ from sqlalchemy.orm import joinedload
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.deliverynote.models import Deliverynote from ereuse_devicehub.resources.deliverynote.models import Deliverynote
from ereuse_devicehub.resources.lot.models import Lot
class DeliverynoteView(View): class DeliverynoteView(View):
@ -25,8 +26,14 @@ class DeliverynoteView(View):
search = f.Str(missing=None) search = f.Str(missing=None)
def post(self): def post(self):
l = request.get_json() # Create delivery note
dlvnote = Deliverynote(**l) 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.add(dlvnote)
db.session().final_flush() db.session().final_flush()
ret = self.schema.jsonify(dlvnote) ret = self.schema.jsonify(dlvnote)
@ -50,7 +57,6 @@ class DeliverynoteView(View):
def one(self, id: uuid.UUID): def one(self, id: uuid.UUID):
"""Gets one action.""" """Gets one action."""
import pdb; pdb.set_trace()
deliverynote = Deliverynote.query.filter_by(id=id).one() # type: Deliverynote deliverynote = Deliverynote.query.filter_by(id=id).one() # type: Deliverynote
return self.schema.jsonify(deliverynote) return self.schema.jsonify(deliverynote)