Rename Deliverynote and add more fields, no tests

This commit is contained in:
yiorgos marinellis 2020-02-27 18:29:26 +01:00
parent 46fcfa6d6d
commit 48f7201e3f
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.resources.deliverynote import schemas
from ereuse_devicehub.resources.deliverynote.views import DeliveryNoteView
from ereuse_devicehub.resources.deliverynote.views import DeliverynoteView
class DeliveryNoteDef(Resource):
SCHEMA = schemas.DeliveryNote
VIEW = DeliveryNoteView
class DeliverynoteDef(Resource):
SCHEMA = schemas.Deliverynote
VIEW = DeliverynoteView
# AUTH = True
AUTH = False
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
class DeliveryNote(Thing):
class Deliverynote(Thing):
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)
# 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)
# owner_address = db.Column(CIText(),
# db.ForeignKey(User.ethereum_address),
# nullable=False,
# default=lambda: g.user.ethereum_address)
# transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False)
# transfer_state.comment = TransferState.__doc__
# lots = db.relationship(Lot,
# backref=db.backref('deliverynotes', lazy=True, collection_class=set),
# secondary=lambda: LotDevice.__table__,
# lazy=True,
# collection_class=set)
"""The **children** devices that the lot has.
# Note that the lot can have more devices, if they are inside
# 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)
deposit = db.Column(CIText(), nullable=False)
# The following fiels are supposed to be 0:N relationships
# to SnapshotDelivery entity.
# At this stage of implementation they will treated as a
# comma-separated string of the devices expexted/transfered
expected_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.comment = TransferState.__doc__
lots = db.relationship(Lot,
backref=db.backref('deliverynotes', lazy=True, collection_class=set),
secondary=lambda: LotDevice.__table__,
lazy=True,
collection_class=set)
def __init__(self) -> None:
"""Initializes a delivery note
@ -92,7 +54,7 @@ class DeliveryNote(Thing):
@property
def url(self) -> urlutils.URL:
"""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):
@ -125,7 +87,7 @@ class DeliveryNote(Thing):
def __repr__(self) -> str:
# 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):

View File

@ -3,11 +3,20 @@ from teal.marshmallow import SanitizedStr, URL, EnumField
from ereuse_devicehub.marshmallow import NestedOn
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
class DeliveryNote(Thing):
class Deliverynote(Thing):
id = f.UUID(dump_only=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.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):
"""Allowed arguments for the ``find``
method (GET collection) endpoint
@ -26,7 +26,7 @@ class DeliveryNoteView(View):
def post(self):
l = request.get_json()
dlvnote = DeliveryNote(**l)
dlvnote = Deliverynote(**l)
db.session.add(dlvnote)
db.session().final_flush()
ret = self.schema.jsonify(dlvnote)
@ -37,7 +37,7 @@ class DeliveryNoteView(View):
# 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)
# 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']
# computers = [x for x in dlvnote.all_devices if isinstance(x, Computer)]
# for key, value in d.items():
@ -51,7 +51,7 @@ 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
deliverynote = Deliverynote.query.filter_by(id=id).one() # type: Deliverynote
return self.schema.jsonify(deliverynote)
@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
you can filter.
"""
query = DeliveryNote.query
query = Deliverynote.query
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)
return things_response(
self.schema.dump(dlvnote.items, many=True, nested=0),
@ -81,7 +81,7 @@ class DeliveryNoteView(View):
)
def delete(self, id):
dlvnote = DeliveryNote.query.filter_by(id=id).one()
dlvnote = Deliverynote.query.filter_by(id=id).one()
dlvnote.delete()
db.session.commit()
return Response(status=204)