2021-05-19 07:47:14 +00:00
|
|
|
import os
|
2021-05-19 11:59:59 +00:00
|
|
|
import time
|
2021-05-19 07:47:14 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from flask import current_app as app, request, g, Response
|
2021-05-13 11:35:46 +00:00
|
|
|
from teal.resource import View
|
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
2021-05-14 10:58:56 +00:00
|
|
|
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
|
2021-06-24 17:09:12 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import ConfirmDocument
|
2021-06-15 10:38:24 +00:00
|
|
|
from ereuse_devicehub.resources.hash_reports import ReportHash
|
2021-05-19 07:47:14 +00:00
|
|
|
|
2021-05-13 11:35:46 +00:00
|
|
|
|
2021-05-14 10:58:56 +00:00
|
|
|
class TradeDocumentView(View):
|
2021-05-13 11:35:46 +00:00
|
|
|
|
|
|
|
def one(self, id: str):
|
2021-05-14 10:58:56 +00:00
|
|
|
doc = TradeDocument.query.filter_by(id=id, owner=g.user).one()
|
|
|
|
return self.schema.jsonify(doc)
|
2021-05-13 11:35:46 +00:00
|
|
|
|
|
|
|
def post(self):
|
2021-05-14 10:58:56 +00:00
|
|
|
"""Add one document."""
|
2021-05-19 07:47:14 +00:00
|
|
|
|
2021-05-14 10:58:56 +00:00
|
|
|
data = request.get_json(validate=True)
|
2021-06-15 10:38:24 +00:00
|
|
|
hash3 = data['file_hash']
|
|
|
|
db_hash = ReportHash(hash3=hash3)
|
|
|
|
db.session.add(db_hash)
|
2021-05-19 07:47:14 +00:00
|
|
|
|
2021-05-14 10:58:56 +00:00
|
|
|
doc = TradeDocument(**data)
|
2021-05-21 11:16:30 +00:00
|
|
|
trade = doc.lot.trade
|
|
|
|
if trade:
|
|
|
|
trade.documents.add(doc)
|
2021-06-24 17:09:12 +00:00
|
|
|
confirm = ConfirmDocument(action=trade,
|
2021-05-21 11:16:30 +00:00
|
|
|
user=g.user,
|
|
|
|
devices=set(),
|
|
|
|
documents={doc})
|
|
|
|
db.session.add(confirm)
|
2021-05-14 10:58:56 +00:00
|
|
|
db.session.add(doc)
|
2021-05-13 11:35:46 +00:00
|
|
|
db.session().final_flush()
|
2021-05-14 10:58:56 +00:00
|
|
|
ret = self.schema.jsonify(doc)
|
2021-05-13 11:35:46 +00:00
|
|
|
ret.status_code = 201
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
2021-05-14 10:58:56 +00:00
|
|
|
|
|
|
|
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)
|