remove using user email from back/front end

This commit is contained in:
SvyatoslavArtymovych 2023-04-20 17:37:38 +03:00
parent 35b3524823
commit 188682154f
13 changed files with 11 additions and 55 deletions

View File

@ -27,12 +27,13 @@ def init(app: Flask):
@app.cli.command("create-admin") @app.cli.command("create-admin")
def create_admin(): def create_admin():
"""Create super admin account""" """Create super admin account"""
if m.User.query.filter_by(email=app.config["ADMIN_EMAIL"]).first(): if m.User.query.filter_by(username=app.config["ADMIN_USERNAME"]).first():
print(f"User with e-mail: [{app.config['ADMIN_EMAIL']}] already exists") print(
f"User with username: [{app.config['ADMIN_USERNAME']}] already exists"
)
return return
m.User( m.User(
username=app.config["ADMIN_USERNAME"], username=app.config["ADMIN_USERNAME"],
email=app.config["ADMIN_EMAIL"],
password=app.config["ADMIN_PASSWORD"], password=app.config["ADMIN_PASSWORD"],
).save() ).save()
print("admin created") print("admin created")

View File

@ -14,7 +14,6 @@ from app import models as m
class UserForm(FlaskForm): class UserForm(FlaskForm):
next_url = StringField("next_url") next_url = StringField("next_url")
user_id = StringField("user_id", [DataRequired()]) user_id = StringField("user_id", [DataRequired()])
email = StringField("email", [DataRequired(), Email()])
activated = BooleanField("activated") activated = BooleanField("activated")
username = StringField("Username", [DataRequired()]) username = StringField("Username", [DataRequired()])
password = PasswordField("Password", validators=[DataRequired(), Length(6, 30)]) password = PasswordField("Password", validators=[DataRequired(), Length(6, 30)])
@ -36,18 +35,8 @@ class UserForm(FlaskForm):
): ):
raise ValidationError("This username is taken.") raise ValidationError("This username is taken.")
def validate_email(self, field):
if (
m.User.query.filter_by(email=field.data)
.filter(m.User.id != int(self.user_id.data))
.first()
is not None
):
raise ValidationError("This email is already registered.")
class NewUserForm(FlaskForm): class NewUserForm(FlaskForm):
email = StringField("email", [DataRequired(), Email()])
activated = BooleanField("activated") activated = BooleanField("activated")
username = StringField("Username", [DataRequired()]) username = StringField("Username", [DataRequired()])
password = PasswordField("Password", validators=[DataRequired(), Length(6, 30)]) password = PasswordField("Password", validators=[DataRequired(), Length(6, 30)])
@ -63,7 +52,3 @@ class NewUserForm(FlaskForm):
def validate_username(self, field): def validate_username(self, field):
if m.User.query.filter_by(username=field.data).first() is not None: if m.User.query.filter_by(username=field.data).first() is not None:
raise ValidationError("This username is taken.") raise ValidationError("This username is taken.")
def validate_email(self, field):
if m.User.query.filter_by(email=field.data).first() is not None:
raise ValidationError("This email is already registered.")

View File

@ -22,12 +22,8 @@ class User(db.Model, UserMixin, ModelMixin):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(60), unique=True, nullable=False) username = db.Column(db.String(60), unique=True, nullable=False)
email = db.Column(db.String(255), unique=True, nullable=False)
password_hash = db.Column(db.String(255), default="") password_hash = db.Column(db.String(255), default="")
activated = db.Column(db.Boolean, default=False)
created_at = db.Column(db.DateTime, default=datetime.now) created_at = db.Column(db.DateTime, default=datetime.now)
unique_id = db.Column(db.String(36), default=gen_password_reset_id)
reset_password_uid = db.Column(db.String(64), default=gen_password_reset_id)
@hybrid_property @hybrid_property
def password(self): def password(self):
@ -40,10 +36,7 @@ class User(db.Model, UserMixin, ModelMixin):
@classmethod @classmethod
def authenticate(cls, user_id, password): def authenticate(cls, user_id, password):
user = cls.query.filter( user = cls.query.filter(
db.or_( func.lower(cls.username) == func.lower(user_id),
func.lower(cls.username) == func.lower(user_id),
func.lower(cls.email) == func.lower(user_id),
)
).first() ).first()
if not user: if not user:
log(log.WARNING, "user:[%s] not found", user_id) log(log.WARNING, "user:[%s] not found", user_id)
@ -51,13 +44,8 @@ class User(db.Model, UserMixin, ModelMixin):
if user is not None and check_password_hash(user.password, password): if user is not None and check_password_hash(user.password, password):
return user return user
def reset_password(self):
self.password_hash = ""
self.reset_password_uid = gen_password_reset_id()
self.save()
def __repr__(self): def __repr__(self):
return f"<{self.id}: {self.username},{self.email}>" return f"<{self.id}: {self.username}>"
@property @property
def json(self): def json(self):

File diff suppressed because one or more lines are too long

View File

@ -314,11 +314,6 @@
<p class="text-sm text-gray-900 dark:text-white" role="none"> <p class="text-sm text-gray-900 dark:text-white" role="none">
{{current_user.username}} {{current_user.username}}
</p> </p>
<p
class="text-sm font-medium text-gray-900 truncate dark:text-gray-300"
role="none">
{{current_user.email}}
</p>
</div> </div>
<ul class="py-1" role="none"> <ul class="py-1" role="none">
<li> <li>

View File

@ -48,7 +48,6 @@
> >
<div class="pl-3"> <div class="pl-3">
<div class="text-base font-semibold">{{ user.username }}</div> <div class="text-base font-semibold">{{ user.username }}</div>
<div class="font-normal text-gray-500"><a class="hover:text-blue-500" href="mailto:{{ user.email }}">{{ user.email }}</a></div>
</div> </div>
</td> </td>
<td class="p-4 text-base font-normal text-gray-900 whitespace-nowrap dark:text-white"> <td class="p-4 text-base font-normal text-gray-900 whitespace-nowrap dark:text-white">

View File

@ -23,7 +23,7 @@ def get_all():
q = request.args.get("q", type=str, default=None) q = request.args.get("q", type=str, default=None)
users = m.User.query.order_by(m.User.id) users = m.User.query.order_by(m.User.id)
if q: if q:
users = users.filter(m.User.username.like(f"{q}%") | m.User.email.like(f"{q}%")) users = users.filter(m.User.username.like(f"{q}%"))
pagination = create_pagination(total=users.count()) pagination = create_pagination(total=users.count())
@ -45,7 +45,6 @@ def save():
log(log.ERROR, "Not found user by id : [%s]", form.user_id.data) log(log.ERROR, "Not found user by id : [%s]", form.user_id.data)
flash("Cannot save user data", "danger") flash("Cannot save user data", "danger")
u.username = form.username.data u.username = form.username.data
u.email = form.email.data
u.activated = form.activated.data u.activated = form.activated.data
if form.password.data.strip("*\n "): if form.password.data.strip("*\n "):
u.password = form.password.data u.password = form.password.data
@ -67,7 +66,6 @@ def create():
if form.validate_on_submit(): if form.validate_on_submit():
user = m.User( user = m.User(
username=form.username.data, username=form.username.data,
email=form.email.data,
password=form.password.data, password=form.password.data,
activated=form.activated.data, activated=form.activated.data,
) )

View File

@ -18,7 +18,6 @@ class BaseConfig(BaseSettings):
# Super admin # Super admin
ADMIN_USERNAME: str ADMIN_USERNAME: str
ADMIN_EMAIL: str
ADMIN_PASSWORD: str ADMIN_PASSWORD: str
# Pagination # Pagination

View File

@ -21,7 +21,6 @@ LOCAL_WEB_PORT=8080
# Super admin # Super admin
ADMIN_USERNAME=admin ADMIN_USERNAME=admin
ADMIN_EMAIL=simple2b.info@gmail.com
ADMIN_PASSWORD=admin ADMIN_PASSWORD=admin
# Pagination # Pagination

View File

@ -97,8 +97,6 @@ function editUser(user: IUser) {
input.value = user.username; input.value = user.username;
input = document.querySelector('#user-edit-id'); input = document.querySelector('#user-edit-id');
input.value = user.id.toString(); input.value = user.id.toString();
input = document.querySelector('#user-edit-email');
input.value = user.email;
input = document.querySelector('#user-edit-password'); input = document.querySelector('#user-edit-password');
input.value = '*******'; input.value = '*******';
input = document.querySelector('#user-edit-password_confirmation'); input = document.querySelector('#user-edit-password_confirmation');

View File

@ -50,7 +50,6 @@ def populate(client: FlaskClient):
for i in range(NUM_TEST_USERS): for i in range(NUM_TEST_USERS):
m.User( m.User(
username=f"user{i+1}", username=f"user{i+1}",
email=f"user{i+1}@mail.com",
password="password", password="password",
).save(False) ).save(False)
db.session.commit() db.session.commit()

View File

@ -40,7 +40,6 @@ def populate(count: int = NUM_TEST_USERS):
for username, email in gen_test_items(count): for username, email in gen_test_items(count):
m.User( m.User(
username=username, username=username,
email=email,
).save(False) ).save(False)
db.session.commit() db.session.commit()

View File

@ -5,10 +5,8 @@ TEST_ADMIN_EMAIL = "bob@test.com"
TEST_ADMIN_PASSWORD = "password" TEST_ADMIN_PASSWORD = "password"
def create( def create(username=TEST_ADMIN_NAME, password=TEST_ADMIN_PASSWORD):
username=TEST_ADMIN_NAME, email=TEST_ADMIN_EMAIL, password=TEST_ADMIN_PASSWORD user = User(username=username)
):
user = User(username=username, email=email)
user.password = password user.password = password
user.save() user.save()
return user.id return user.id