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

@ -15,11 +15,11 @@ class TradeView():
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,10 +49,14 @@ 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,
confirm_devs = Confirm(user=g.user,
action=self.trade,
devices=self.trade.devices)
db.session.add(confirm)
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

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