diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index dca1044c..1bfd7907 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -1,3 +1,4 @@ +from flask import g from flask_wtf import FlaskForm from werkzeug.security import generate_password_hash from wtforms import ( @@ -9,6 +10,7 @@ from wtforms import ( validators, ) +from ereuse_devicehub.db import db from ereuse_devicehub.enums import Country from ereuse_devicehub.resources.user.models import User @@ -93,3 +95,25 @@ class ProfileForm(FlaskForm): country = SelectField( 'Country', choices=COUNTRY, default="es", render_kw={'class': "form-select"} ) + + def __init__(self, *args, **kwargs): + user = kwargs.pop('user', None) + super().__init__(*args, **kwargs) + if user: + self.name.data = user.name + self.last_name.data = user.last_name + self.email.data = user.email + self.telephone.data = user.telephone + self.country.data = user.country + + def save(self, commit=True): + agent = g.user.individual + agent.name = self.name.data + agent.last_name = self.last_name.data + agent.email = self.email.data + agent.telephone = self.telephone.data + agent.country = self.country.data + + db.session.add(agent) + if commit: + db.session.commit() diff --git a/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html b/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html index 4b6468ba..dba8db62 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html @@ -135,6 +135,13 @@
+ {% for error in f.errors %}
+ {{ error }}
+ {% endfor %}
+