From a54942a8a8c5d59b7b4b19af5f26047e1fc2b894 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 14 Oct 2022 15:47:54 +0200 Subject: [PATCH] add kangaroo form --- ereuse_devicehub/workbench/forms.py | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 ereuse_devicehub/workbench/forms.py diff --git a/ereuse_devicehub/workbench/forms.py b/ereuse_devicehub/workbench/forms.py new file mode 100644 index 00000000..648f5ceb --- /dev/null +++ b/ereuse_devicehub/workbench/forms.py @@ -0,0 +1,47 @@ +from flask import g +from flask_wtf import FlaskForm +from wtforms import StringField, validators + +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.device.models import Placeholder + + +class KangarooForm(FlaskForm): + phid = StringField('Phid', [validators.length(min=1)]) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.placeholder = None + self.kangaroos = Placeholder.query.filter( + Placeholder.kangaroo.is_(True) + ).filter(Placeholder.owner_id == g.user.id) + + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + if not is_valid: + return False + + if not self.placeholder: + self.placeholder = ( + Placeholder.query.filter(Placeholder.phid == self.phid.data) + .filter(Placeholder.owner_id == g.user.id) + .first() + ) + if self.placeholder: + if self.placeholder.status not in ['Snapshot', 'Twin']: + self.placeholder = None + + if not self.placeholder: + self.phid.errors = ["Device not exist"] + return False + + return True + + def save(self): + if not self.placeholder or self.placeholder.kangaroo: + return + + self.placeholder.kangaroo = True + db.session.commit() + return self.placeholder