Fixed and finish profile update
This commit is contained in:
parent
1cdffa2453
commit
bcab78126a
31
app.py
31
app.py
@ -271,14 +271,18 @@ def profile_change():
|
|||||||
|
|
||||||
# Check for errors
|
# Check for errors
|
||||||
errors = {}
|
errors = {}
|
||||||
if not newName and not newEmail and not newPassword:
|
if not newName:
|
||||||
errors['newName'] = 'Mindestens eine Änderung muss erfolgen.'
|
errors['newName'] = 'Der Name ist erforderlich.'
|
||||||
errors['newEmail'] = 'Mindestens eine Änderung muss erfolgen.'
|
|
||||||
errors['newPassword'] = 'Mindestens eine Änderung muss erfolgen.'
|
if not newEmail:
|
||||||
|
errors['newEmail'] = 'Die E-Mail Adresse ist erforderlich.'
|
||||||
|
|
||||||
if not oldPassword:
|
if not oldPassword:
|
||||||
errors['oldPassword'] = 'Du musst dein aktuelles Passwort angeben.'
|
errors['oldPassword'] = 'Du musst dein aktuelles Passwort angeben.'
|
||||||
|
else:
|
||||||
|
if hashlib.sha256(oldPassword.encode()).hexdigest() != current_user.password:
|
||||||
|
errors['oldPassword'] = 'Das Passwort ist falsch.'
|
||||||
|
|
||||||
print(errors)
|
|
||||||
if errors:
|
if errors:
|
||||||
return render_template(
|
return render_template(
|
||||||
"profile.html",
|
"profile.html",
|
||||||
@ -287,11 +291,20 @@ def profile_change():
|
|||||||
errors=errors
|
errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
# Save habit to database
|
# Update user
|
||||||
# habit = Habit.create(current_user.id, name, times, note, unit)
|
current_user.name = newName
|
||||||
|
current_user.email = newEmail
|
||||||
|
if newPassword:
|
||||||
|
current_user.password = hashlib.sha256(newPassword.encode()).hexdigest()
|
||||||
|
current_user.update()
|
||||||
|
|
||||||
# Back to index
|
# Back to profile
|
||||||
return redirect(url_for('index'))
|
return render_template(
|
||||||
|
"profile.html",
|
||||||
|
name=current_user.name,
|
||||||
|
email=current_user.email,
|
||||||
|
errors={}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/check', methods=['POST'])
|
@app.route('/check', methods=['POST'])
|
||||||
|
|||||||
@ -42,12 +42,15 @@ def get_user_by_email(email: str):
|
|||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
def update_user(id: int, name: str, email: str, password: str):
|
def update_user(id: int, name: str, email: str, password: str = None):
|
||||||
query = f"UPDATE users SET name = {name}, email = {email}, password = {password} WHERE id = {id};"
|
if password:
|
||||||
|
query = f"UPDATE users SET name = '{name}', email = '{email}', password = '{password}' WHERE id = {id};"
|
||||||
|
else:
|
||||||
|
query = f"UPDATE users SET name = '{name}', email = '{email}' WHERE id = {id};"
|
||||||
conn = con3()
|
conn = con3()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
user = cursor.fetchone()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
return cursor.lastrowid
|
return cursor.lastrowid
|
||||||
|
|
||||||
|
|||||||
@ -25,14 +25,8 @@ class User(UserMixin):
|
|||||||
user = get_user_by_email(email)
|
user = get_user_by_email(email)
|
||||||
return User(user[0], user[1], user[2], user[3]) if user else None
|
return User(user[0], user[1], user[2], user[3]) if user else None
|
||||||
|
|
||||||
def update(self, name: str = None, email: str = None, password: str = None):
|
def update(self):
|
||||||
update_user(self.id, name, email, password)
|
update_user(self.id, self.name, self.email)
|
||||||
if name is not None:
|
|
||||||
self.name = name
|
|
||||||
if email is not None:
|
|
||||||
self.email = email
|
|
||||||
if password is not None:
|
|
||||||
self.password = password
|
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
delete_user(self.id)
|
delete_user(self.id)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user