save data profile

This commit is contained in:
Cayo Puigdefabregas 2022-04-11 17:16:20 +02:00
parent 874ca77286
commit 1820b15255
3 changed files with 47 additions and 3 deletions

View file

@ -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()

View file

@ -135,6 +135,13 @@
<label for="company" class="col-md-4 col-lg-3 col-form-label">{{ f.label }}</label>
<div class="col-md-8 col-lg-9">
{{ f }}
{% if f.errors %}
<p class="text-danger">
{% for error in f.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
</div>
{% endif %}
@ -149,7 +156,7 @@
<div class="tab-pane fade pt-3" id="profile-settings">
<!-- Settings Form -->
<form>
<form class="d-none">
<div class="row mb-3">
<label for="fullName" class="col-md-4 col-lg-3 col-form-label">Email Notifications</label>

View file

@ -1,9 +1,10 @@
import flask
from flask import Blueprint
from flask import Blueprint, request
from flask.views import View
from flask_login import current_user, login_required, login_user, logout_user
from ereuse_devicehub import __version__
from ereuse_devicehub import __version__, messages
from ereuse_devicehub.db import db
from ereuse_devicehub.forms import LoginForm, ProfileForm
from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.utils import is_safe_url
@ -46,11 +47,15 @@ class LogoutView(View):
class UserProfileView(View):
methods = ['GET', 'POST']
decorators = [login_required]
template_name = 'ereuse_devicehub/user_profile.html'
def dispatch_request(self):
form = ProfileForm()
if request.method == 'GET':
form = ProfileForm(user=current_user.individual)
sessions = {s.created.strftime('%H:%M %d-%m-%Y') for s in current_user.sessions}
context = {
'current_user': current_user,
@ -58,6 +63,14 @@ class UserProfileView(View):
'version': __version__,
'profile_form': form,
}
if form.validate_on_submit():
form.save(commit=False)
messages.success('Modify user Profile datas successfully!')
elif form.errors:
messages.error('Error modify user Profile data!')
db.session.commit()
return flask.render_template(self.template_name, **context)