From e2388d8f673de2013a27ad3c91333699a9eaa93c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 1 Apr 2021 08:32:06 -0700 Subject: [PATCH] Makes user profile view editable --- bookwyrm/forms.py | 13 +++++++++++ bookwyrm/tests/views/test_get_started.py | 19 ++++++++++++++-- bookwyrm/views/get_started.py | 15 +++++++++++- bookwyrm/views/user.py | 29 ++++++++++++++---------- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index c0f4a923..1477234f 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -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 diff --git a/bookwyrm/tests/views/test_get_started.py b/bookwyrm/tests/views/test_get_started.py index 2b0dd751..11896f30 100644 --- a/bookwyrm/tests/views/test_get_started.py +++ b/bookwyrm/tests/views/test_get_started.py @@ -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 """ diff --git a/bookwyrm/views/get_started.py b/bookwyrm/views/get_started.py index a6fd7a72..7bf236c8 100644 --- a/bookwyrm/views/get_started.py +++ b/bookwyrm/views/get_started.py @@ -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): diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py index b8e3a86d..cce921be 100644 --- a/bookwyrm/views/user.py +++ b/bookwyrm/views/user.py @@ -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 """