Simplify edit user function

This commit is contained in:
Mouse Reeve 2020-12-21 14:54:45 -08:00
parent 65e9afd271
commit e6105c6cb0
2 changed files with 36 additions and 16 deletions

View file

@ -238,6 +238,36 @@ class ViewActions(TestCase):
self.assertEqual(resp.template_name, 'password_reset.html')
self.assertTrue(models.PasswordReset.objects.exists())
def test_password_change(self):
''' change password '''
password_hash = self.local_user.password
request = self.factory.post('', {
'password': 'hi',
'confirm-password': 'hi'
})
request.user = self.local_user
resp = actions.password_change(request)
self.assertEqual(resp.template_name, 'user.html')
self.assertNotEqual(self.user.password, password_hash)
def test_password_change_mismatch(self):
''' change password '''
password_hash = self.local_user.password
request = self.factory.post('', {
'password': 'hi',
'confirm-password': 'hihi'
})
request.user = self.local_user
resp = actions.password_change(request)
self.assertEqual(resp.template_name, 'edit_user.html')
self.assertEqual(self.user.password, password_hash)
def test_edit_user(self):
''' use a form to update a user '''
def test_switch_edition(self):
''' updates user's relationships to a book '''
work = models.Work.objects.create(title='test work')

View file

@ -159,7 +159,7 @@ def password_change(request):
request.user.set_password(new_password)
request.user.save()
login(request, request.user)
return redirect('/user-edit')
return redirect('/user/%s' % request.user.localname)
@login_required
@ -168,14 +168,11 @@ def edit_profile(request):
''' les get fancy with images '''
form = forms.EditUserForm(request.POST, request.FILES)
if not form.is_valid():
data = {
'form': form,
'user': request.user,
}
data = {'form': form, 'user': request.user}
return TemplateResponse(request, 'edit_user.html', data)
request.user.name = form.data['name']
request.user.email = form.data['email']
user = form.save(commit=False)
if 'avatar' in form.files:
# crop and resize avatar upload
image = Image.open(form.files['avatar'])
@ -201,15 +198,8 @@ def edit_profile(request):
# set the name to a hash
extension = form.files['avatar'].name.split('.')[-1]
filename = '%s.%s' % (uuid4(), extension)
request.user.avatar.save(
filename,
ContentFile(output.getvalue())
)
request.user.summary = form.data['summary']
request.user.manually_approves_followers = \
form.cleaned_data['manually_approves_followers']
request.user.save()
user.avatar.save(filename, ContentFile(output.getvalue()))
user.save()
outgoing.handle_update_user(request.user)
return redirect('/user/%s' % request.user.localname)