feat: moved users views to web app
This commit is contained in:
+70
-2
@@ -1,13 +1,76 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views.generic import TemplateView
|
||||
from django.http.response import HttpResponse
|
||||
from django.views.decorators.http import require_http_methods
|
||||
|
||||
from shop.models import Product, Cart, CartItem
|
||||
from shop.models import Product, CartItem
|
||||
from web.utils import get_or_create_cart
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
|
||||
|
||||
from django.contrib.auth import login as login_user
|
||||
from django.contrib.auth import logout as logout_user
|
||||
from django.shortcuts import redirect, render, reverse
|
||||
from django.views.generic import TemplateView, View
|
||||
|
||||
from users.forms import LoginForm
|
||||
|
||||
|
||||
class RedirectionMixin:
|
||||
redirect_to = ""
|
||||
|
||||
def get_redirection(self):
|
||||
return reverse(self.redirect_to)
|
||||
|
||||
|
||||
class LoginView(View, RedirectionMixin):
|
||||
template_name = "users/login.html"
|
||||
redirect_to = "web:index"
|
||||
login_form_class = LoginForm
|
||||
|
||||
def get_login_form(self, request):
|
||||
return self.login_form_class(data=request.POST)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
if request.user.is_authenticated:
|
||||
return redirect(self.get_redirection())
|
||||
|
||||
login_form = self.login_form_class()
|
||||
return render(request, self.template_name, {"form": login_form})
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
login_form = self.get_login_form(request)
|
||||
|
||||
if login_form.is_valid():
|
||||
login_user(request, login_form.get_user())
|
||||
|
||||
if not login_form.cleaned_data.get('remember_me'):
|
||||
request.session.set_expiry(0)
|
||||
|
||||
return redirect(self.get_redirection())
|
||||
|
||||
return render(request, self.template_name, {"form": login_form}, status=400)
|
||||
|
||||
|
||||
class LogoutView(View, RedirectionMixin):
|
||||
redirect_to = "web:index"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
logout_user(request)
|
||||
return redirect(self.get_redirection())
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
logout_user(request)
|
||||
return redirect(self.get_redirection())
|
||||
|
||||
|
||||
class RegisterView(TemplateView):
|
||||
template_name = "web/register.html"
|
||||
|
||||
|
||||
class ResetPasswordView(TemplateView):
|
||||
template_name = "web/reset-password.html"
|
||||
|
||||
|
||||
class IndexView(TemplateView):
|
||||
template_name = "web/index.html"
|
||||
|
||||
@@ -76,3 +139,8 @@ def delete_cart_item(request, pk, *args, **kwargs):
|
||||
|
||||
index = IndexView.as_view()
|
||||
product_detail = ProductDetail.as_view()
|
||||
|
||||
login = LoginView.as_view()
|
||||
logout = LogoutView.as_view()
|
||||
register = RegisterView.as_view()
|
||||
reset_password = ResetPasswordView.as_view()
|
||||
|
||||
Reference in New Issue
Block a user