This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/tradedocument/views.py

54 lines
1.6 KiB
Python
Raw Normal View History

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
2023-03-21 11:08:13 +00:00
from flask import Response
from flask import current_app as app
from flask import g, request
2021-07-06 14:23:31 +00:00
from marshmallow import ValidationError
2021-05-13 11:35:46 +00:00
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action.models import ConfirmDocument
2021-06-15 10:38:24 +00:00
from ereuse_devicehub.resources.hash_reports import ReportHash
2023-03-21 11:08:13 +00:00
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
from ereuse_devicehub.teal.resource import View
2021-05-19 07:47:14 +00:00
2021-05-13 11:35:46 +00:00
class TradeDocumentView(View):
2021-05-13 11:35:46 +00:00
def one(self, id: str):
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):
"""Add one document."""
2021-05-19 07:47:14 +00:00
2021-07-06 14:23:31 +00:00
try:
data = request.get_json(validate=True)
except ValueError as err:
raise ValidationError(err)
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
doc = TradeDocument(**data)
2021-05-21 11:16:30 +00:00
trade = doc.lot.trade
if trade:
trade.documents.add(doc)
2023-03-21 11:08:13 +00:00
confirm = ConfirmDocument(
action=trade, user=g.user, devices=set(), documents={doc}
)
2021-05-21 11:16:30 +00:00
db.session.add(confirm)
db.session.add(doc)
2021-05-13 11:35:46 +00:00
db.session().final_flush()
ret = self.schema.jsonify(doc)
2021-05-13 11:35:46 +00:00
ret.status_code = 201
db.session.commit()
return ret
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)