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")
def create_admin():
"""Create super admin account"""
if m.User.query.filter_by(email=app.config["ADMIN_EMAIL"]).first():
print(f"User with e-mail: [{app.config['ADMIN_EMAIL']}] already exists")
if m.User.query.filter_by(username=app.config["ADMIN_USERNAME"]).first():
print(
f"User with username: [{app.config['ADMIN_USERNAME']}] already exists"
)
return
m.User(
username=app.config["ADMIN_USERNAME"],
email=app.config["ADMIN_EMAIL"],
password=app.config["ADMIN_PASSWORD"],
).save()
print("admin created")

View File

@ -14,7 +14,6 @@ from app import models as m
class UserForm(FlaskForm):
next_url = StringField("next_url")
user_id = StringField("user_id", [DataRequired()])
email = StringField("email", [DataRequired(), Email()])
activated = BooleanField("activated")
username = StringField("Username", [DataRequired()])
password = PasswordField("Password", validators=[DataRequired(), Length(6, 30)])
@ -36,18 +35,8 @@ class UserForm(FlaskForm):
):
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):
email = StringField("email", [DataRequired(), Email()])
activated = BooleanField("activated")
username = StringField("Username", [DataRequired()])
password = PasswordField("Password", validators=[DataRequired(), Length(6, 30)])
@ -63,7 +52,3 @@ class NewUserForm(FlaskForm):
def validate_username(self, field):
if m.User.query.filter_by(username=field.data).first() is not None:
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)
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="")
activated = db.Column(db.Boolean, default=False)
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
def password(self):
@ -40,10 +36,7 @@ class User(db.Model, UserMixin, ModelMixin):
@classmethod
def authenticate(cls, user_id, password):
user = cls.query.filter(
db.or_(
func.lower(cls.username) == func.lower(user_id),
func.lower(cls.email) == func.lower(user_id),
)
).first()
if not user:
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):
return user
def reset_password(self):
self.password_hash = ""
self.reset_password_uid = gen_password_reset_id()
self.save()
def __repr__(self):
return f"<{self.id}: {self.username},{self.email}>"
return f"<{self.id}: {self.username}>"
@property
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">
{{current_user.username}}
</p>
<p
class="text-sm font-medium text-gray-900 truncate dark:text-gray-300"
role="none">
{{current_user.email}}
</p>
</div>
<ul class="py-1" role="none">
<li>

View File

@ -48,7 +48,6 @@
>
<div class="pl-3">
<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>
</td>
<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)
users = m.User.query.order_by(m.User.id)
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())
@ -45,7 +45,6 @@ def save():
log(log.ERROR, "Not found user by id : [%s]", form.user_id.data)
flash("Cannot save user data", "danger")
u.username = form.username.data
u.email = form.email.data
u.activated = form.activated.data
if form.password.data.strip("*\n "):
u.password = form.password.data
@ -67,7 +66,6 @@ def create():
if form.validate_on_submit():
user = m.User(
username=form.username.data,
email=form.email.data,
password=form.password.data,
activated=form.activated.data,
)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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