Rename Deliverynote and add more fields, no tests

This commit is contained in:
yiorgos marinellis 2020-02-27 18:29:26 +01:00 committed by nad
parent b831a48b60
commit a387e7590c
4 changed files with 45 additions and 74 deletions

View File

@ -5,12 +5,12 @@ from teal.resource import Converters, Resource
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.resources.deliverynote import schemas from ereuse_devicehub.resources.deliverynote import schemas
from ereuse_devicehub.resources.deliverynote.views import DeliveryNoteView 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

View File

@ -18,67 +18,29 @@ from ereuse_devicehub.resources.user.models import User
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
# creator = db.relationship(User, primaryjoin=owner_address == User.ethereum_address)
documentID = db.Column(CIText(), nullable=False) documentID = db.Column(CIText(), nullable=False)
# supplier = db.relationship(User, primaryjoin=owner_address == User.ethereum_address) creator_id = Column(Integer, ForeignKey(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)
# 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)
# owner_address = db.Column(CIText(), deposit = db.Column(CIText(), nullable=False)
# db.ForeignKey(User.ethereum_address), # The following fiels are supposed to be 0:N relationships
# nullable=False, # to SnapshotDelivery entity.
# default=lambda: g.user.ethereum_address) # At this stage of implementation they will treated as a
# transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False) # comma-separated string of the devices expexted/transfered
# transfer_state.comment = TransferState.__doc__ expected_devices = db.Column(CIText(), nullable=False)
# lots = db.relationship(Lot, transferred_devices = db.Column(CIText(), nullable=False)
# backref=db.backref('deliverynotes', lazy=True, collection_class=set), transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False)
# secondary=lambda: LotDevice.__table__, transfer_state.comment = TransferState.__doc__
# lazy=True, lots = db.relationship(Lot,
# collection_class=set) backref=db.backref('deliverynotes', lazy=True, collection_class=set),
"""The **children** devices that the lot has. secondary=lambda: LotDevice.__table__,
lazy=True,
# Note that the lot can have more devices, if they are inside collection_class=set)
# descendant lots.
# """
# parents = db.relationship(lambda: Lot,
# viewonly=True,
# lazy=True,
# collection_class=set,
# secondary=lambda: LotParent.__table__,
# primaryjoin=lambda: Lot.id == LotParent.child_id,
# secondaryjoin=lambda: LotParent.parent_id == Lot.id,
# cascade='refresh-expire', # propagate changes outside ORM
# backref=db.backref('children',
# viewonly=True,
# lazy=True,
# cascade='refresh-expire',
# collection_class=set)
# )
# """The parent lots."""
# all_devices = db.relationship(Device,
# viewonly=True,
# lazy=True,
# collection_class=set,
# secondary=lambda: LotDeviceDescendants.__table__,
# primaryjoin=lambda: Lot.id == LotDeviceDescendants.ancestor_lot_id,
# secondaryjoin=lambda: LotDeviceDescendants.device_id == Device.id)
# """All devices, including components, inside this lot and its
# descendants.
# """
# deposit = db.Column(db.Integer, check_range('deposit', min=0, max=100), default=0)
# owner_address = db.Column(CIText(),
# db.ForeignKey(User.ethereum_address),
# nullable=False,
# default=lambda: g.user.ethereum_address)
# owner = db.relationship(User, primaryjoin=owner_address == User.ethereum_address)
# transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False)
# transfer_state.comment = TransferState.__doc__
# receiver_address = db.Column(CIText(),
# db.ForeignKey(User.ethereum_address),
# nullable=True)
# receiver = db.relationship(User, primaryjoin=receiver_address == User.ethereum_address)
# deliverynote_address = db.Column(CIText(), nullable=True)
def __init__(self) -> None: def __init__(self) -> None:
"""Initializes a delivery note """Initializes a delivery note
@ -92,7 +54,7 @@ class DeliveryNote(Thing):
@property @property
def url(self) -> urlutils.URL: def url(self) -> urlutils.URL:
"""The URL where to GET this action.""" """The URL where to GET this action."""
return urlutils.URL(url_for_resource(DeliveryNote, item_id=self.id)) return urlutils.URL(url_for_resource(Deliverynote, item_id=self.id))
# def delete(self): # def delete(self):
@ -125,7 +87,7 @@ class DeliveryNote(Thing):
def __repr__(self) -> str: def __repr__(self) -> str:
# return '<Lot {0.name} devices={0.devices!r}>'.format(self) # return '<Lot {0.name} devices={0.devices!r}>'.format(self)
return '<DeliveryNote {0.documentID}>'.format(self) return '<Deliverynote {0.documentID}>'.format(self)
# class LotDevice(db.Model): # class LotDevice(db.Model):

View File

@ -3,11 +3,20 @@ from teal.marshmallow import SanitizedStr, URL, EnumField
from ereuse_devicehub.marshmallow import NestedOn from ereuse_devicehub.marshmallow import NestedOn
from ereuse_devicehub.resources.deliverynote import models as m 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.models import STR_SIZE
from ereuse_devicehub.resources.schemas import Thing from ereuse_devicehub.resources.schemas import Thing
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) documentID = 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')
supplier = NestedOn(s_user.User,only_query='id')
# deposit = f.Integer(validate=f.validate.Range(min=0, max=100),
# description=m.Lot.deposit.__doc__)
deposit = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True)
expected_devices = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True)
transferred_devices = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True)
transfer_state = EnumField(TransferState, description=m.Lot.transfer_state.comment)

View File

@ -14,10 +14,10 @@ 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
class DeliveryNoteView(View): class DeliverynoteView(View):
class FindArgs(MarshmallowSchema): class FindArgs(MarshmallowSchema):
"""Allowed arguments for the ``find`` """Allowed arguments for the ``find``
method (GET collection) endpoint method (GET collection) endpoint
@ -26,7 +26,7 @@ class DeliveryNoteView(View):
def post(self): def post(self):
l = request.get_json() l = request.get_json()
dlvnote = DeliveryNote(**l) dlvnote = Deliverynote(**l)
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)
@ -37,7 +37,7 @@ class DeliveryNoteView(View):
# def patch(self, id): # def patch(self, id):
# patch_schema = self.resource_def.SCHEMA(only=('name', 'description', 'transfer_state', 'receiver_address', 'deposit', 'deliverynote_address', 'devices', 'owner_address'), partial=True) # patch_schema = self.resource_def.SCHEMA(only=('name', 'description', 'transfer_state', 'receiver_address', 'deposit', 'deliverynote_address', 'devices', 'owner_address'), partial=True)
# d = request.get_json(schema=patch_schema) # d = request.get_json(schema=patch_schema)
# dlvnote = DeliveryNote.query.filter_by(id=id).one() # dlvnote = Deliverynote.query.filter_by(id=id).one()
# device_fields = ['transfer_state', 'receiver_address', 'deposit', 'deliverynote_address', 'owner_address'] # device_fields = ['transfer_state', 'receiver_address', 'deposit', 'deliverynote_address', 'owner_address']
# computers = [x for x in dlvnote.all_devices if isinstance(x, Computer)] # computers = [x for x in dlvnote.all_devices if isinstance(x, Computer)]
# for key, value in d.items(): # for key, value in d.items():
@ -51,7 +51,7 @@ 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() 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)
@teal.cache.cache(datetime.timedelta(minutes=5)) @teal.cache.cache(datetime.timedelta(minutes=5))
@ -71,9 +71,9 @@ class DeliveryNoteView(View):
Otherwise it just returns the standard flat view of lots that Otherwise it just returns the standard flat view of lots that
you can filter. you can filter.
""" """
query = DeliveryNote.query query = Deliverynote.query
if args['search']: if args['search']:
query = query.filter(DeliveryNote.name.ilike(args['search'] + '%')) query = query.filter(Deliverynote.name.ilike(args['search'] + '%'))
dlvnote = query.paginate(per_page=6 if args['search'] else 30) dlvnote = query.paginate(per_page=6 if args['search'] else 30)
return things_response( return things_response(
self.schema.dump(dlvnote.items, many=True, nested=0), self.schema.dump(dlvnote.items, many=True, nested=0),
@ -81,7 +81,7 @@ class DeliveryNoteView(View):
) )
def delete(self, id): def delete(self, id):
dlvnote = DeliveryNote.query.filter_by(id=id).one() dlvnote = Deliverynote.query.filter_by(id=id).one()
dlvnote.delete() dlvnote.delete()
db.session.commit() db.session.commit()
return Response(status=204) return Response(status=204)