Commit fcc979e2 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

add change password form

parent 124d2b53
......@@ -9,6 +9,7 @@ urlpatterns = [
path('', TemplateView.as_view(template_name='core/index.html'), name='home'),
path('admin/', admin.site.urls),
path('signup/', core_views.signup, name='signup'),
path('password/', core_views.change_password, name='change_password'),
path('profile/', core_views.profile, name='profile'),
path('login/', auth_views.login, {'template_name': 'login.html'}, name='login'),
path('logout/', auth_views.logout, {'next_page': 'login'}, name='logout'),
......
......@@ -131,11 +131,11 @@ table.snake-list {
height: 320px;
}
#login_form, #signup_form {
#login_form, #signup_form, #change_password_form {
margin-left: 200px;
}
#login_form div.segment, #signup_form div.segment {
#login_form div.segment, #signup_form div.segment, #change_password_form div.segment {
background: none;
width: 350px;
}
......
{% extends 'core/base.html' %}
{% block content %}
<h2>Change Password</h2>
<form method="post" id="change_password_form">
{% csrf_token %}
<div class="ui inverted segment">
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<div class="ui inverted mini form">
{% for field in form %}
<div class="fifteen wide field">
{{ field.label_tag }}
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text|safe }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</div>
{% endfor %}
<button type="submit" class="ui small positive button">Change password</button>
</div>
</div>
</form>
{% endblock %}
\ No newline at end of file
......@@ -3,6 +3,8 @@
{% block content %}
<h1>Profile</h1>
<h3>Password</h3>
<form method="GET" action="{% url "change_password" %}"><button type="submit">Change User Password</button></form>
<h3>API Keys</h3>
<form method="GET" action="{% url "api_keys_list" %}"><button type="submit">Manage API Keys</button></form>
<h3>Viewer Key</h3>
......
from django.contrib import messages
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import UserCreationForm, PasswordChangeForm
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from core.models import get_user_profile
......@@ -22,3 +24,21 @@ def signup(request):
@login_required
def profile(request):
return render(request, 'core/profile.html', {'user': request.user, 'profile': get_user_profile(request.user)})
@login_required
def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
user = form.save()
update_session_auth_hash(request, user) # Important!
messages.success(request, 'Your password was successfully updated!')
return redirect('change_password')
else:
messages.error(request, 'Please correct the error below.')
else:
form = PasswordChangeForm(request.user)
return render(request, 'core/change_password.html', {
'form': form
})
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment