From 0ff2bcae92745f567da98d6cc85cba7f3da3472e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 7 Mar 2023 16:39:52 +0100 Subject: [PATCH] use populate_obj --- ereuse_devicehub/forms.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index 895160c5..495446b8 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -141,15 +141,17 @@ class SanitizationEntityForm(FlaskForm): return True def save(self, commit=True): - sanitation_data = SanitizationEntity( - logo=URL(self.logo.data), - company_name=self.company_name.data, - location=self.location.data, - responsable_person=self.responsable_person.data, - supervisor_person=self.supervisor_person.data, - user=g.user, - ) - db.session.add(sanitation_data) + if isinstance(self.logo.data, str): + self.logo.data = URL(self.logo.data) + + sanitation_data = SanitizationEntity.query.filter_by(user_id=g.user.id).first() + + if not sanitation_data: + sanitation_data = SanitizationEntity(user_id=g.user.id) + self.populate_obj(sanitation_data) + db.session.add(sanitation_data) + else: + self.populate_obj(sanitation_data) if commit: db.session.commit()