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

36 lines
1.2 KiB
Python
Raw Normal View History

2021-05-13 11:35:46 +00:00
import marshmallow
from flask import g, current_app as app, render_template, request, Response
from flask.json import jsonify
from flask_sqlalchemy import Pagination
from marshmallow import fields, fields as f, validate as v, Schema as MarshmallowSchema
from teal.resource import View
from ereuse_devicehub import auth
from ereuse_devicehub.db import db
from ereuse_devicehub.query import SearchQueryParser, things_response
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
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."""
data = request.get_json(validate=True)
doc = TradeDocument(**data)
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)