From 38db59b150d16e8619d0b1d034c051127e07cefa Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 27 Sep 2021 14:58:25 +0200 Subject: [PATCH] put the correct user un rol_user --- ereuse_devicehub/resources/action/schemas.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 6f18de75..02065081 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -2,7 +2,7 @@ import copy from datetime import datetime, timedelta from dateutil.tz import tzutc from flask import current_app as app, g -from marshmallow import Schema as MarshmallowSchema, ValidationError, fields as f, validates_schema +from marshmallow import Schema as MarshmallowSchema, ValidationError, fields as f, validates_schema, post_load from marshmallow.fields import Boolean, DateTime, Decimal, Float, Integer, Nested, String, \ TimeDelta, UUID from marshmallow.validate import Length, OneOf, Range @@ -426,6 +426,15 @@ class Ready(ActionWithMultipleDevices): class ActionStatus(ActionWithMultipleDevices): rol_user = NestedOn(s_user.User, dump_only=True, exclude=('token',)) + @post_load + def put_rol_user(self, data: dict): + for dev in data['devices']: + if dev.trading in [None, 'Revoke', 'ConfirmRevoke']: + return data + trade = [ac for ac in dev.actions if ac.t == 'Trade'][-1] + if trade.user_to != g.user: + data['rol_user'] = trade.user_to + class Recycling(ActionStatus): __doc__ = m.Recycling.__doc__