added profile page

This commit is contained in:
janphilippweinsheimer 2024-01-26 10:25:01 +01:00
parent 4fd34ba5bc
commit befdb5fc2b
4 changed files with 111 additions and 8 deletions

47
app.py
View File

@ -119,7 +119,6 @@ def logout():
# Create a new route
@app.route('/')
def index():
if current_user.is_authenticated:
habits = current_user.get_habits()
name = "Hallo " + current_user.name
@ -173,7 +172,7 @@ def habit_creation():
return render_template(
'habit.html',
title='Erstelle ein Habit',
unit=1,
unit="Woche",
errors={},
)
@ -251,6 +250,50 @@ def habit_create():
)"""
@app.route('/profile')
@login_required
def profile():
return render_template(
"profile.html",
name=current_user.name,
email=current_user.email,
errors={}
)
@app.route('/profile', methods=['POST'])
@login_required
def profile_change():
newName = request.form.get('newName')
newEmail = request.form.get('newEmail')
newPassword = request.form.get('newPassword')
oldPassword = request.form.get('oldPassword')
# Check for errors
errors = {}
if not newName and not newEmail and not newPassword:
errors['newName'] = 'Mindestens eine Änderung muss erfolgen.'
errors['newEmail'] = 'Mindestens eine Änderung muss erfolgen.'
errors['newPassword'] = 'Mindestens eine Änderung muss erfolgen.'
if not oldPassword:
errors['oldPassword'] = 'Du musst dein aktuelles Passwort angeben.'
print(errors)
if errors:
return render_template(
"profile.html",
name=current_user.name,
email=current_user.email,
errors=errors
)
# Save habit to database
# habit = Habit.create(current_user.id, name, times, note, unit)
# Back to index
return redirect(url_for('index'))
@app.route('/check', methods=['POST'])
@login_required
def check_habit():

View File

@ -7,14 +7,14 @@
<form action="/habit" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Name der Gewohnheit</label>
<input type="text" class="form-control {% if errors.get('name') %} is-invalid {% endif %}" id="name" name="name">
<input type="text" class="form-control {% if errors.get('name') %} is-invalid {% endif %}" id="name" name="name" value="{{name}}">
<div class="invalid-feedback">
{{ errors.get('name', '') }}
</div>
</div>
<div class="mb-3">
<label for="note" class="form-label">Beschreibung</label>
<input type="text" class="form-control {% if errors.get('note') %} is-invalid {% endif %}" id="note" name="note">
<input type="text" class="form-control {% if errors.get('note') %} is-invalid {% endif %}" id="note" name="note" value="{{note}}">
<div class="invalid-feedback">
{{ errors.get('note', '') }}
</div>
@ -23,7 +23,7 @@
<div class="row">
<div class="mb-3 col-2">
<label for="times" class="form-label">Häufigkeit</label>
<input type="number" min="1" class="form-control {% if errors.get('times') %} is-invalid {% endif %}" id="times" name="times">
<input type="number" min="1" class="form-control {% if errors.get('times') %} is-invalid {% endif %}" id="times" name="times" value="{{times}}">
<div class="invalid-feedback">
{{ errors.get('times', '') }}
</div>
@ -31,12 +31,23 @@
<div class="mb-3 col-10">
<label for="unit" class="form-label">Im Zeitraum</label>
<select class="form-select {% if errors.get('unit') %} is-invalid {% endif %}" id="unit" name="unit">
<option selected value="Woche">Woche</option>
<option value="Tag">Tag</option>
<option value="Woche">Woche</option>
<option value="Monat">Monat</option>
<option value="Jahr">Jahr</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
let selectedElement = document.getElementById('unit');
for (let option of selectedElement.options) {
if (option.value == '{{ unit }}') {
option.selected = true;
break;
}
}
});
</script>
<div class="invalid-feedback">
{{ errors.get('unit', '') }}
</div>

View File

@ -9,7 +9,7 @@
<link rel="stylesheet" href="/static/main.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<!-- Axios Libary-->
<!-- Axios Library-->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body style="background-color: White">
@ -35,6 +35,9 @@
<a class="btn btn-outline-secondary" aria-current="page" href="{{ url_for('signup') }}">Signup</a>
</li>
{% else %}
<li class="nav-item me-2">
<a class="btn text-white btn-primary" aria-current="page" href="{{ url_for('profile') }}">Profil</a>
</li>
<li class="nav-item me-2">
<a class="btn btn-primary" aria-current="page" href="{{ url_for('logout') }}">Logout</a>
</li>

46
templates/profile.html Normal file
View File

@ -0,0 +1,46 @@
{% extends 'layouts/main.html' %}
{% block content %}
<h1 class="mt-5">Account Einstellungen👤</h1>
<form action="/profile" method="POST">
<div class="form-group mb-3">
<label for="newName">Neuer Name:</label>
<input type="text" class="form-control {% if errors.get('newName') %} is-invalid {% endif %}" id="newName" name="newName" value="{{name}}">
<div class="invalid-feedback">
{{ errors.get('newName', '') }}
</div>
</div>
<div class="form-group mb-3">
<label for="newEmail">Neue E-Mail:</label>
<input type="email" class="form-control {% if errors.get('newEmail') %} is-invalid {% endif %}" id="newEmail" name="newEmail" value="{{email}}">
<div class="invalid-feedback">
{{ errors.get('newEmail', '') }}
</div>
</div>
<div class="form-group mb-3">
<label for="newPassword">Neues Passwort:</label>
<input type="password" class="form-control {% if errors.get('newPassword') %} is-invalid {% endif %}" id="newPassword" name="newPassword">
<div class="invalid-feedback">
{{ errors.get('newPassword', '') }}
</div>
</div>
<div class="form-group mb-3">
<label for="oldPassword">Altes Passwort:</label>
<input type="password" class="form-control {% if errors.get('oldPassword') %} is-invalid {% endif %}" id="oldPassword" name="oldPassword">
<div class="invalid-feedback">
{{ errors.get('oldPassword', '') }}
</div>
</div>
<button type="submit" class="btn btn-primary">Änderungen speichern</button>
</form>
{% endblock %}