Receiver, creator, deposit, expected/transferred devices constraints, remove GET, PATCH delivery note
This commit is contained in:
parent
3d96e8190d
commit
8e5511b587
|
@ -12,7 +12,6 @@ class DeliverynoteDef(Resource):
|
||||||
SCHEMA = schemas.Deliverynote
|
SCHEMA = schemas.Deliverynote
|
||||||
VIEW = DeliverynoteView
|
VIEW = DeliverynoteView
|
||||||
AUTH = True
|
AUTH = True
|
||||||
# AUTH = False
|
|
||||||
ID_CONVERTER = Converters.uuid
|
ID_CONVERTER = Converters.uuid
|
||||||
|
|
||||||
def __init__(self, app,
|
def __init__(self, app,
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
from boltons import urlutils
|
from boltons import urlutils
|
||||||
from citext import CIText
|
from citext import CIText
|
||||||
from flask import g
|
from flask import g
|
||||||
from sqlalchemy import TEXT, Enum as DBEnum
|
from sqlalchemy.types import ARRAY
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy_utils import LtreeType
|
from teal.db import CASCADE_OWN, check_range, IntEnum
|
||||||
from sqlalchemy_utils.types.ltree import LQUERY
|
|
||||||
from teal.db import CASCADE_OWN, UUIDLtree, check_range, IntEnum
|
|
||||||
from teal.resource import url_for_resource
|
from teal.resource import url_for_resource
|
||||||
|
|
||||||
from ereuse_devicehub.db import create_view, db, exp, f
|
from ereuse_devicehub.db import db, 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.lot.models import Lot
|
||||||
|
@ -33,20 +30,19 @@ class Deliverynote(Thing):
|
||||||
default=lambda: g.user.email)
|
default=lambda: g.user.email)
|
||||||
supplier = db.relationship(User, primaryjoin=lambda: Deliverynote.supplier_email == User.email)
|
supplier = db.relationship(User, primaryjoin=lambda: Deliverynote.supplier_email == User.email)
|
||||||
receiver_address = db.Column(CIText(),
|
receiver_address = db.Column(CIText(),
|
||||||
db.ForeignKey(User.email))
|
db.ForeignKey(User.email),
|
||||||
# nullable=False)
|
nullable=False,
|
||||||
receiver = db.relationship(User, primaryjoin=lambda: Deliverynote.receiver_address== User.email)
|
default=lambda: g.user.email)
|
||||||
# supplier = db.relationship(User)
|
receiver = db.relationship(User, primaryjoin=lambda: Deliverynote.receiver_address == User.email)
|
||||||
date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
|
date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
|
||||||
date.comment = 'The date the DeliveryNote initiated'
|
date.comment = 'The date the DeliveryNote initiated'
|
||||||
# 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)
|
# The following fields are supposed to be 0:N relationships
|
||||||
# The following fiels are supposed to be 0:N relationships
|
|
||||||
# to SnapshotDelivery entity.
|
# to SnapshotDelivery entity.
|
||||||
# At this stage of implementation they will treated as a
|
# At this stage of implementation they will treated as a
|
||||||
# comma-separated string of the devices expexted/transfered
|
# comma-separated string of the devices expexted/transfered
|
||||||
expected_devices = db.Column(CIText(), nullable=False)
|
expected_devices = db.Column(db.ARRAY(db.Integer, dimensions=1), nullable=False)
|
||||||
transferred_devices = db.Column(CIText(), nullable=True)
|
transferred_devices = db.Column(db.ARRAY(db.Integer, dimensions=1), nullable=True)
|
||||||
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__
|
||||||
ethereum_address = db.Column(CIText(), unique=True, default=None)
|
ethereum_address = db.Column(CIText(), unique=True, default=None)
|
||||||
|
|
|
@ -14,14 +14,15 @@ class Deliverynote(Thing):
|
||||||
id = f.UUID(dump_only=True)
|
id = f.UUID(dump_only=True)
|
||||||
document_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE),
|
document_id = SanitizedStr(validate=f.validate.Length(max=STR_SIZE),
|
||||||
required=True, data_key='documentID')
|
required=True, data_key='documentID')
|
||||||
url = URL(dump_only=True, description=m.Deliverynote.url.__doc__)
|
creator = NestedOn(s_user.User, dump_only=True)
|
||||||
creator = NestedOn(s_user.User,only_query='id')
|
supplier_email = SanitizedStr(validate=f.validate.Length(max=STR_SIZE),
|
||||||
supplier_email = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True)
|
load_only=True, required=True)
|
||||||
supplier = NestedOn(s_user.User,only_query='id')
|
supplier = NestedOn(s_user.User, dump_only=True)
|
||||||
|
receiver = NestedOn(s_user.User, dump_only=True)
|
||||||
date = f.DateTime('iso', required=True)
|
date = f.DateTime('iso', required=True)
|
||||||
# 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.Deliverynote.deposit.__doc__)
|
||||||
deposit = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True)
|
ethereum_address = f.String(description='User identifier address inside the Blockchain')
|
||||||
expected_devices = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=True)
|
expected_devices = f.List(f.Integer(), required=True, data_key='expectedDevices')
|
||||||
transferred_devices = SanitizedStr(validate=f.validate.Length(max=STR_SIZE), required=False)
|
transferred_devices = f.List(f.Integer(), required=False, data_key='transferredDevices')
|
||||||
transfer_state = EnumField(TransferState, description=m.Lot.transfer_state.comment)
|
transfer_state = EnumField(TransferState, description=m.Lot.transfer_state.comment)
|
||||||
|
|
|
@ -19,11 +19,6 @@ from ereuse_devicehub.resources.lot.models import Lot
|
||||||
|
|
||||||
|
|
||||||
class DeliverynoteView(View):
|
class DeliverynoteView(View):
|
||||||
class FindArgs(MarshmallowSchema):
|
|
||||||
"""Allowed arguments for the ``find``
|
|
||||||
method (GET collection) endpoint
|
|
||||||
"""
|
|
||||||
search = f.Str(missing=None)
|
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
# Create delivery note
|
# Create delivery note
|
||||||
|
@ -42,47 +37,13 @@ class DeliverynoteView(View):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def patch(self, id):
|
def patch(self, id):
|
||||||
patch_schema = self.resource_def.SCHEMA(only=('transfer_state', 'receiver_address', 'supplier_email', 'ethereum_address'), partial=True)
|
patch_schema = self.resource_def.SCHEMA(only=('transfer_state',
|
||||||
|
'transferred_devices',
|
||||||
|
'supplier_email',
|
||||||
|
'ethereum_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()
|
||||||
for key, value in d.items():
|
for key, value in d.items():
|
||||||
setattr(dlvnote, key, value)
|
setattr(dlvnote, key, value)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return Response(status=204)
|
return Response(status=204)
|
||||||
|
|
||||||
def one(self, id: uuid.UUID):
|
|
||||||
"""Gets one action."""
|
|
||||||
deliverynote = Deliverynote.query.filter_by(id=id).one() # type: Deliverynote
|
|
||||||
return self.schema.jsonify(deliverynote)
|
|
||||||
|
|
||||||
@teal.cache.cache(datetime.timedelta(minutes=5))
|
|
||||||
def find(self, args: dict):
|
|
||||||
"""Gets deliverynotes.
|
|
||||||
|
|
||||||
By passing the value `UiTree` in the parameter `format`
|
|
||||||
of the query you get a recursive nested suited for ui-tree::
|
|
||||||
|
|
||||||
[
|
|
||||||
{title: 'lot1',
|
|
||||||
nodes: [{title: 'child1', nodes:[]}]
|
|
||||||
]
|
|
||||||
|
|
||||||
Note that in this format filters are ignored.
|
|
||||||
|
|
||||||
Otherwise it just returns the standard flat view of lots that
|
|
||||||
you can filter.
|
|
||||||
"""
|
|
||||||
query = Deliverynote.query
|
|
||||||
if 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),
|
|
||||||
dlvnote.page, dlvnote.per_page, dlvnote.total, dlvnote.prev_num, dlvnote.next_num
|
|
||||||
)
|
|
||||||
|
|
||||||
def delete(self, id):
|
|
||||||
dlvnote = Deliverynote.query.filter_by(id=id).one()
|
|
||||||
dlvnote.delete()
|
|
||||||
db.session.commit()
|
|
||||||
return Response(status=204)
|
|
||||||
|
|
Reference in New Issue