From 9afe6bf9e6a21241e147953821cdccdab744626e Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 28 Apr 2022 12:03:12 +0200 Subject: [PATCH] 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. --- ereuse_devicehub/forms.py | 67 +---------------------- ereuse_devicehub/resources/user/models.py | 8 +-- ereuse_devicehub/views.py | 19 +------ 3 files changed, 6 insertions(+), 88 deletions(-) diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index 4c614c95..0f4cefbe 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -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', diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index a02efd2b..5eadb21d 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -69,8 +69,7 @@ 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) + return next(iter(self.individuals), None) @property def code(self): @@ -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): diff --git a/ereuse_devicehub/views.py b/ereuse_devicehub/views.py index 12f6d4ba..1c975285 100644 --- a/ereuse_devicehub/views.py +++ b/ereuse_devicehub/views.py @@ -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)