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" txt = "you need to be the owner of the lot for to do a trade"
raise ValidationError(txt) 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['devices'] = data['lot'].devices
data['documents'] = data['lot'].documents
@validates_schema @validates_schema
def validate_user_to_id(self, data: dict): def validate_user_to_id(self, data: dict):

View file

@ -15,11 +15,11 @@ class TradeView():
request_post = { request_post = {
'type': 'Trade', 'type': 'Trade',
'devices': [device_id], 'devices': [device_id],
'documents': [document_id],
'userFrom': user2.email, 'userFrom': user2.email,
'userTo': user.email, 'userTo': user.email,
'price': 10, 'price': 10,
'date': "2020-12-01T02:00:00+00:00", 'date': "2020-12-01T02:00:00+00:00",
'documentID': '1',
'lot': lot['id'], 'lot': lot['id'],
'confirm': True, 'confirm': True,
} }
@ -27,6 +27,7 @@ class TradeView():
""" """
def __init__(self, data, resource_def, schema): def __init__(self, data, resource_def, schema):
# import pdb; pdb.set_trace()
self.schema = schema self.schema = schema
a = resource_def.schema.load(data) a = resource_def.schema.load(data)
self.trade = Trade(**a) self.trade = Trade(**a)
@ -36,7 +37,6 @@ class TradeView():
self.create_confirmations() self.create_confirmations()
def post(self): def post(self):
# import pdb; pdb.set_trace()
db.session().final_flush() db.session().final_flush()
ret = self.schema.jsonify(self.trade) ret = self.schema.jsonify(self.trade)
ret.status_code = 201 ret.status_code = 201
@ -49,10 +49,14 @@ class TradeView():
# if the confirmation is mandatory, do automatic confirmation only for # if the confirmation is mandatory, do automatic confirmation only for
# owner of the lot # owner of the lot
if self.trade.confirm: if self.trade.confirm:
confirm = Confirm(user=g.user, confirm_devs = Confirm(user=g.user,
action=self.trade, action=self.trade,
devices=self.trade.devices) 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 return
# check than the user than want to do the action is one of the users # check than the user than want to do the action is one of the users

View file

@ -1,7 +1,7 @@
import os import os
import time
from datetime import datetime from datetime import datetime
from flask import current_app as app, request, g, Response from flask import current_app as app, request, g, Response
from marshmallow import ValidationError
from teal.resource import View from teal.resource import View
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
@ -22,8 +22,9 @@ def save_doc(data, user):
day = now.day day = now.day
hour = now.hour hour = now.hour
minutes = now.minute 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_dir_base = os.path.join(app.config['PATH_DOCUMENTS_STORAGE'] , user)
path = os.path.join(path_dir_base, str(lot.id)) path = os.path.join(path_dir_base, str(lot.id))
path_name = os.path.join(path, name_file) path_name = os.path.join(path, name_file)
@ -44,14 +45,12 @@ class TradeDocumentView(View):
def post(self): def post(self):
"""Add one document.""" """Add one document."""
# import pdb; pdb.set_trace()
data = request.get_json(validate=True) data = request.get_json(validate=True)
data['path_name'] = save_doc(data, g.user.email) data['path_name'] = save_doc(data, g.user.email)
bfile = data.pop('file') bfile = data.pop('file')
insert_hash(bfile) insert_hash(bfile)
# import pdb; pdb.set_trace()
doc = TradeDocument(**data) doc = TradeDocument(**data)
db.session.add(doc) db.session.add(doc)
db.session().final_flush() db.session().final_flush()