From 28da6a953d6862341e002f659935bf4e2272bd2f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 19 May 2021 13:59:59 +0200 Subject: [PATCH] adding trade for documents --- ereuse_devicehub/resources/action/schemas.py | 6 ++++ .../resources/action/views/trade.py | 28 +++++++++++-------- .../resources/tradedocument/views.py | 7 ++--- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index a8c7df85..fb33cfea 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -528,7 +528,13 @@ class Trade(ActionWithMultipleDevices): txt = "you need to be the owner of the lot for to do a trade" raise ValidationError(txt) + for doc in data['lot'].documents: + if not doc.owner == g.user: + txt = "you need to be the owner of the documents for to do a trade" + raise ValidationError(txt) + data['devices'] = data['lot'].devices + data['documents'] = data['lot'].documents @validates_schema def validate_user_to_id(self, data: dict): diff --git a/ereuse_devicehub/resources/action/views/trade.py b/ereuse_devicehub/resources/action/views/trade.py index 57ecc665..8b22de17 100644 --- a/ereuse_devicehub/resources/action/views/trade.py +++ b/ereuse_devicehub/resources/action/views/trade.py @@ -11,15 +11,15 @@ from ereuse_devicehub.resources.user.models import User class TradeView(): """Handler for manager the trade action register from post - + request_post = { 'type': 'Trade', 'devices': [device_id], + 'documents': [document_id], 'userFrom': user2.email, 'userTo': user.email, 'price': 10, 'date': "2020-12-01T02:00:00+00:00", - 'documentID': '1', 'lot': lot['id'], 'confirm': True, } @@ -27,6 +27,7 @@ class TradeView(): """ def __init__(self, data, resource_def, schema): + # import pdb; pdb.set_trace() self.schema = schema a = resource_def.schema.load(data) self.trade = Trade(**a) @@ -36,7 +37,6 @@ class TradeView(): self.create_confirmations() def post(self): - # import pdb; pdb.set_trace() db.session().final_flush() ret = self.schema.jsonify(self.trade) ret.status_code = 201 @@ -49,21 +49,25 @@ class TradeView(): # if the confirmation is mandatory, do automatic confirmation only for # owner of the lot if self.trade.confirm: - confirm = Confirm(user=g.user, - action=self.trade, - devices=self.trade.devices) - db.session.add(confirm) + confirm_devs = Confirm(user=g.user, + action=self.trade, + devices=self.trade.devices) + + confirm_docs = Confirm(user=g.user, + action=self.trade, + documents=self.trade.documents) + db.session.add(confirm_devs, confirm_docs) return # check than the user than want to do the action is one of the users # involved in the action assert g.user.id in [self.trade.user_from_id, self.trade.user_to_id] - confirm_from = Confirm(user=self.trade.user_from, - action=self.trade, + confirm_from = Confirm(user=self.trade.user_from, + action=self.trade, devices=self.trade.devices) - confirm_to = Confirm(user=self.trade.user_to, - action=self.trade, + confirm_to = Confirm(user=self.trade.user_to, + action=self.trade, devices=self.trade.devices) db.session.add(confirm_from) db.session.add(confirm_to) @@ -292,7 +296,7 @@ class ConfirmRevokeView(ConfirmMixin): # Change the owner for every devices trade = data['action'] for dev in data['devices']: - # TODO @cayop this should be the author of confirm actions instead of + # TODO @cayop this should be the author of confirm actions instead of # author of trade dev.owner = trade.author if hasattr(dev, 'components'): diff --git a/ereuse_devicehub/resources/tradedocument/views.py b/ereuse_devicehub/resources/tradedocument/views.py index 212c6264..e85db306 100644 --- a/ereuse_devicehub/resources/tradedocument/views.py +++ b/ereuse_devicehub/resources/tradedocument/views.py @@ -1,7 +1,7 @@ import os +import time from datetime import datetime from flask import current_app as app, request, g, Response -from marshmallow import ValidationError from teal.resource import View from ereuse_devicehub.db import db @@ -22,8 +22,9 @@ def save_doc(data, user): day = now.day hour = now.hour minutes = now.minute + created = time.time() - name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{user}_{filename}" + name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{created}_{user}_{filename}" path_dir_base = os.path.join(app.config['PATH_DOCUMENTS_STORAGE'] , user) path = os.path.join(path_dir_base, str(lot.id)) path_name = os.path.join(path, name_file) @@ -44,14 +45,12 @@ class TradeDocumentView(View): def post(self): """Add one document.""" - # import pdb; pdb.set_trace() data = request.get_json(validate=True) data['path_name'] = save_doc(data, g.user.email) bfile = data.pop('file') insert_hash(bfile) - # import pdb; pdb.set_trace() doc = TradeDocument(**data) db.session.add(doc) db.session().final_flush()