2021-05-13 11:35:46 +00:00
|
|
|
from citext import CIText
|
2021-06-15 11:11:49 +00:00
|
|
|
from flask import g
|
2021-05-13 11:35:46 +00:00
|
|
|
|
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2021-05-19 07:47:14 +00:00
|
|
|
from sortedcontainers import SortedSet
|
2021-06-15 11:11:49 +00:00
|
|
|
from ereuse_devicehub.resources.models import Thing
|
2021-05-13 11:35:46 +00:00
|
|
|
|
2021-06-15 11:11:49 +00:00
|
|
|
from sqlalchemy import BigInteger, Column, Sequence
|
|
|
|
from sqlalchemy.orm import backref
|
|
|
|
from teal.db import CASCADE_OWN, URL
|
2021-05-13 11:35:46 +00:00
|
|
|
|
2021-06-15 11:11:49 +00:00
|
|
|
from ereuse_devicehub.resources.enums import Severity
|
2021-05-13 11:35:46 +00:00
|
|
|
|
|
|
|
|
2021-05-19 07:47:14 +00:00
|
|
|
_sorted_documents = {
|
|
|
|
'order_by': lambda: TradeDocument.created,
|
|
|
|
'collection_class': SortedSet
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:58:56 +00:00
|
|
|
class TradeDocument(Thing):
|
2021-05-13 11:35:46 +00:00
|
|
|
"""This represent a document involved in a trade action.
|
|
|
|
Every document is added to a lot.
|
|
|
|
When this lot is converted in one trade, the action trade is added to the document
|
|
|
|
and the action trade need to be confirmed for the both users of the trade.
|
|
|
|
This confirmation can be revoked and this revoked need to be ConfirmRevoke for have
|
|
|
|
some efect.
|
2021-06-15 11:11:49 +00:00
|
|
|
|
|
|
|
This documents can be invoices or list of devices or certificates of erasure of
|
2021-05-13 11:35:46 +00:00
|
|
|
one disk.
|
|
|
|
|
|
|
|
Like a Devices one document have actions and is possible add or delete of one lot
|
|
|
|
if this lot don't have a trade
|
|
|
|
|
|
|
|
The document is saved in the database
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
id = Column(BigInteger, Sequence('device_seq'), primary_key=True)
|
|
|
|
id.comment = """The identifier of the device for this database. Used only
|
|
|
|
internally for software; users should not use this.
|
|
|
|
"""
|
|
|
|
# type = Column(Unicode(STR_SM_SIZE), nullable=False)
|
|
|
|
date = Column(db.DateTime)
|
|
|
|
date.comment = """The date of document, some documents need to have one date
|
|
|
|
"""
|
|
|
|
id_document = Column(CIText())
|
|
|
|
id_document.comment = """The id of one document like invoice so they can be linked."""
|
|
|
|
description = Column(db.CIText())
|
|
|
|
description.comment = """A description of document."""
|
|
|
|
owner_id = db.Column(UUID(as_uuid=True),
|
|
|
|
db.ForeignKey(User.id),
|
|
|
|
nullable=False,
|
|
|
|
default=lambda: g.user.id)
|
|
|
|
owner = db.relationship(User, primaryjoin=owner_id == User.id)
|
2021-05-19 07:47:14 +00:00
|
|
|
lot_id = db.Column(UUID(as_uuid=True),
|
|
|
|
db.ForeignKey('lot.id'),
|
|
|
|
nullable=False)
|
2021-06-15 11:11:49 +00:00
|
|
|
lot = db.relationship('Lot',
|
2021-05-19 07:47:14 +00:00
|
|
|
backref=backref('documents',
|
|
|
|
lazy=True,
|
|
|
|
cascade=CASCADE_OWN,
|
|
|
|
**_sorted_documents),
|
|
|
|
primaryjoin='TradeDocument.lot_id == Lot.id')
|
|
|
|
lot.comment = """Lot to which the document is associated"""
|
2021-05-13 11:35:46 +00:00
|
|
|
file_name = Column(db.CIText())
|
|
|
|
file_name.comment = """This is the name of the file when user up the document."""
|
2021-06-15 10:38:02 +00:00
|
|
|
file_hash = Column(db.CIText())
|
|
|
|
file_hash.comment = """This is the hash of the file produced from frontend."""
|
2021-06-15 11:11:49 +00:00
|
|
|
url = db.Column(URL())
|
2021-06-15 10:38:02 +00:00
|
|
|
url.comment = """This is the url where resides the document."""
|
2021-08-03 14:40:17 +00:00
|
|
|
weight = db.Column(db.Float(nullable=True))
|
|
|
|
weight.comment = """This is the weight of one container than this document express."""
|
2021-05-13 11:35:46 +00:00
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
db.Index('document_id', id, postgresql_using='hash'),
|
|
|
|
# db.Index('type_doc', type, postgresql_using='hash')
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def actions(self) -> list:
|
|
|
|
"""All the actions where the device participated, including:
|
|
|
|
|
|
|
|
1. Actions performed directly to the device.
|
|
|
|
2. Actions performed to a component.
|
|
|
|
3. Actions performed to a parent device.
|
|
|
|
|
|
|
|
Actions are returned by descending ``created`` time.
|
|
|
|
"""
|
2021-06-24 09:41:14 +00:00
|
|
|
return sorted(self.actions_docs, key=lambda x: x.created)
|
2021-05-13 11:35:46 +00:00
|
|
|
|
2021-06-22 14:53:10 +00:00
|
|
|
@property
|
|
|
|
def trading(self):
|
|
|
|
"""The trading state, or None if no Trade action has
|
|
|
|
ever been performed to this device. This extract the posibilities for to do"""
|
|
|
|
|
2021-06-28 14:06:07 +00:00
|
|
|
confirm = 'Confirm'
|
|
|
|
need_confirm = 'Need Confirmation'
|
|
|
|
double_confirm = 'Document Confirmed'
|
2021-06-22 14:53:10 +00:00
|
|
|
revoke = 'Revoke'
|
2021-06-28 14:06:07 +00:00
|
|
|
revoke_pending = 'Revoke Pending'
|
|
|
|
confirm_revoke = 'Document Revoked'
|
2021-06-22 14:53:10 +00:00
|
|
|
if not self.actions:
|
2021-09-09 11:34:24 +00:00
|
|
|
return
|
2021-06-22 14:53:10 +00:00
|
|
|
|
2021-06-28 14:06:07 +00:00
|
|
|
ac = self.actions[-1]
|
2021-06-22 14:53:10 +00:00
|
|
|
|
2021-06-28 14:06:07 +00:00
|
|
|
if ac.type == 'ConfirmRevokeDocument':
|
|
|
|
# can to do revoke_confirmed
|
|
|
|
return confirm_revoke
|
2021-05-13 11:35:46 +00:00
|
|
|
|
2021-09-09 11:34:24 +00:00
|
|
|
if ac.type == 'RevokeDocument':
|
2021-06-28 14:06:07 +00:00
|
|
|
if ac.user == g.user:
|
|
|
|
# can todo revoke_pending
|
|
|
|
return revoke_pending
|
|
|
|
else:
|
|
|
|
# can to do confirm_revoke
|
|
|
|
return revoke
|
2021-05-13 11:35:46 +00:00
|
|
|
|
2021-06-28 14:06:07 +00:00
|
|
|
if ac.type == 'ConfirmDocument':
|
|
|
|
if ac.user == self.owner:
|
|
|
|
if self.owner == g.user:
|
|
|
|
# can to do revoke
|
|
|
|
return confirm
|
|
|
|
else:
|
|
|
|
# can to do confirm
|
|
|
|
return need_confirm
|
|
|
|
else:
|
|
|
|
# can to do revoke
|
|
|
|
return double_confirm
|
2021-05-13 11:35:46 +00:00
|
|
|
|
2021-09-09 11:34:24 +00:00
|
|
|
@property
|
|
|
|
def total_weight(self):
|
|
|
|
"""Return all weight than this container have."""
|
2021-09-10 12:07:35 +00:00
|
|
|
weight = self.weight or 0
|
|
|
|
for x in self.actions:
|
|
|
|
if not x.type == 'MoveOnDocument' or not x.weight:
|
|
|
|
continue
|
|
|
|
if self == x.container_from:
|
|
|
|
continue
|
|
|
|
weight += x.weight
|
|
|
|
|
|
|
|
return weight
|
2021-09-09 11:34:24 +00:00
|
|
|
|
2021-05-13 11:35:46 +00:00
|
|
|
def _warning_actions(self, actions):
|
2021-09-09 11:34:24 +00:00
|
|
|
"""Show warning actions"""
|
2021-05-13 11:35:46 +00:00
|
|
|
return sorted(ev for ev in actions if ev.severity >= Severity.Warning)
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.id < other.id
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return '{0.file_name}'.format(self)
|