From 4b113727197771e3a36e2795d55a24fe6fbe64de Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 14 May 2021 12:58:56 +0200 Subject: [PATCH] Definition of endpoint, view and scheme for basic document --- ereuse_devicehub/config.py | 2 ++ ereuse_devicehub/resources/action/models.py | 22 +++++++------- .../resources/tradedocument/__init__.py | 10 ------- .../resources/tradedocument/definitions.py | 10 +++++++ .../resources/tradedocument/models.py | 2 +- .../resources/tradedocument/schemas.py | 15 +++++----- .../resources/tradedocument/views.py | 30 +++++++++---------- 7 files changed, 46 insertions(+), 45 deletions(-) create mode 100644 ereuse_devicehub/resources/tradedocument/definitions.py diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index 02d97da3..e9d1373d 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -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), diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index fe23d2e2..5250469d 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -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 `_. """ - 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) diff --git a/ereuse_devicehub/resources/tradedocument/__init__.py b/ereuse_devicehub/resources/tradedocument/__init__.py index 0e75f6e8..e69de29b 100644 --- a/ereuse_devicehub/resources/tradedocument/__init__.py +++ b/ereuse_devicehub/resources/tradedocument/__init__.py @@ -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 diff --git a/ereuse_devicehub/resources/tradedocument/definitions.py b/ereuse_devicehub/resources/tradedocument/definitions.py new file mode 100644 index 00000000..e321c7b4 --- /dev/null +++ b/ereuse_devicehub/resources/tradedocument/definitions.py @@ -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 diff --git a/ereuse_devicehub/resources/tradedocument/models.py b/ereuse_devicehub/resources/tradedocument/models.py index 85db108b..d4274fd8 100644 --- a/ereuse_devicehub/resources/tradedocument/models.py +++ b/ereuse_devicehub/resources/tradedocument/models.py @@ -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 diff --git a/ereuse_devicehub/resources/tradedocument/schemas.py b/ereuse_devicehub/resources/tradedocument/schemas.py index b71b85d7..86c2e213 100644 --- a/ereuse_devicehub/resources/tradedocument/schemas.py +++ b/ereuse_devicehub/resources/tradedocument/schemas.py @@ -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__) diff --git a/ereuse_devicehub/resources/tradedocument/views.py b/ereuse_devicehub/resources/tradedocument/views.py index cc30e228..69b206c9 100644 --- a/ereuse_devicehub/resources/tradedocument/views.py +++ b/ereuse_devicehub/resources/tradedocument/views.py @@ -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)