<filename>assistant/users/views.py
import logging
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, RedirectView, UpdateView
from django.http import HttpRequest, JsonResponse

from assistant.core.pub_sub import Publisher, Consumer
from assistant.core.models import RemoteModel


logger = logging.getLogger(__name__)
User = get_user_model()


class UserDetailView(LoginRequiredMixin, DetailView):

    model = User
    slug_field = "username"
    slug_url_kwarg = "username"


user_detail_view = UserDetailView.as_view()


class UserUpdateView(LoginRequiredMixin, UpdateView):

    model = User
    fields = ["name"]

    def get_success_url(self):
        return reverse("users:detail", kwargs={"username": self.request.user.username})

    def get_object(self):
        return User.objects.get(username=self.request.user.username)

    def on_publisher_error(self, error):
        logger.error(f"Error Publishing User Update: {error}")

    def form_valid(self, form):
        messages.add_message(
            self.request, messages.INFO, _("Infos successfully updated")
        )
        publisher = Publisher(
            exchange_name="amq.direct",
            queue_name="users",
            routing_key="user.updated",
            error_callback=self.on_publisher_error,
            infinite_retry=True,
        )
        publisher.publish({"id": "1"})
        return super().form_valid(form)


user_update_view = UserUpdateView.as_view()


class UserRedirectView(LoginRequiredMixin, RedirectView):

    permanent = False

    def get_redirect_url(self):
        return reverse("users:detail", kwargs={"username": self.request.user.username})


user_redirect_view = UserRedirectView.as_view()


def user_email_exists(request: HttpRequest, email: str) -> JsonResponse:
    context = {"exists": User.objects.filter(email=email).exists()}
    return JsonResponse(context)

<filename>assistant/users/views.py
import logging
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, RedirectView, UpdateView
from django.http import HttpRequest, JsonResponse

from assistant.core.pub_sub import Publisher, Consumer
from assistant.core.models import RemoteModel


logger = logging.getLogger(__name__)
User = get_user_model()


class UserDetailView(LoginRequiredMixin, DetailView):

    model = User
    slug_field = "username"
    slug_url_kwarg = "username"


user_detail_view = UserDetailView.as_view()


class UserUpdateView(LoginRequiredMixin, UpdateView):

    model = User
    fields = ["name"]

    def get_success_url(self):
        return reverse("users:detail", kwargs={"username": self.request.user.username})

    def get_object(self):
        return User.objects.get(username=self.request.user.username)

    def on_publisher_error(self, error):
        logger.error(f"Error Publishing User Update: {error}")

    def form_valid(self, form):
        messages.add_message(
            self.request, messages.INFO, _("Infos successfully updated")
        )
        publisher = Publisher(
            exchange_name="amq.direct",
            queue_name="users",
            routing_key="user.updated",
            error_callback=self.on_publisher_error,
            infinite_retry=True,
        )
        publisher.publish({"id": "1"})
        return super().form_valid(form)


user_update_view = UserUpdateView.as_view()


class UserRedirectView(LoginRequiredMixin, RedirectView):

    permanent = False

    def get_redirect_url(self):
        return reverse("users:detail", kwargs={"username": self.request.user.username})


user_redirect_view = UserRedirectView.as_view()


def user_email_exists(request: HttpRequest, email: str) -> JsonResponse:
    context = {"exists": User.objects.filter(email=email).exists()}
    return JsonResponse(context)
