moviewyrm/bookwyrm/views/user_admin.py

81 lines
2.7 KiB
Python
Raw Normal View History

2021-03-28 21:20:49 +00:00
""" manage user """
from django.contrib.auth.decorators import login_required, permission_required
from django.core.paginator import Paginator
2021-04-19 22:40:51 +00:00
from django.shortcuts import get_object_or_404
2021-03-28 21:20:49 +00:00
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import forms, models
2021-03-28 21:20:49 +00:00
from bookwyrm.settings import PAGE_LENGTH
# pylint: disable= no-self-use
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.moderate_users", raise_exception=True),
name="dispatch",
)
2021-04-19 22:40:51 +00:00
class UserAdminList(View):
2021-04-26 16:15:42 +00:00
"""admin view of users on this server"""
2021-03-28 21:20:49 +00:00
def get(self, request):
2021-04-26 16:15:42 +00:00
"""list of users"""
2021-03-28 21:27:56 +00:00
filters = {}
server = request.GET.get("server")
if server:
server = models.FederatedServer.objects.filter(server_name=server).first()
2021-03-28 21:27:56 +00:00
filters["federated_server"] = server
filters["federated_server__isnull"] = False
username = request.GET.get("username")
if username:
filters["username__icontains"] = username
scope = request.GET.get("scope")
if scope:
filters["local"] = scope == "local"
2021-03-28 21:27:56 +00:00
users = models.User.objects.filter(**filters)
2021-03-28 21:20:49 +00:00
sort = request.GET.get("sort", "-created_date")
sort_fields = [
"created_date",
"last_active_date",
"username",
"federated_server__server_name",
"is_active",
]
if sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]:
users = users.order_by(sort)
paginated = Paginator(users, PAGE_LENGTH)
data = {
2021-04-19 22:01:20 +00:00
"users": paginated.get_page(request.GET.get("page")),
2021-04-17 18:09:48 +00:00
"sort": sort,
"server": server,
}
return TemplateResponse(request, "user_admin/user_admin.html", data)
2021-04-19 22:40:51 +00:00
2021-04-19 22:40:51 +00:00
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.moderate_users", raise_exception=True),
name="dispatch",
)
class UserAdmin(View):
2021-04-26 16:15:42 +00:00
"""moderate an individual user"""
2021-04-19 22:40:51 +00:00
def get(self, request, user):
2021-04-26 16:15:42 +00:00
"""user view"""
2021-04-19 22:40:51 +00:00
user = get_object_or_404(models.User, id=user)
data = {"user": user, "group_form": forms.UserGroupForm()}
return TemplateResponse(request, "user_admin/user.html", data)
def post(self, request, user):
2021-04-26 16:15:42 +00:00
"""update user group"""
user = get_object_or_404(models.User, id=user)
form = forms.UserGroupForm(request.POST, instance=user)
if form.is_valid():
form.save()
data = {"user": user, "group_form": form}
return TemplateResponse(request, "user_admin/user.html", data)