Revert all changes related to ProfileForm

We need to talk about the concepts of Agent and User and their
relationship before allowing the user to update their data.
This commit is contained in:
Santiago L 2022-04-28 12:03:12 +02:00
parent 3fd208c479
commit 9afe6bf9e6
3 changed files with 6 additions and 88 deletions

View file

@ -1,22 +1,11 @@
from flask import g
from flask_wtf import FlaskForm
from teal.enums import Country
from werkzeug.security import generate_password_hash
from wtforms import (
BooleanField,
EmailField,
PasswordField,
SelectField,
StringField,
validators,
)
from wtforms import BooleanField, EmailField, PasswordField, validators
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.agent.models import Agent
from ereuse_devicehub.resources.user.models import User
COUNTRY = [(x.name, x.value) for x in Country]
class LoginForm(FlaskForm):
email = EmailField('Email Address', [validators.Length(min=6, max=35)])
@ -74,60 +63,6 @@ class LoginForm(FlaskForm):
return user.is_active
class ProfileForm(FlaskForm):
name = StringField(
'First name',
[validators.Length(min=2, max=35)],
render_kw={'class': "form-control"},
)
email = StringField(
'Email Address',
[validators.Length(min=6, max=35)],
render_kw={'class': "form-control"},
)
telephone = StringField(
'Phone', [validators.Length(min=6, max=35)], render_kw={'class': "form-control"}
)
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.email.data = user.email
self.telephone.data = user.telephone
if user.country:
self.country.data = user.country.name
def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators)
if not is_valid:
return False
email = self.email.data
if email != g.user.individual.email:
if Agent.query.filter_by(email=email).first():
self.email.errors = ['You can not use this email.']
return False
return True
def save(self, commit=True):
agent = g.user.individual
agent.name = self.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()
class PasswordForm(FlaskForm):
password = PasswordField(
'Current Password',

View file

@ -69,7 +69,6 @@ class User(UserMixin, Thing):
@property
def individual(self):
"""The individual associated for this database, or None."""
if self.individuals:
return next(iter(self.individuals), None)
@property
@ -86,9 +85,8 @@ class User(UserMixin, Thing):
@property
def get_full_name(self):
if self.individuals:
return self.individual.get_full_name
# TODO(@slamora) create first_name & last_name fields???
# needs to be discussed related to Agent <--> User concepts
return self.email
def check_password(self, password):

View file

@ -1,11 +1,11 @@
import flask
from flask import Blueprint, request
from flask import Blueprint
from flask.views import View
from flask_login import current_user, login_required, login_user, logout_user
from ereuse_devicehub import __version__, messages
from ereuse_devicehub.db import db
from ereuse_devicehub.forms import LoginForm, PasswordForm, ProfileForm
from ereuse_devicehub.forms import LoginForm, PasswordForm
from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.utils import is_safe_url
@ -47,31 +47,16 @@ 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,
'sessions': sessions,
'version': __version__,
'profile_form': form,
'password_form': PasswordForm(),
}
if form.validate_on_submit():
form.save(commit=False)
messages.success('Modify user Profile datas successfully!')
db.session.commit()
elif form.errors:
messages.error('Error modifying user Profile data!')
return flask.render_template(self.template_name, **context)