Add a 'set password' form

This commit is contained in:
Oliver Walters 2019-05-14 13:58:23 +10:00
parent 0032ea3409
commit b996c03f61
5 changed files with 87 additions and 3 deletions

View File

@ -45,4 +45,30 @@ class EditUserForm(HelperForm):
'first_name',
'last_name',
'email'
]
]
class SetPasswordForm(HelperForm):
""" Form for setting user password
"""
enter_password = forms.CharField(max_length=100,
min_length=8,
required=True,
initial='',
widget=forms.PasswordInput(attrs={'autocomplete': 'off'}),
help_text='Enter new password')
confirm_password = forms.CharField(max_length=100,
min_length=8,
required=True,
initial='',
widget=forms.PasswordInput(attrs={'autocomplete': 'off'}),
help_text='Confirm new password')
class Meta:
model = User
fields = [
'enter_password',
'confirm_password'
]

View File

@ -30,7 +30,7 @@ from django.conf.urls.static import static
from django.views.generic.base import RedirectView
from rest_framework.documentation import include_docs_urls
from .views import IndexView, SearchView, SettingsView, EditUserView
from .views import IndexView, SearchView, SettingsView, EditUserView, SetPasswordView
from users.urls import user_urls
@ -65,6 +65,7 @@ urlpatterns = [
url(r'^settings/', SettingsView.as_view(), name='settings'),
url(r'^edit-user/', EditUserView.as_view(), name='edit-user'),
url(r'^set-password/', SetPasswordView.as_view(), name='set-password'),
url(r'^admin/', admin.site.urls, name='inventree-admin'),

View File

@ -17,7 +17,7 @@ from django.views.generic.base import TemplateView
from part.models import Part
from .forms import DeleteForm, EditUserForm
from .forms import DeleteForm, EditUserForm, SetPasswordForm
from .helpers import str2bool
from rest_framework import views
@ -382,6 +382,47 @@ class EditUserView(AjaxUpdateView):
return self.request.user
class SetPasswordView(AjaxUpdateView):
""" View for setting user password """
ajax_template_name = "InvenTree/password.html"
ajax_form_title = "Set Password"
form_class = SetPasswordForm
def get_object(self):
return self.request.user
def post(self, request, *args, **kwargs):
form = self.get_form()
valid = form.is_valid()
p1 = request.POST.get('enter_password', '')
p2 = request.POST.get('confirm_password', '')
if valid:
# Passwords must match
if not p1 == p2:
error = 'Password fields must match'
form.errors['enter_password'] = [error]
form.errors['confirm_password'] = [error]
valid = False
data = {
'form_valid': valid
}
if valid:
user = self.request.user
user.set_password(p1)
user.save()
return self.renderJsonResponse(request, form, data=data)
class IndexView(TemplateView):
""" View for InvenTree index page """

View File

@ -0,0 +1,7 @@
{% extends "modal_form.html" %}
{% block pre_form_content %}
{{ block.super }}
{% endblock %}

View File

@ -54,4 +54,13 @@ InvenTree | Settings
);
});
$("#edit-password").on('click', function() {
launchModalForm(
"{% url 'set-password' %}",
{
reload: true,
}
);
});
{% endblock %}