fix bug 3841

This commit is contained in:
Cayo Puigdefabregas 2022-09-26 08:14:04 +02:00
parent 5f745ad04a
commit 194fe366fd
2 changed files with 43 additions and 33 deletions

View File

@ -1,26 +1,25 @@
import copy import copy
from contextlib import suppress from contextlib import suppress
from citext import CIText from citext import CIText
from flask import g from flask import g
from sqlalchemy.dialects.postgresql import UUID
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.user.models import User
from sortedcontainers import SortedSet from sortedcontainers import SortedSet
from ereuse_devicehub.resources.models import Thing
from sqlalchemy import BigInteger, Column, Sequence from sqlalchemy import BigInteger, Column, Sequence
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import backref from sqlalchemy.orm import backref
from teal.db import CASCADE_OWN, URL from teal.db import CASCADE_OWN, URL
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.enums import Severity from ereuse_devicehub.resources.enums import Severity
from ereuse_devicehub.resources.models import Thing
from ereuse_devicehub.resources.user.models import User
_sorted_documents = { _sorted_documents = {
'order_by': lambda: TradeDocument.created, 'order_by': lambda: TradeDocument.created,
'collection_class': SortedSet 'collection_class': SortedSet,
} }
class TradeDocument(Thing): class TradeDocument(Thing):
"""This represent a document involved in a trade action. """This represent a document involved in a trade action.
Every document is added to a lot. Every document is added to a lot.
@ -48,23 +47,26 @@ class TradeDocument(Thing):
date.comment = """The date of document, some documents need to have one date date.comment = """The date of document, some documents need to have one date
""" """
id_document = Column(CIText()) id_document = Column(CIText())
id_document.comment = """The id of one document like invoice so they can be linked.""" id_document.comment = (
"""The id of one document like invoice so they can be linked."""
)
description = Column(db.CIText()) description = Column(db.CIText())
description.comment = """A description of document.""" description.comment = """A description of document."""
owner_id = db.Column(UUID(as_uuid=True), owner_id = db.Column(
db.ForeignKey(User.id), UUID(as_uuid=True),
nullable=False, db.ForeignKey(User.id),
default=lambda: g.user.id) nullable=False,
default=lambda: g.user.id,
)
owner = db.relationship(User, primaryjoin=owner_id == User.id) owner = db.relationship(User, primaryjoin=owner_id == User.id)
lot_id = db.Column(UUID(as_uuid=True), lot_id = db.Column(UUID(as_uuid=True), db.ForeignKey('lot.id'), nullable=False)
db.ForeignKey('lot.id'), lot = db.relationship(
nullable=False) 'Lot',
lot = db.relationship('Lot', backref=backref(
backref=backref('documents', 'documents', lazy=True, cascade=CASCADE_OWN, **_sorted_documents
lazy=True, ),
cascade=CASCADE_OWN, primaryjoin='TradeDocument.lot_id == Lot.id',
**_sorted_documents), )
primaryjoin='TradeDocument.lot_id == Lot.id')
lot.comment = """Lot to which the document is associated""" lot.comment = """Lot to which the document is associated"""
file_name = Column(db.CIText()) file_name = Column(db.CIText())
file_name.comment = """This is the name of the file when user up the document.""" file_name.comment = """This is the name of the file when user up the document."""
@ -73,7 +75,9 @@ class TradeDocument(Thing):
url = db.Column(URL()) url = db.Column(URL())
url.comment = """This is the url where resides the document.""" url.comment = """This is the url where resides the document."""
weight = db.Column(db.Float()) weight = db.Column(db.Float())
weight.comment = """This is the weight of one container than this document express.""" weight.comment = (
"""This is the weight of one container than this document express."""
)
__table_args__ = ( __table_args__ = (
db.Index('document_id', id, postgresql_using='hash'), db.Index('document_id', id, postgresql_using='hash'),
@ -107,7 +111,6 @@ class TradeDocument(Thing):
if not ac: if not ac:
return return
if ac.type == 'ConfirmRevokeDocument': if ac.type == 'ConfirmRevokeDocument':
# can to do revoke_confirmed # can to do revoke_confirmed
return confirm_revoke return confirm_revoke
@ -145,16 +148,23 @@ class TradeDocument(Thing):
return weight return weight
def get_url(self) -> str:
if self.url:
return self.url.to_text()
return ''
def last_action_trading(self): def last_action_trading(self):
"""which is the last action trading""" """which is the last action trading"""
with suppress(StopIteration, ValueError): with suppress(StopIteration, ValueError):
actions = copy.copy(self.actions) actions = copy.copy(self.actions)
actions.sort(key=lambda x: x.created) actions.sort(key=lambda x: x.created)
t_trades = ['Trade', t_trades = [
'Confirm', 'Trade',
'ConfirmRevokeDocument', 'Confirm',
'RevokeDocument', 'ConfirmRevokeDocument',
'ConfirmDocument'] 'RevokeDocument',
'ConfirmDocument',
]
return next(e for e in reversed(actions) if e.t in t_trades) return next(e for e in reversed(actions) if e.t in t_trades)
def _warning_actions(self, actions): def _warning_actions(self, actions):

View File

@ -465,8 +465,8 @@
{% for doc in lot.documents %} {% for doc in lot.documents %}
<tr> <tr>
<td> <td>
{% if doc.url.to_text() %} {% if doc.get_url() %}
<a href="{{ doc.url.to_text() }}" target="_blank">{{ doc.file_name}}</a> <a href="{{ doc.get_url() }}" target="_blank">{{ doc.file_name}}</a>
{% else %} {% else %}
{{ doc.file_name}} {{ doc.file_name}}
{% endif %} {% endif %}
@ -479,8 +479,8 @@
{% for doc in lot.trade.documents %} {% for doc in lot.trade.documents %}
<tr> <tr>
<td> <td>
{% if doc.url.to_text() %} {% if doc.get_url() %}
<a href="{{ doc.url.to_text() }}" target="_blank">{{ doc.file_name}}</a> <a href="{{ doc.get_url() }}" target="_blank">{{ doc.file_name}}</a>
{% else %} {% else %}
{{ doc.file_name}} {{ doc.file_name}}
{% endif %} {% endif %}