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/deliverynote/views.py

54 lines
1.9 KiB
Python
Raw Normal View History

2020-02-26 19:21:59 +00:00
import datetime
import uuid
2020-08-17 14:45:18 +00:00
from flask import Response, request
2020-02-26 19:21:59 +00:00
from teal.resource import View
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.deliverynote.models import Deliverynote
from ereuse_devicehub.resources.lot.models import Lot
2020-02-26 19:21:59 +00:00
class DeliverynoteView(View):
2020-02-26 19:21:59 +00:00
def post(self):
# Create delivery note
dn = request.get_json()
dlvnote = Deliverynote(**dn)
# Create a lot
lot_name = dlvnote.supplier_email + "_" + datetime.datetime.utcnow().strftime("%B-%d-%Y")
new_lot = Lot(name=lot_name)
dlvnote.lot_id = new_lot.id
db.session.add(new_lot)
2020-02-26 19:21:59 +00:00
db.session.add(dlvnote)
db.session().final_flush()
ret = self.schema.jsonify(dlvnote)
ret.status_code = 201
db.session.commit()
return ret
def patch(self, id):
patch_schema = self.resource_def.SCHEMA(only=('transfer_state',
'ethereum_address'), partial=True)
d = request.get_json(schema=patch_schema)
dlvnote = Deliverynote.query.filter_by(id=id).one()
# device_fields = ['transfer_state', 'deliverynote_address']
# computers = [x for x in dlvnote.transferred_devices if isinstance(x, Computer)]
for key, value in d.items():
setattr(dlvnote, key, value)
# Transalate ethereum_address attribute
# devKey = key
# if key == 'ethereum_address':
# devKey = 'deliverynote_address'
# if devKey in device_fields:
# for dev in computers:
# setattr(dev, devKey, value)
2020-08-17 14:45:18 +00:00
db.session.commit()
return Response(status=204)
2020-03-04 16:44:58 +00:00
def one(self, id: uuid.UUID):
"""Get one delivery note"""
deliverynote = Deliverynote.query.filter_by(id=id).one() # type Deliverynote
return self.schema.jsonify(deliverynote)