Merge pull request #216 from mouse-reeve/change-password

Change password for logged in users
This commit is contained in:
Mouse Reeve 2020-10-02 15:50:48 -07:00 committed by GitHub
commit 827badac7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 10 deletions

View file

@ -25,6 +25,8 @@ class CustomForm(ModelForm):
input_type = visible.field.widget.input_type
if isinstance(visible.field.widget, Textarea):
input_type = 'textarea'
visible.field.widget.attrs['cols'] = None
visible.field.widget.attrs['rows'] = None
visible.field.widget.attrs['class'] = css_classes[input_type]
class LoginForm(CustomForm):
@ -96,7 +98,9 @@ class ReplyForm(CustomForm):
class EditUserForm(CustomForm):
class Meta:
model = models.User
fields = ['avatar', 'name', 'summary', 'manually_approves_followers']
fields = [
'avatar', 'name', 'email', 'summary', 'manually_approves_followers'
]
help_texts = {f: None for f in fields}

View file

@ -1,11 +1,51 @@
{% extends 'layout.html' %}
{% block content %}
<div class="block">
<h2 class="title">Edit Profile</h2>
<form name="avatar" action="/edit_profile/" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button class="button is-primary" type="submit">Update profile</button>
</form>
<div class="block columns">
<div class="column is-half">
<form name="edit-profile" action="/edit_profile/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h2 class="title">Profile</h2>
<p class="block">
<label class="label" for="id_avatar">Avatar:</label>
{{ form.avatar }}
</p>
<p class="block">
<label class="label" for="id_name">Display name:</label>
{{ form.name }}
</p>
<p class="block">
<label class="label" for="id_summary">Summary:</label>
{{ form.summary }}
</p>
<p class="block">
<label class="label" for="id_email">Email address:</label>
{{ form.email }}
</p>
<p class="block">
<label class="checkbox label" for="id_manually_approve_followers">
Manually approve followers:
{{ form.manually_approves_followers }}
</label>
</p>
<button class="button is-primary" type="submit">Save</button>
</form>
</div>
<div class="column is-half">
<div class="block">
<h2 class="title">Change password</h2>
<form name="edit-profile" action="/change-password/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p class="block">
<label class="label" for="id_password">New password:</label>
<input type="password" name="password" maxlength="128" class="input" required="" id="id_password">
</p>
<p class="block">
<label class="label" for="id_confirm_password">Confirm password:</label>
<input type="password" name="confirm-password" maxlength="128" class="input" required="" id="id_confirm_password">
</p>
<button class="button is-primary" type="submit">Change password</button>
</form>
</div>
</div>
</div>
{% endblock %}

View file

@ -85,6 +85,7 @@ urlpatterns = [
re_path(r'^user-register/?$', actions.register),
re_path(r'^reset-password-request/?$', actions.password_reset_request),
re_path(r'^reset-password/?$', actions.password_reset),
re_path(r'^change-password/?$', actions.password_change),
re_path(r'^edit_profile/?$', actions.edit_profile),

View file

@ -102,9 +102,8 @@ def password_reset_request(request):
return TemplateResponse(request, 'password_reset_request.html', data)
def password_reset(request):
''' allow a user to change their password '''
''' allow a user to change their password through an emailed token '''
try:
reset_code = models.PasswordReset.objects.get(
code=request.POST.get('reset-code')
@ -129,6 +128,21 @@ def password_reset(request):
return redirect('/')
@login_required
def password_change(request):
''' allow a user to change their password '''
new_password = request.POST.get('password')
confirm_password = request.POST.get('confirm-password')
if new_password != confirm_password:
return redirect('/user-edit')
request.user.set_password(new_password)
request.user.save()
login(request, request.user)
return redirect('/user-edit')
@login_required
def edit_profile(request):
''' les get fancy with images '''