From c119c951f51bda10536e754ea82a9d60628c40cb Mon Sep 17 00:00:00 2001 From: emmdim Date: Wed, 18 Dec 2019 14:29:25 +0100 Subject: [PATCH] Propagates PATCH /lot updates to the involved devices --- ereuse_devicehub/resources/lot/views.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index b9681588..37ff1c8c 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -10,10 +10,11 @@ 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 -from ereuse_devicehub.resources.device.models import Device +from ereuse_devicehub.resources.device.models import Device, Computer from ereuse_devicehub.resources.lot.models import Lot, Path @@ -40,11 +41,16 @@ class LotView(View): return ret def patch(self, id): - patch_schema = self.resource_def.SCHEMA(only=('name', 'description', 'transfer_state', 'receiver_id', 'deposit', 'delivery_note_address'), partial=True) + patch_schema = self.resource_def.SCHEMA(only=('name', 'description', 'transfer_state', 'receiver_id', 'deposit', 'delivery_note_address', 'devices'), partial=True) l = request.get_json(schema=patch_schema) lot = Lot.query.filter_by(id=id).one() + device_fields = ['transfer_state', 'receiver_id', 'deposit', 'delivery_note_address'] + computers = [x for x in lot.all_devices if isinstance(x, Computer)] for key, value in l.items(): setattr(lot, key, value) + if key in device_fields: + for dev in computers: + setattr(dev, key, value) db.session.commit() return Response(status=204)