Move preferences views into directory

This commit is contained in:
Mouse Reeve 2021-09-28 11:23:47 -07:00
parent e971638f14
commit 1b0a5232bc
7 changed files with 79 additions and 60 deletions

View file

@ -1,4 +1,5 @@
""" make sure all our nice views are available """ """ make sure all our nice views are available """
# site admin
from .admin.announcements import Announcements, Announcement, delete_announcement from .admin.announcements import Announcements, Announcement, delete_announcement
from .admin.dashboard import Dashboard from .admin.dashboard import Dashboard
from .admin.federation import Federation, FederatedServer from .admin.federation import Federation, FederatedServer
@ -19,13 +20,17 @@ from .admin.reports import (
) )
from .admin.site import Site from .admin.site import Site
from .admin.user_admin import UserAdmin, UserAdminList from .admin.user_admin import UserAdmin, UserAdminList
# user preferences
from .preferences.change_password import ChangePassword
from .preferences.edit_user import EditUser
from .preferences.delete_user import DeleteUser
# misc views
from .author import Author, EditAuthor from .author import Author, EditAuthor
from .block import Block, unblock from .block import Block, unblock
from .books import Book, EditBook, ConfirmEditBook from .books import Book, EditBook, ConfirmEditBook
from .books import upload_cover, add_description, resolve_book from .books import upload_cover, add_description, resolve_book
from .directory import Directory from .directory import Directory
from .discover import Discover from .discover import Discover
from .edit_user import EditUser, DeleteUser
from .editions import Editions, switch_edition from .editions import Editions, switch_edition
from .feed import DirectMessage, Feed, Replies, Status from .feed import DirectMessage, Feed, Replies, Status
from .follow import follow, unfollow from .follow import follow, unfollow
@ -47,7 +52,7 @@ from .reading import delete_readthrough, delete_progressupdate
from .reading import ReadingStatus from .reading import ReadingStatus
from .register import Register, ConfirmEmail, ConfirmEmailCode, resend_link from .register import Register, ConfirmEmail, ConfirmEmailCode, resend_link
from .rss_feed import RssFeed from .rss_feed import RssFeed
from .password import PasswordResetRequest, PasswordReset, ChangePassword from .password import PasswordResetRequest, PasswordReset
from .search import Search from .search import Search
from .shelf import Shelf from .shelf import Shelf
from .shelf import create_shelf, delete_shelf from .shelf import create_shelf, delete_shelf

View file

@ -13,7 +13,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 bookwyrm.suggested_users import suggested_users from bookwyrm.suggested_users import suggested_users
from .edit_user import save_user_form from .preferences.edit_user import save_user_form
# pylint: disable= no-self-use # pylint: disable= no-self-use

View file

@ -1,10 +1,8 @@
""" class views for password management """ """ class views for password management """
from django.contrib.auth import login from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.shortcuts import redirect 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.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views import View from django.views import View
@ -82,26 +80,3 @@ class PasswordReset(View):
login(request, user) login(request, user)
reset_code.delete() reset_code.delete()
return redirect("/") return redirect("/")
@method_decorator(login_required, name="dispatch")
class ChangePassword(View):
"""change password as logged in user"""
def get(self, request):
"""change password page"""
data = {"user": request.user}
return TemplateResponse(request, "preferences/change_password.html", data)
def post(self, request):
"""allow a user to change their password"""
new_password = request.POST.get("password")
confirm_password = request.POST.get("confirm-password")
if new_password != confirm_password:
return redirect("prefs-password")
request.user.set_password(new_password)
request.user.save(broadcast=False, update_fields=["password"])
login(request, request.user)
return redirect("user-feed", request.user.localname)

View file

View file

@ -0,0 +1,31 @@
""" class views for password management """
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
# pylint: disable= no-self-use
@method_decorator(login_required, name="dispatch")
class ChangePassword(View):
"""change password as logged in user"""
def get(self, request):
"""change password page"""
data = {"user": request.user}
return TemplateResponse(request, "preferences/change_password.html", data)
def post(self, request):
"""allow a user to change their password"""
new_password = request.POST.get("password")
confirm_password = request.POST.get("confirm-password")
if new_password != confirm_password:
return redirect("prefs-password")
request.user.set_password(new_password)
request.user.save(broadcast=False, update_fields=["password"])
login(request, request.user)
return redirect("user-feed", request.user.localname)

View file

@ -0,0 +1,38 @@
""" edit your own account """
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import forms, models
# pylint: disable=no-self-use
@method_decorator(login_required, name="dispatch")
class DeleteUser(View):
"""delete user view"""
def get(self, request):
"""delete page for a user"""
data = {
"form": forms.DeleteUserForm(),
"user": request.user,
}
return TemplateResponse(request, "preferences/delete_user.html", data)
def post(self, request):
"""les get fancy with images"""
form = forms.DeleteUserForm(request.POST, instance=request.user)
# idk why but I couldn't get check_password to work on request.user
user = models.User.objects.get(id=request.user.id)
if form.is_valid() and user.check_password(form.cleaned_data["password"]):
user.deactivation_reason = "self_deletion"
user.delete()
logout(request)
return redirect("/")
form.errors["password"] = ["Invalid password"]
data = {"form": form, "user": request.user}
return TemplateResponse(request, "preferences/delete_user.html", data)

View file

@ -1,9 +1,8 @@
""" edit or delete ones own account""" """ edit your own account """
from io import BytesIO from io import BytesIO
from uuid import uuid4 from uuid import uuid4
from PIL import Image from PIL import Image
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.shortcuts import redirect from django.shortcuts import redirect
@ -11,7 +10,7 @@ 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
from bookwyrm import forms, models from bookwyrm import forms
# pylint: disable=no-self-use # pylint: disable=no-self-use
@ -39,35 +38,6 @@ class EditUser(View):
return redirect("user-feed", request.user.localname) return redirect("user-feed", request.user.localname)
# pylint: disable=no-self-use
@method_decorator(login_required, name="dispatch")
class DeleteUser(View):
"""delete user view"""
def get(self, request):
"""delete page for a user"""
data = {
"form": forms.DeleteUserForm(),
"user": request.user,
}
return TemplateResponse(request, "preferences/delete_user.html", data)
def post(self, request):
"""les get fancy with images"""
form = forms.DeleteUserForm(request.POST, instance=request.user)
# idk why but I couldn't get check_password to work on request.user
user = models.User.objects.get(id=request.user.id)
if form.is_valid() and user.check_password(form.cleaned_data["password"]):
user.deactivation_reason = "self_deletion"
user.delete()
logout(request)
return redirect("/")
form.errors["password"] = ["Invalid password"]
data = {"form": form, "user": request.user}
return TemplateResponse(request, "preferences/delete_user.html", data)
def save_user_form(form): def save_user_form(form):
"""special handling for the user form""" """special handling for the user form"""
user = form.save(commit=False) user = form.save(commit=False)