2024-03-25 11:59:06 +00:00
|
|
|
from django.contrib import messages
|
2024-03-26 12:18:47 +00:00
|
|
|
from django.contrib.auth.views import LoginView as BaseLoginView
|
|
|
|
|
from django.contrib.auth.views import LogoutView as BaseLogoutView
|
2024-03-25 10:36:10 +00:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
|
from django.urls import reverse_lazy
|
2024-03-26 12:18:47 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2024-03-25 10:36:10 +00:00
|
|
|
|
2024-03-26 17:05:43 +00:00
|
|
|
from moku.forms.auth import AuthenticationForm
|
2024-03-25 22:52:53 +00:00
|
|
|
from moku.views.base import View
|
2024-03-25 10:36:10 +00:00
|
|
|
|
2024-03-25 22:52:53 +00:00
|
|
|
|
|
|
|
|
class LoginView(View, BaseLoginView):
|
2024-03-26 12:44:19 +00:00
|
|
|
"""Allows users to log in by username and password."""
|
|
|
|
|
|
2024-03-25 10:36:10 +00:00
|
|
|
template_name = "moku/login.jinja"
|
2024-03-26 17:05:43 +00:00
|
|
|
form_class = AuthenticationForm
|
2024-03-26 16:51:55 +00:00
|
|
|
page_title = "log in"
|
2024-03-25 10:36:10 +00:00
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
if self.request.user.is_authenticated:
|
|
|
|
|
return redirect(self.get_success_url())
|
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2024-03-25 11:59:06 +00:00
|
|
|
if self.request.user.is_authenticated:
|
2024-03-26 12:18:47 +00:00
|
|
|
messages.success(
|
|
|
|
|
self.request,
|
|
|
|
|
_("welcome back, %(username)s!")
|
|
|
|
|
% {"username": self.request.user.username},
|
|
|
|
|
)
|
2024-03-25 10:36:10 +00:00
|
|
|
return self.request.GET.get("next", reverse_lazy("feed"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LogoutView(BaseLogoutView):
|
2024-03-26 12:44:19 +00:00
|
|
|
"""Logs the user out and redirect them to the feed."""
|
|
|
|
|
|
2024-03-25 10:36:10 +00:00
|
|
|
next_page = "feed"
|