From 45c6978bc397691b22db0360b16b19979eac7dce Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Fri, 16 Dec 2022 16:38:52 -0700 Subject: User admin and LD schema fixes --- users/views/admin/__init__.py | 15 ++------ users/views/admin/users.py | 84 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 users/views/admin/users.py (limited to 'users/views') diff --git a/users/views/admin/__init__.py b/users/views/admin/__init__.py index bb70ff7..5ace04d 100644 --- a/users/views/admin/__init__.py +++ b/users/views/admin/__init__.py @@ -3,7 +3,7 @@ from django.utils.decorators import method_decorator from django.views.generic import FormView, RedirectView, TemplateView from users.decorators import admin_required -from users.models import Identity, User +from users.models import Identity from users.views.admin.domains import ( # noqa DomainCreate, DomainDelete, @@ -23,6 +23,7 @@ from users.views.admin.settings import ( # noqa TuningSettings, ) from users.views.admin.stator import Stator # noqa +from users.views.admin.users import UserEdit, UsersRoot # noqa @method_decorator(admin_required, name="dispatch") @@ -30,18 +31,6 @@ class AdminRoot(RedirectView): pattern_name = "admin_basic" -@method_decorator(admin_required, name="dispatch") -class Users(TemplateView): - - template_name = "admin/users.html" - - def get_context_data(self): - return { - "users": User.objects.order_by("email"), - "section": "users", - } - - @method_decorator(admin_required, name="dispatch") class Identities(TemplateView): diff --git a/users/views/admin/users.py b/users/views/admin/users.py new file mode 100644 index 0000000..fab4616 --- /dev/null +++ b/users/views/admin/users.py @@ -0,0 +1,84 @@ +from django import forms +from django.db import models +from django.shortcuts import get_object_or_404, redirect +from django.utils.decorators import method_decorator +from django.views.generic import FormView, ListView + +from users.decorators import admin_required +from users.models import User + + +@method_decorator(admin_required, name="dispatch") +class UsersRoot(ListView): + + template_name = "admin/users.html" + paginate_by = 50 + + def get(self, request, *args, **kwargs): + self.query = request.GET.get("query") + self.extra_context = { + "section": "users", + "query": self.query or "", + } + return super().get(request, *args, **kwargs) + + def get_queryset(self): + users = User.objects.annotate( + num_identities=models.Count("identities") + ).order_by("created") + if self.query: + users = users.filter(email__icontains=self.query) + return users + + +@method_decorator(admin_required, name="dispatch") +class UserEdit(FormView): + + template_name = "admin/user_edit.html" + extra_context = { + "section": "users", + } + + class form_class(forms.Form): + status = forms.ChoiceField( + choices=[ + ("normal", "Normal User"), + ("moderator", "Moderator"), + ("admin", "Admin"), + ("banned", "Banned"), + ] + ) + + def dispatch(self, request, id, *args, **kwargs): + self.user = get_object_or_404(User, id=id) + return super().dispatch(request, *args, **kwargs) + + def get_initial(self): + status = "normal" + if self.user.moderator: + status = "moderator" + if self.user.admin: + status = "admin" + if self.user.banned: + status = "banned" + return { + "email": self.user.email, + "status": status, + } + + def form_valid(self, form): + # Don't let them change themselves + if self.user == self.request.user: + return redirect(".") + status = form.cleaned_data["status"] + self.user.banned = status == "banned" + self.user.moderator = status == "moderator" + self.user.admin = status == "admin" + self.user.save() + return redirect(self.user.urls.admin) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["editing_user"] = self.user + context["same_user"] = self.user == self.request.user + return context -- cgit v1.2.3