diff options
Diffstat (limited to 'users/views')
-rw-r--r-- | users/views/admin/__init__.py | 15 | ||||
-rw-r--r-- | users/views/admin/users.py | 84 |
2 files changed, 86 insertions, 13 deletions
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") @@ -31,18 +32,6 @@ class AdminRoot(RedirectView): @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): template_name = "admin/identities.html" 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 |