Receiver, creator, deposit, expected/transferred devices constraints, remove GET, PATCH delivery note

This commit is contained in:
yiorgos marinellis 2020-03-03 20:32:04 +01:00
parent 3d96e8190d
commit 8e5511b587
4 changed files with 25 additions and 68 deletions

View File

@ -12,7 +12,6 @@ class DeliverynoteDef(Resource):
SCHEMA = schemas.Deliverynote
VIEW = DeliverynoteView
AUTH = True
# AUTH = False
ID_CONVERTER = Converters.uuid
def __init__(self, app,

View File

@ -1,18 +1,15 @@
import uuid
from datetime import datetime
from typing import Union
from boltons import urlutils
from citext import CIText
from flask import g
from sqlalchemy import TEXT, Enum as DBEnum
from sqlalchemy.types import ARRAY
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy_utils import LtreeType
from sqlalchemy_utils.types.ltree import LQUERY
from teal.db import CASCADE_OWN, UUIDLtree, check_range, IntEnum
from teal.db import CASCADE_OWN, check_range, IntEnum
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.user.models import User
from ereuse_devicehub.resources.lot.models import Lot
@ -33,20 +30,19 @@ class Deliverynote(Thing):
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)
db.ForeignKey(User.email),
nullable=False,
default=lambda: g.user.email)
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
deposit = db.Column(db.Integer, check_range('deposit', min=0, max=100), default=0)
# The following fields 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=True)
expected_devices = db.Column(db.ARRAY(db.Integer, dimensions=1), nullable=False)
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.comment = TransferState.__doc__
ethereum_address = db.Column(CIText(), unique=True, default=None)

View File

@ -14,14 +14,15 @@ class Deliverynote(Thing):
id = f.UUID(dump_only=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')
creator = NestedOn(s_user.User, dump_only=True)
supplier_email = SanitizedStr(validate=f.validate.Length(max=STR_SIZE),
load_only=True, required=True)
supplier = NestedOn(s_user.User, dump_only=True)
receiver = NestedOn(s_user.User, dump_only=True)
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=False)
deposit = f.Integer(validate=f.validate.Range(min=0, max=100),
description=m.Deliverynote.deposit.__doc__)
ethereum_address = f.String(description='User identifier address inside the Blockchain')
expected_devices = f.List(f.Integer(), required=True, data_key='expectedDevices')
transferred_devices = f.List(f.Integer(), required=False, data_key='transferredDevices')
transfer_state = EnumField(TransferState, description=m.Lot.transfer_state.comment)

View File

@ -19,11 +19,6 @@ from ereuse_devicehub.resources.lot.models import Lot
class DeliverynoteView(View):
class FindArgs(MarshmallowSchema):
"""Allowed arguments for the ``find``
method (GET collection) endpoint
"""
search = f.Str(missing=None)
def post(self):
# Create delivery note
@ -42,47 +37,13 @@ class DeliverynoteView(View):
return ret
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)
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."""
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)