aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/users/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-05-13 17:26:25 +0100
committerMatthew Lemon <y@yulqen.org>2024-05-13 17:26:25 +0100
commitefbbd480ddc62e695123d31c31d233b0df5155bd (patch)
treebc2fb465edd5050d83c97f280b1aac8e023fe3e5 /pyblackbird_cc/users/views.py
After first pre-commit processing
Diffstat (limited to 'pyblackbird_cc/users/views.py')
-rw-r--r--pyblackbird_cc/users/views.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/pyblackbird_cc/users/views.py b/pyblackbird_cc/users/views.py
new file mode 100644
index 0000000..26876a1
--- /dev/null
+++ b/pyblackbird_cc/users/views.py
@@ -0,0 +1,45 @@
+from django.contrib.auth.mixins import LoginRequiredMixin
+from django.contrib.messages.views import SuccessMessageMixin
+from django.urls import reverse
+from django.utils.translation import gettext_lazy as _
+from django.views.generic import DetailView
+from django.views.generic import RedirectView
+from django.views.generic import UpdateView
+
+from pyblackbird_cc.users.models import User
+
+
+class UserDetailView(LoginRequiredMixin, DetailView):
+ model = User
+ slug_field = "id"
+ slug_url_kwarg = "id"
+
+
+user_detail_view = UserDetailView.as_view()
+
+
+class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
+ model = User
+ fields = ["name"]
+ success_message = _("Information successfully updated")
+
+ def get_success_url(self):
+ # for mypy to know that the user is authenticated
+ assert self.request.user.is_authenticated
+ return self.request.user.get_absolute_url()
+
+ def get_object(self):
+ return self.request.user
+
+
+user_update_view = UserUpdateView.as_view()
+
+
+class UserRedirectView(LoginRequiredMixin, RedirectView):
+ permanent = False
+
+ def get_redirect_url(self):
+ return reverse("users:detail", kwargs={"pk": self.request.user.pk})
+
+
+user_redirect_view = UserRedirectView.as_view()