Definition of endpoint, view and scheme for basic document

This commit is contained in:
Cayo Puigdefabregas 2021-05-14 12:58:56 +02:00
parent 6fc0e71833
commit 4b11372719
7 changed files with 46 additions and 45 deletions

View File

@ -12,6 +12,7 @@ from ereuse_devicehub.resources import action, agent, deliverynote, inventory, \
lot, tag, user
from ereuse_devicehub.resources.device import definitions
from ereuse_devicehub.resources.documents import documents
from ereuse_devicehub.resources.tradedocument import definitions as tradedocument
from ereuse_devicehub.resources.enums import PriceSoftware
from ereuse_devicehub.resources.versions import versions
from ereuse_devicehub.resources.licences import licences
@ -27,6 +28,7 @@ class DevicehubConfig(Config):
import_resource(lot),
import_resource(deliverynote),
import_resource(documents),
import_resource(tradedocument),
import_resource(inventory),
import_resource(versions),
import_resource(licences),

View File

@ -48,7 +48,7 @@ from ereuse_devicehub.resources.enums import AppearanceRange, BatteryHealth, Bio
TestDataStorageLength
from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing
from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.tradedocument.models import Document
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
class JoinedTableMixin:
@ -296,18 +296,17 @@ class ActionDevice(db.Model):
primary_key=True)
class ActionWithMultipleDocuments(ActionWithMultipleDevices):
# pass
documents = relationship(Document,
class ActionWithMultipleTradeDocuments(ActionWithMultipleDevices):
documents = relationship(TradeDocument,
backref=backref('actions_multiple_docs', lazy=True, **_sorted_actions),
secondary=lambda: ActionDocument.__table__,
order_by=lambda: Document.id,
secondary=lambda: ActionTradeDocument.__table__,
order_by=lambda: TradeDocument.id,
collection_class=OrderedSet)
class ActionDocument(db.Model):
document_id = Column(BigInteger, ForeignKey(Document.id), primary_key=True)
action_id = Column(UUID(as_uuid=True), ForeignKey(ActionWithMultipleDocuments.id),
class ActionTradeDocument(db.Model):
document_id = Column(BigInteger, ForeignKey(TradeDocument.id), primary_key=True)
action_id = Column(UUID(as_uuid=True), ForeignKey(ActionWithMultipleTradeDocuments.id),
primary_key=True)
@ -1449,7 +1448,7 @@ class CancelReservation(Organize):
"""The act of cancelling a reservation."""
class Confirm(JoinedTableMixin, ActionWithMultipleDocuments):
class Confirm(JoinedTableMixin, ActionWithMultipleTradeDocuments):
"""Users confirm the one action trade this confirmation it's link to trade
and the devices that confirm
"""
@ -1489,7 +1488,7 @@ class ConfirmRevoke(Confirm):
return '<{0.t} {0.id} accepted by {0.user}>'.format(self)
class Trade(JoinedTableMixin, ActionWithMultipleDocuments):
class Trade(JoinedTableMixin, ActionWithMultipleTradeDocuments):
"""Trade actions log the political exchange of devices between users.
Every time a trade action is performed, the old user looses its
political possession, for example ownership, in favor of another
@ -1502,7 +1501,6 @@ class Trade(JoinedTableMixin, ActionWithMultipleDocuments):
This class and its inheritors
extend `Schema's Trade <http://schema.org/TradeAction>`_.
"""
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
user_from_id = db.Column(UUID(as_uuid=True),
db.ForeignKey(User.id),
nullable=False)

View File

@ -1,10 +0,0 @@
from teal.resource import Converters, Resource
from ereuse_devicehub.resources.tradedocument import schemas
from ereuse_devicehub.resources.tradedocument.views import DocumentView
class TradeDocumentDef(Resource):
SCHEMA = schemas.Document
VIEW = DocumentView
AUTH = True
ID_CONVERTER = Converters.string

View File

@ -0,0 +1,10 @@
from teal.resource import Converters, Resource
from ereuse_devicehub.resources.tradedocument import schemas
from ereuse_devicehub.resources.tradedocument.views import TradeDocumentView
class TradeDocumentDef(Resource):
SCHEMA = schemas.TradeDocument
VIEW = TradeDocumentView
AUTH = True
ID_CONVERTER = Converters.string

View File

@ -24,7 +24,7 @@ from ereuse_devicehub.resources.enums import BatteryTechnology, CameraFacing, Co
DataStorageInterface, DisplayTech, PrinterTechnology, RamFormat, RamInterface, Severity, TransferState
class Document(Thing):
class TradeDocument(Thing):
"""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

View File

@ -5,10 +5,11 @@ from ereuse_devicehub.resources.schemas import Thing
from ereuse_devicehub.resources.tradedocument import models as m
class Document(Thing):
__doc__ = m.Document.__doc__
id = Integer(description=m.Document.id.comment, dump_only=True)
date = DateTime(required=False, description=m.Document.date.comment)
id_document = SanitizedStr(default='', description=m.Document.id_document.comment)
description = SanitizedStr(default='', description=m.Document.description.comment)
file_name = SanitizedStr(default='', description=m.Document.file_name.comment)
class TradeDocument(Thing):
__doc__ = m.TradeDocument.__doc__
id = Integer(description=m.TradeDocument.id.comment, dump_only=True)
date = DateTime(required=False, description=m.TradeDocument.date.comment)
id_document = SanitizedStr(default='', description=m.TradeDocument.id_document.comment)
description = SanitizedStr(default='', description=m.TradeDocument.description.comment)
file_name = SanitizedStr(default='', description=m.TradeDocument.file_name.comment)
# lot = NestedOn('Lot', dump_only=True, description=m.TradeDocument.lot.__doc__)

View File

@ -9,27 +9,27 @@ from teal.resource import View
from ereuse_devicehub import auth
from ereuse_devicehub.db import db
from ereuse_devicehub.query import SearchQueryParser, things_response
from ereuse_devicehub.resources.tradedocument.models import Document
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
class DocumentView(View):
class TradeDocumentView(View):
# @auth.Auth.requires_auth
def one(self, id: str):
document = Document.query.filter_by(id=id).first()
return self.schema.jsonify(document)
doc = TradeDocument.query.filter_by(id=id, owner=g.user).one()
return self.schema.jsonify(doc)
# @auth.Auth.requires_auth
def post(self):
"""Posts an action."""
json = request.get_json(validate=False)
resource_def = app.resources[json['type']]
a = resource_def.schema.load(json)
Model = db.Model._decl_class_registry.data[json['type']]()
action = Model(**a)
db.session.add(action)
"""Add one document."""
data = request.get_json(validate=True)
doc = TradeDocument(**data)
db.session.add(doc)
db.session().final_flush()
ret = self.schema.jsonify(action)
ret = self.schema.jsonify(doc)
ret.status_code = 201
db.session.commit()
return ret
def delete(self, id):
doc = TradeDocument.query.filter_by(id=id, owner=g.user).one()
db.session.delete(doc)
db.session.commit()
return Response(status=204)