2020-02-26 19:21:59 +00:00
|
|
|
import uuid
|
|
|
|
from datetime import datetime
|
2020-08-17 14:45:18 +00:00
|
|
|
from typing import Iterable
|
2020-02-26 19:21:59 +00:00
|
|
|
|
|
|
|
from boltons import urlutils
|
|
|
|
from citext import CIText
|
|
|
|
from flask import g
|
2020-03-11 17:39:58 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
2020-08-17 14:45:18 +00:00
|
|
|
from teal.db import check_range, IntEnum
|
2020-02-26 19:21:59 +00:00
|
|
|
from teal.resource import url_for_resource
|
|
|
|
|
2020-08-17 14:45:18 +00:00
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.enums import TransferState
|
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2020-02-26 19:21:59 +00:00
|
|
|
from ereuse_devicehub.resources.models import Thing
|
|
|
|
from ereuse_devicehub.resources.user.models import User
|
|
|
|
|
|
|
|
|
2020-02-27 17:29:26 +00:00
|
|
|
class Deliverynote(Thing):
|
2020-02-26 19:21:59 +00:00
|
|
|
id = db.Column(UUID(as_uuid=True), primary_key=True) # uuid is generated on init by default
|
2020-02-29 21:58:49 +00:00
|
|
|
document_id = db.Column(CIText(), nullable=False)
|
|
|
|
creator_id = db.Column(UUID(as_uuid=True),
|
2020-08-17 14:45:18 +00:00
|
|
|
db.ForeignKey(User.id),
|
|
|
|
nullable=False,
|
|
|
|
default=lambda: g.user.id)
|
2020-02-27 17:29:26 +00:00
|
|
|
creator = db.relationship(User, primaryjoin=creator_id == User.id)
|
2020-02-29 21:58:49 +00:00
|
|
|
supplier_email = db.Column(CIText(),
|
2020-08-17 14:45:18 +00:00
|
|
|
db.ForeignKey(User.email),
|
|
|
|
nullable=False,
|
|
|
|
default=lambda: g.user.email)
|
2020-02-29 21:58:49 +00:00
|
|
|
supplier = db.relationship(User, primaryjoin=lambda: Deliverynote.supplier_email == User.email)
|
2020-03-03 12:10:42 +00:00
|
|
|
receiver_address = db.Column(CIText(),
|
2020-08-17 14:45:18 +00:00
|
|
|
db.ForeignKey(User.email),
|
|
|
|
nullable=False,
|
|
|
|
default=lambda: g.user.email)
|
2020-03-03 19:32:04 +00:00
|
|
|
receiver = db.relationship(User, primaryjoin=lambda: Deliverynote.receiver_address == User.email)
|
2020-02-29 21:58:49 +00:00
|
|
|
date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
|
2020-03-03 12:10:42 +00:00
|
|
|
date.comment = 'The date the DeliveryNote initiated'
|
2021-02-05 12:21:20 +00:00
|
|
|
amount = db.Column(db.Integer, check_range('amount', min=0, max=100), default=0)
|
2020-03-03 19:32:04 +00:00
|
|
|
# The following fields are supposed to be 0:N relationships
|
2020-02-27 17:29:26 +00:00
|
|
|
# to SnapshotDelivery entity.
|
|
|
|
# At this stage of implementation they will treated as a
|
|
|
|
# comma-separated string of the devices expexted/transfered
|
2020-03-11 17:39:58 +00:00
|
|
|
expected_devices = db.Column(JSONB, nullable=False)
|
|
|
|
# expected_devices = db.Column(db.ARRAY(JSONB, dimensions=1), nullable=False)
|
2020-03-03 19:32:04 +00:00
|
|
|
transferred_devices = db.Column(db.ARRAY(db.Integer, dimensions=1), nullable=True)
|
2020-02-27 17:29:26 +00:00
|
|
|
transfer_state = db.Column(IntEnum(TransferState), default=TransferState.Initial, nullable=False)
|
|
|
|
transfer_state.comment = TransferState.__doc__
|
2020-02-29 21:58:49 +00:00
|
|
|
lot_id = db.Column(UUID(as_uuid=True),
|
2020-08-17 14:45:18 +00:00
|
|
|
db.ForeignKey(Lot.id),
|
|
|
|
nullable=False)
|
2020-03-04 17:49:55 +00:00
|
|
|
lot = db.relationship(Lot,
|
|
|
|
backref=db.backref('deliverynote', uselist=False, lazy=True),
|
|
|
|
lazy=True,
|
|
|
|
primaryjoin=Lot.id == lot_id)
|
2020-02-26 19:21:59 +00:00
|
|
|
|
2021-02-05 12:21:20 +00:00
|
|
|
def __init__(self, document_id: str, amount: str, date,
|
2020-08-17 14:45:18 +00:00
|
|
|
supplier_email: str,
|
2020-03-11 17:39:58 +00:00
|
|
|
expected_devices: Iterable,
|
2020-03-10 14:51:16 +00:00
|
|
|
transfer_state: TransferState) -> None:
|
2020-02-26 19:21:59 +00:00
|
|
|
"""Initializes a delivery note
|
|
|
|
"""
|
2020-02-29 21:58:49 +00:00
|
|
|
super().__init__(id=uuid.uuid4(),
|
2021-02-05 12:21:20 +00:00
|
|
|
document_id=document_id, amount=amount, date=date,
|
2020-02-29 21:58:49 +00:00
|
|
|
supplier_email=supplier_email,
|
2020-03-10 14:51:16 +00:00
|
|
|
expected_devices=expected_devices,
|
|
|
|
transfer_state=transfer_state)
|
2020-02-26 19:21:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self) -> str:
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self) -> urlutils.URL:
|
|
|
|
"""The URL where to GET this action."""
|
2020-02-27 17:29:26 +00:00
|
|
|
return urlutils.URL(url_for_resource(Deliverynote, item_id=self.id))
|
2020-02-26 19:21:59 +00:00
|
|
|
|
2020-03-03 12:10:42 +00:00
|
|
|
def delete(self):
|
|
|
|
"""Deletes the deliverynote.
|
2020-02-26 19:21:59 +00:00
|
|
|
|
2020-03-03 12:10:42 +00:00
|
|
|
This method removes the delivery note.
|
|
|
|
"""
|
|
|
|
db.session.delete(self)
|
2020-02-26 19:21:59 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2021-02-04 19:42:50 +00:00
|
|
|
return '<Deliverynote {0.document_id}>'.format(self)
|