mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-29 11:30:39 +00:00
Simplify edit user function
This commit is contained in:
parent
65e9afd271
commit
e6105c6cb0
2 changed files with 36 additions and 16 deletions
|
@ -238,6 +238,36 @@ class ViewActions(TestCase):
|
||||||
self.assertEqual(resp.template_name, 'password_reset.html')
|
self.assertEqual(resp.template_name, 'password_reset.html')
|
||||||
self.assertTrue(models.PasswordReset.objects.exists())
|
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):
|
def test_switch_edition(self):
|
||||||
''' updates user's relationships to a book '''
|
''' updates user's relationships to a book '''
|
||||||
work = models.Work.objects.create(title='test work')
|
work = models.Work.objects.create(title='test work')
|
||||||
|
|
|
@ -159,7 +159,7 @@ def password_change(request):
|
||||||
request.user.set_password(new_password)
|
request.user.set_password(new_password)
|
||||||
request.user.save()
|
request.user.save()
|
||||||
login(request, request.user)
|
login(request, request.user)
|
||||||
return redirect('/user-edit')
|
return redirect('/user/%s' % request.user.localname)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -168,14 +168,11 @@ def edit_profile(request):
|
||||||
''' les get fancy with images '''
|
''' les get fancy with images '''
|
||||||
form = forms.EditUserForm(request.POST, request.FILES)
|
form = forms.EditUserForm(request.POST, request.FILES)
|
||||||
if not form.is_valid():
|
if not form.is_valid():
|
||||||
data = {
|
data = {'form': form, 'user': request.user}
|
||||||
'form': form,
|
|
||||||
'user': request.user,
|
|
||||||
}
|
|
||||||
return TemplateResponse(request, 'edit_user.html', data)
|
return TemplateResponse(request, 'edit_user.html', data)
|
||||||
|
|
||||||
request.user.name = form.data['name']
|
user = form.save(commit=False)
|
||||||
request.user.email = form.data['email']
|
|
||||||
if 'avatar' in form.files:
|
if 'avatar' in form.files:
|
||||||
# crop and resize avatar upload
|
# crop and resize avatar upload
|
||||||
image = Image.open(form.files['avatar'])
|
image = Image.open(form.files['avatar'])
|
||||||
|
@ -201,15 +198,8 @@ def edit_profile(request):
|
||||||
# set the name to a hash
|
# set the name to a hash
|
||||||
extension = form.files['avatar'].name.split('.')[-1]
|
extension = form.files['avatar'].name.split('.')[-1]
|
||||||
filename = '%s.%s' % (uuid4(), extension)
|
filename = '%s.%s' % (uuid4(), extension)
|
||||||
request.user.avatar.save(
|
user.avatar.save(filename, ContentFile(output.getvalue()))
|
||||||
filename,
|
user.save()
|
||||||
ContentFile(output.getvalue())
|
|
||||||
)
|
|
||||||
|
|
||||||
request.user.summary = form.data['summary']
|
|
||||||
request.user.manually_approves_followers = \
|
|
||||||
form.cleaned_data['manually_approves_followers']
|
|
||||||
request.user.save()
|
|
||||||
|
|
||||||
outgoing.handle_update_user(request.user)
|
outgoing.handle_update_user(request.user)
|
||||||
return redirect('/user/%s' % request.user.localname)
|
return redirect('/user/%s' % request.user.localname)
|
||||||
|
|
Loading…
Reference in a new issue