Makes user profile view editable

This commit is contained in:
Mouse Reeve 2021-04-01 08:32:06 -07:00
parent 31b7d267a4
commit e2388d8f67
4 changed files with 61 additions and 15 deletions

View file

@ -137,6 +137,19 @@ class EditUserForm(CustomForm):
help_texts = {f: None for f in fields}
class LimitedEditUserForm(CustomForm):
class Meta:
model = models.User
fields = [
"avatar",
"name",
"summary",
"manually_approves_followers",
"discoverable",
]
help_texts = {f: None for f in fields}
class TagForm(CustomForm):
class Meta:
model = models.Tag

View file

@ -1,10 +1,10 @@
""" test for app action functionality """
from unittest.mock import patch
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models
from bookwyrm import views
from bookwyrm import forms, models, views
class GetStartedViews(TestCase):
@ -41,6 +41,21 @@ class GetStartedViews(TestCase):
def test_profile_view_post(self):
""" save basic user details """
view = views.GetStartedProfile.as_view()
form = forms.LimitedEditUserForm(instance=self.local_user)
form.data["name"] = "New Name"
form.data["discoverable"] = "True"
request = self.factory.post("", form.data)
request.user = self.local_user
self.assertIsNone(self.local_user.name)
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
self.assertEqual(self.local_user.name, "New Name")
self.assertTrue(self.local_user.discoverable)
def test_books_view(self):
""" there are so many views, this just makes sure it LOADS """

View file

@ -1,6 +1,7 @@
""" Helping new users figure out the lay of the land """
from django.contrib.auth.decorators import login_required
from django.db.models import Count, Q
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
@ -8,6 +9,7 @@ from django.views import View
from bookwyrm import forms, models
from bookwyrm.connectors import connector_manager
from .helpers import get_suggested_users
from .user import save_user_form
# pylint: disable= no-self-use
@ -18,11 +20,22 @@ class GetStartedProfile(View):
def get(self, request):
""" basic profile info """
data = {
"form": forms.EditUserForm(instance=request.user),
"form": forms.LimitedEditUserForm(instance=request.user),
"next": "get-started-books",
}
return TemplateResponse(request, "get_started/profile.html", data)
def post(self, request):
""" update your profile """
form = forms.LimitedEditUserForm(
request.POST, request.FILES, instance=request.user
)
if not form.is_valid():
data = {"form": form, "next": "get-started-books"}
return TemplateResponse(request, "get_started/profile.html", data)
save_user_form(form)
return redirect('get-started-books')
@method_decorator(login_required, name="dispatch")
class GetStartedBooks(View):

View file

@ -163,21 +163,26 @@ class EditUser(View):
data = {"form": form, "user": request.user}
return TemplateResponse(request, "preferences/edit_user.html", data)
user = form.save(commit=False)
if "avatar" in form.files:
# crop and resize avatar upload
image = Image.open(form.files["avatar"])
image = crop_avatar(image)
# set the name to a hash
extension = form.files["avatar"].name.split(".")[-1]
filename = "%s.%s" % (uuid4(), extension)
user.avatar.save(filename, image, save=False)
user.save()
save_user_form(form)
return redirect(user.local_path)
def save_user_form(form):
""" special handling for the user form """
user = form.save(commit=False)
if "avatar" in form.files:
# crop and resize avatar upload
image = Image.open(form.files["avatar"])
image = crop_avatar(image)
# set the name to a hash
extension = form.files["avatar"].name.split(".")[-1]
filename = "%s.%s" % (uuid4(), extension)
user.avatar.save(filename, image, save=False)
user.save()
return user
def crop_avatar(image):
""" reduce the size and make an avatar square """