adding trade for documents

This commit is contained in:
Cayo Puigdefabregas 2021-05-19 13:59:59 +02:00
parent 443464c9b8
commit 28da6a953d
3 changed files with 25 additions and 16 deletions

View file

@ -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):

View file

@ -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'):

View file

@ -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()