2020-02-26 19:21:59 +00:00
|
|
|
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.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.resource import url_for_resource
|
|
|
|
|
|
|
|
from ereuse_devicehub.db import create_view, db, exp, f
|
|
|
|
from ereuse_devicehub.resources.models import Thing
|
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2020-02-29 21:58:49 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2020-02-26 19:21:59 +00:00
|
|
|
from ereuse_devicehub.resources.enums import TransferState
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
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(),
|
|
|
|
db.ForeignKey(User.email),
|
|
|
|
nullable=False,
|
|
|
|
default=lambda: g.user.email)
|
|
|
|
supplier = db.relationship(User, primaryjoin=lambda: Deliverynote.supplier_email == User.email)
|
|
|
|
# supplier = db.relationship(User)
|
|
|
|
date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
|
2020-02-26 19:21:59 +00:00
|
|
|
# deposit = db.Column(db.Integer, check_range('deposit', min=0, max=100), default=0)
|
2020-02-27 17:29:26 +00:00
|
|
|
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__
|
2020-02-29 21:58:49 +00:00
|
|
|
lot_id = db.Column(UUID(as_uuid=True),
|
|
|
|
db.ForeignKey(Lot.id),
|
|
|
|
nullable=False)
|
2020-02-27 17:29:26 +00:00
|
|
|
lots = db.relationship(Lot,
|
|
|
|
backref=db.backref('deliverynotes', lazy=True, collection_class=set),
|
|
|
|
lazy=True,
|
2020-02-29 21:58:49 +00:00
|
|
|
primaryjoin=Lot.id == lot_id,
|
2020-02-27 17:29:26 +00:00
|
|
|
collection_class=set)
|
2020-02-26 19:21:59 +00:00
|
|
|
|
2020-02-29 21:58:49 +00:00
|
|
|
def __init__(self, document_id: str, deposit: str,
|
|
|
|
supplier_email: str,
|
|
|
|
expected_devices: str,
|
|
|
|
transferred_devices: str) -> 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(),
|
|
|
|
document_id=document_id, deposit=deposit,
|
|
|
|
supplier_email=supplier_email,
|
|
|
|
expected_devices=expected_devices,
|
|
|
|
transferred_devices=transferred_devices)
|
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
|
|
|
|
|
|
|
|
|
|
|
# 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__))
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
# return '<Lot {0.name} devices={0.devices!r}>'.format(self)
|
2020-02-27 17:29:26 +00:00
|
|
|
return '<Deliverynote {0.documentID}>'.format(self)
|
2020-02-26 19:21:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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."""
|