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
|
2020-02-27 17:29:26 +00:00
|
|
|
from ereuse_devicehub.resources.deliverynote.models import Deliverynote
|
2020-02-29 21:58:49 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2020-02-26 19:21:59 +00:00
|
|
|
|
|
|
|
|
2020-02-27 17:29:26 +00:00
|
|
|
class DeliverynoteView(View):
|
2020-02-26 19:21:59 +00:00
|
|
|
|
|
|
|
def post(self):
|
2020-02-29 21:58:49 +00:00
|
|
|
# Create delivery note
|
|
|
|
dn = request.get_json()
|
|
|
|
dlvnote = Deliverynote(**dn)
|
|
|
|
# Create a lot
|
2021-02-05 14:58:39 +00:00
|
|
|
lot_name = dlvnote.document_id + "_" + datetime.datetime.utcnow().strftime("%Y-%m-%d")
|
2020-02-29 21:58:49 +00:00
|
|
|
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
|
|
|
|
|
2020-03-03 12:10:42 +00:00
|
|
|
def patch(self, id):
|
2021-02-03 21:28:04 +00:00
|
|
|
patch_schema = self.resource_def.SCHEMA(only=('transfer_state'), partial=True)
|
2020-03-03 12:10:42 +00:00
|
|
|
d = request.get_json(schema=patch_schema)
|
|
|
|
dlvnote = Deliverynote.query.filter_by(id=id).one()
|
2020-08-25 13:57:17 +00:00
|
|
|
# device_fields = ['transfer_state', 'deliverynote_address']
|
|
|
|
# computers = [x for x in dlvnote.transferred_devices if isinstance(x, Computer)]
|
2020-03-03 12:10:42 +00:00
|
|
|
for key, value in d.items():
|
|
|
|
setattr(dlvnote, key, value)
|
2020-08-17 14:45:18 +00:00
|
|
|
|
2020-03-03 12:10:42 +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
|
2020-03-04 17:52:07 +00:00
|
|
|
return self.schema.jsonify(deliverynote)
|