forked from mirrors/bookwyrm
Makes user profile view editable
This commit is contained in:
parent
31b7d267a4
commit
e2388d8f67
4 changed files with 61 additions and 15 deletions
|
@ -137,6 +137,19 @@ class EditUserForm(CustomForm):
|
||||||
help_texts = {f: None for f in fields}
|
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 TagForm(CustomForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Tag
|
model = models.Tag
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
""" test for app action functionality """
|
""" test for app action functionality """
|
||||||
|
from unittest.mock import patch
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import forms, models, views
|
||||||
from bookwyrm import views
|
|
||||||
|
|
||||||
|
|
||||||
class GetStartedViews(TestCase):
|
class GetStartedViews(TestCase):
|
||||||
|
@ -41,6 +41,21 @@ class GetStartedViews(TestCase):
|
||||||
|
|
||||||
def test_profile_view_post(self):
|
def test_profile_view_post(self):
|
||||||
""" save basic user details """
|
""" 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):
|
def test_books_view(self):
|
||||||
""" there are so many views, this just makes sure it LOADS """
|
""" there are so many views, this just makes sure it LOADS """
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
""" Helping new users figure out the lay of the land """
|
""" Helping new users figure out the lay of the land """
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.db.models import Count, Q
|
from django.db.models import Count, Q
|
||||||
|
from django.shortcuts import redirect
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
from django.views import View
|
from django.views import View
|
||||||
|
@ -8,6 +9,7 @@ from django.views import View
|
||||||
from bookwyrm import forms, models
|
from bookwyrm import forms, models
|
||||||
from bookwyrm.connectors import connector_manager
|
from bookwyrm.connectors import connector_manager
|
||||||
from .helpers import get_suggested_users
|
from .helpers import get_suggested_users
|
||||||
|
from .user import save_user_form
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable= no-self-use
|
# pylint: disable= no-self-use
|
||||||
|
@ -18,11 +20,22 @@ class GetStartedProfile(View):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
""" basic profile info """
|
""" basic profile info """
|
||||||
data = {
|
data = {
|
||||||
"form": forms.EditUserForm(instance=request.user),
|
"form": forms.LimitedEditUserForm(instance=request.user),
|
||||||
"next": "get-started-books",
|
"next": "get-started-books",
|
||||||
}
|
}
|
||||||
return TemplateResponse(request, "get_started/profile.html", data)
|
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")
|
@method_decorator(login_required, name="dispatch")
|
||||||
class GetStartedBooks(View):
|
class GetStartedBooks(View):
|
||||||
|
|
|
@ -163,6 +163,12 @@ class EditUser(View):
|
||||||
data = {"form": form, "user": request.user}
|
data = {"form": form, "user": request.user}
|
||||||
return TemplateResponse(request, "preferences/edit_user.html", data)
|
return TemplateResponse(request, "preferences/edit_user.html", data)
|
||||||
|
|
||||||
|
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)
|
user = form.save(commit=False)
|
||||||
|
|
||||||
if "avatar" in form.files:
|
if "avatar" in form.files:
|
||||||
|
@ -175,8 +181,7 @@ class EditUser(View):
|
||||||
filename = "%s.%s" % (uuid4(), extension)
|
filename = "%s.%s" % (uuid4(), extension)
|
||||||
user.avatar.save(filename, image, save=False)
|
user.avatar.save(filename, image, save=False)
|
||||||
user.save()
|
user.save()
|
||||||
|
return user
|
||||||
return redirect(user.local_path)
|
|
||||||
|
|
||||||
|
|
||||||
def crop_avatar(image):
|
def crop_avatar(image):
|
||||||
|
|
Loading…
Reference in a new issue