Add ethereum_address, receiver_address to deliverynote and PATCH it

This commit is contained in:
yiorgos marinellis 2020-03-03 13:10:42 +01:00
parent d9315c2a25
commit 3d96e8190d
3 changed files with 29 additions and 62 deletions

View File

@ -32,8 +32,13 @@ class Deliverynote(Thing):
nullable=False,
default=lambda: g.user.email)
supplier = db.relationship(User, primaryjoin=lambda: Deliverynote.supplier_email == User.email)
receiver_address = db.Column(CIText(),
db.ForeignKey(User.email))
# nullable=False)
receiver = db.relationship(User, primaryjoin=lambda: Deliverynote.receiver_address== User.email)
# supplier = db.relationship(User)
date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
date.comment = 'The date the DeliveryNote initiated'
# 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
@ -41,9 +46,10 @@ class Deliverynote(Thing):
# 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)
transferred_devices = db.Column(CIText(), nullable=True)
transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False)
transfer_state.comment = TransferState.__doc__
ethereum_address = db.Column(CIText(), unique=True, default=None)
lot_id = db.Column(UUID(as_uuid=True),
db.ForeignKey(Lot.id),
nullable=False)
@ -53,17 +59,15 @@ class Deliverynote(Thing):
primaryjoin=Lot.id == lot_id,
collection_class=set)
def __init__(self, document_id: str, deposit: str,
def __init__(self, document_id: str, deposit: str, date,
supplier_email: str,
expected_devices: str,
transferred_devices: str) -> None:
expected_devices: str) -> None:
"""Initializes a delivery note
"""
super().__init__(id=uuid.uuid4(),
document_id=document_id, deposit=deposit,
document_id=document_id, deposit=deposit, date=date,
supplier_email=supplier_email,
expected_devices=expected_devices,
transferred_devices=transferred_devices)
expected_devices=expected_devices)
@property
def type(self) -> str:
@ -74,47 +78,12 @@ class Deliverynote(Thing):
"""The URL where to GET this action."""
return urlutils.URL(url_for_resource(Deliverynote, item_id=self.id))
def delete(self):
"""Deletes the deliverynote.
# def delete(self):
# """Deletes the lot.
# This method removes the children lots and children
# devices orphan from this lot and then marks this lot
# for deletion.
# """
# self.remove_children(*self.children)
# db.session.delete(self)
# def _refresh_models_with_relationships_to_lots(self):
# session = db.Session.object_session(self)
# for model in session:
# if isinstance(model, (Device, Lot, Path)):
# session.expire(model)
# def __contains__(self, child: Union['Lot', Device]):
# if isinstance(child, Lot):
# return Path.has_lot(self.id, child.id)
# elif isinstance(child, Device):
# device = db.session.query(LotDeviceDescendants) \
# .filter(LotDeviceDescendants.device_id == child.id) \
# .filter(LotDeviceDescendants.ancestor_lot_id == self.id) \
# .one_or_none()
# return device
# else:
# raise TypeError('Lot only contains devices and lots, not {}'.format(child.__class__))
This method removes the delivery note.
"""
db.session.delete(self)
def __repr__(self) -> str:
# return '<Lot {0.name} devices={0.devices!r}>'.format(self)
return '<Deliverynote {0.documentID}>'.format(self)
# class LotDevice(db.Model):
# device_id = db.Column(db.BigInteger, db.ForeignKey(Device.id), primary_key=True)
# lot_id = db.Column(UUID(as_uuid=True), db.ForeignKey(Lot.id), primary_key=True)
# created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
# author_id = db.Column(UUID(as_uuid=True),
# db.ForeignKey(User.id),
# nullable=False,
# default=lambda: g.user.id)
# author = db.relationship(User, primaryjoin=author_id == User.id)
# author_id.comment = """The user that put the device in the lot."""

View File

@ -4,6 +4,7 @@ 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.device import schemas as s_device
from ereuse_devicehub.resources.models import STR_SIZE
from ereuse_devicehub.resources.schemas import Thing
from ereuse_devicehub.resources.enums import TransferState
@ -11,14 +12,16 @@ from ereuse_devicehub.resources.enums import TransferState
class Deliverynote(Thing):
id = f.UUID(dump_only=True)
document_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True)
document_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE),
required=True, data_key='documentID')
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')
date = f.DateTime('iso', required=True)
# 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)
transferred_devices = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=False)
transfer_state = EnumField(TransferState, description=m.Lot.transfer_state.comment)

View File

@ -41,19 +41,14 @@ class DeliverynoteView(View):
db.session.commit()
return ret
# 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()
# 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():
# setattr(dlvnote, key, value)
# if key in device_fields:
# for dev in computers:
# setattr(dev, key, value)
# db.session.commit()
# return Response(status=204)
def patch(self, id):
patch_schema = self.resource_def.SCHEMA(only=('transfer_state', 'receiver_address', 'supplier_email', 'ethereum_address'), partial=True)
d = request.get_json(schema=patch_schema)
dlvnote = Deliverynote.query.filter_by(id=id).one()
for key, value in d.items():
setattr(dlvnote, key, value)
db.session.commit()
return Response(status=204)
def one(self, id: uuid.UUID):
"""Gets one action."""