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
|
|
|
|
|
2021-04-20 00:16:14 +00:00
|
|
|
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(
|
2021-08-29 15:14:53 +00:00
|
|
|
permission_required("bookwyrm.moderate_user", raise_exception=True),
|
2021-03-28 21:20:49 +00:00
|
|
|
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:
|
2021-04-17 17:56:26 +00:00
|
|
|
server = models.FederatedServer.objects.filter(server_name=server).first()
|
2021-03-28 21:27:56 +00:00
|
|
|
filters["federated_server"] = server
|
2021-04-17 17:56:26 +00:00
|
|
|
filters["federated_server__isnull"] = False
|
|
|
|
username = request.GET.get("username")
|
|
|
|
if username:
|
|
|
|
filters["username__icontains"] = username
|
2021-08-16 15:51:23 +00:00
|
|
|
scope = request.GET.get("scope")
|
2021-09-06 15:54:57 +00:00
|
|
|
if scope and scope == "local":
|
|
|
|
filters["local"] = True
|
2021-09-08 22:37:52 +00:00
|
|
|
email = request.GET.get("email")
|
|
|
|
if email:
|
|
|
|
filters["email__endswith"] = email
|
2021-03-28 21:27:56 +00:00
|
|
|
|
2021-04-17 17:56:26 +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)
|
2021-04-17 17:56:26 +00:00
|
|
|
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,
|
2021-04-17 17:56:26 +00:00
|
|
|
}
|
2021-04-19 22:12:36 +00:00
|
|
|
return TemplateResponse(request, "user_admin/user_admin.html", data)
|
2021-04-19 22:40:51 +00:00
|
|
|
|
2021-04-19 22:51:39 +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)
|
2021-04-20 00:16:14 +00:00
|
|
|
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"""
|
2021-04-20 00:16:14 +00:00
|
|
|
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)
|