2020-02-26 19:21:59 +00:00
|
|
|
import datetime
|
|
|
|
import uuid
|
|
|
|
from collections import deque
|
|
|
|
from enum import Enum
|
|
|
|
from typing import Dict, List, Set, Union
|
|
|
|
|
|
|
|
import marshmallow as ma
|
|
|
|
import teal.cache
|
|
|
|
from flask import Response, jsonify, request
|
|
|
|
from marshmallow import Schema as MarshmallowSchema, fields as f
|
|
|
|
from teal.marshmallow import EnumField
|
|
|
|
from teal.resource import View
|
|
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.query import things_response
|
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
|
|
|
class FindArgs(MarshmallowSchema):
|
|
|
|
"""Allowed arguments for the ``find``
|
|
|
|
method (GET collection) endpoint
|
|
|
|
"""
|
|
|
|
search = f.Str(missing=None)
|
|
|
|
|
|
|
|
def post(self):
|
2020-02-29 21:58:49 +00:00
|
|
|
# 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=('name', 'description', 'transfer_state', 'receiver_address', 'deposit', 'deliverynote_address', 'devices', 'owner_address'), partial=True)
|
|
|
|
# d = request.get_json(schema=patch_schema)
|
2020-02-27 17:29:26 +00:00
|
|
|
# dlvnote = Deliverynote.query.filter_by(id=id).one()
|
2020-02-26 19:21:59 +00:00
|
|
|
# device_fields = ['transfer_state', 'receiver_address', 'deposit', 'deliverynote_address', 'owner_address']
|
|
|
|
# computers = [x for x in dlvnote.all_devices if isinstance(x, Computer)]
|
|
|
|
# for key, value in d.items():
|
|
|
|
# setattr(dlvnote, key, value)
|
|
|
|
# if key in device_fields:
|
|
|
|
# for dev in computers:
|
|
|
|
# setattr(dev, key, value)
|
|
|
|
# db.session.commit()
|
|
|
|
# return Response(status=204)
|
|
|
|
|
|
|
|
def one(self, id: uuid.UUID):
|
|
|
|
"""Gets one action."""
|
2020-02-27 17:29:26 +00:00
|
|
|
deliverynote = Deliverynote.query.filter_by(id=id).one() # type: Deliverynote
|
2020-02-26 19:21:59 +00:00
|
|
|
return self.schema.jsonify(deliverynote)
|
|
|
|
|
|
|
|
@teal.cache.cache(datetime.timedelta(minutes=5))
|
|
|
|
def find(self, args: dict):
|
|
|
|
"""Gets deliverynotes.
|
|
|
|
|
|
|
|
By passing the value `UiTree` in the parameter `format`
|
|
|
|
of the query you get a recursive nested suited for ui-tree::
|
|
|
|
|
|
|
|
[
|
|
|
|
{title: 'lot1',
|
|
|
|
nodes: [{title: 'child1', nodes:[]}]
|
|
|
|
]
|
|
|
|
|
|
|
|
Note that in this format filters are ignored.
|
|
|
|
|
|
|
|
Otherwise it just returns the standard flat view of lots that
|
|
|
|
you can filter.
|
|
|
|
"""
|
2020-02-27 17:29:26 +00:00
|
|
|
query = Deliverynote.query
|
2020-02-26 19:21:59 +00:00
|
|
|
if args['search']:
|
2020-02-27 17:29:26 +00:00
|
|
|
query = query.filter(Deliverynote.name.ilike(args['search'] + '%'))
|
2020-02-26 19:21:59 +00:00
|
|
|
dlvnote = query.paginate(per_page=6 if args['search'] else 30)
|
|
|
|
return things_response(
|
|
|
|
self.schema.dump(dlvnote.items, many=True, nested=0),
|
|
|
|
dlvnote.page, dlvnote.per_page, dlvnote.total, dlvnote.prev_num, dlvnote.next_num
|
|
|
|
)
|
|
|
|
|
|
|
|
def delete(self, id):
|
2020-02-27 17:29:26 +00:00
|
|
|
dlvnote = Deliverynote.query.filter_by(id=id).one()
|
2020-02-26 19:21:59 +00:00
|
|
|
dlvnote.delete()
|
|
|
|
db.session.commit()
|
|
|
|
return Response(status=204)
|