chore: splitted views file into separated files
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
from django.shortcuts import render
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from shop.filters import ProductFilter
|
||||
from shop.models import Cart, Product, ProductPrice
|
||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
|
||||
|
||||
class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
|
||||
template_name = "web/list_products.html"
|
||||
queryset = Product.objects.all().prefetch_related(
|
||||
"images",
|
||||
)
|
||||
filter_class = ProductFilter
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
qs = self.get_queryset()
|
||||
page = self.get_paginated_queryset(qs)
|
||||
|
||||
return {
|
||||
"page": page,
|
||||
"has_next_page": page.has_next(),
|
||||
"has_previous_page": page.has_previous(),
|
||||
}
|
||||
|
||||
|
||||
def cart_dropdown(request, *args, **kwargs):
|
||||
cart, created = get_or_create_cart(request)
|
||||
|
||||
response = render(
|
||||
request,
|
||||
"components/cart/cart_navbar.html",
|
||||
context={
|
||||
"cart": cart,
|
||||
},
|
||||
)
|
||||
|
||||
if not request.user.is_authenticated:
|
||||
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid)
|
||||
return response
|
||||
|
||||
|
||||
list_products = ListProducts.as_view()
|
||||
@@ -0,0 +1,68 @@
|
||||
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"
|
||||
|
||||
|
||||
login = LoginView.as_view()
|
||||
logout = LogoutView.as_view()
|
||||
register = RegisterView.as_view()
|
||||
reset_password = ResetPasswordView.as_view()
|
||||
@@ -0,0 +1,78 @@
|
||||
from django.http.response import HttpResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.views.generic import TemplateView, View
|
||||
|
||||
from shop.models import CartItem, Product
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
|
||||
|
||||
class IndexView(TemplateView):
|
||||
template_name = "web/index.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return {
|
||||
"title": "Shoppy",
|
||||
}
|
||||
|
||||
|
||||
class ProductDetail(TemplateView):
|
||||
template_name = "web/product_detail.html"
|
||||
|
||||
def get_context_data(self, pk, slug, **kwargs):
|
||||
product = get_object_or_404(Product, pk=pk)
|
||||
|
||||
return {
|
||||
"product": product,
|
||||
"title": product.name,
|
||||
"description": product.description,
|
||||
"image": product.images.first(),
|
||||
}
|
||||
|
||||
|
||||
@require_http_methods(
|
||||
[
|
||||
"POST",
|
||||
]
|
||||
)
|
||||
def add_cart_item(request, *args, **kwargs):
|
||||
product = get_object_or_404(Product, pk=request.POST.get("product"))
|
||||
cart, created = get_or_create_cart(request)
|
||||
response = HttpResponse(status=201, headers={"HX-Trigger": "updated-cart"})
|
||||
|
||||
if created and request.user.is_anonymous:
|
||||
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid)
|
||||
|
||||
# Comprobamos si existe una línea de carrito para ese carrito de ese producto
|
||||
existing_cart_item = CartItem.objects.filter(cart=cart, product=product).first()
|
||||
|
||||
# Si existe, simplemente le sumamos la cantidad a la línea ya existente
|
||||
if existing_cart_item is not None:
|
||||
existing_cart_item.quantity += int(request.POST.get("quantity"))
|
||||
existing_cart_item.save()
|
||||
# Si no, lo creamos
|
||||
else:
|
||||
CartItem.objects.create(
|
||||
cart=cart,
|
||||
quantity=request.POST.get("quantity"),
|
||||
product=product,
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@require_http_methods(
|
||||
[
|
||||
"POST",
|
||||
]
|
||||
)
|
||||
def delete_cart_item(request, pk, *args, **kwargs):
|
||||
cart, created = get_or_create_cart(request)
|
||||
cart_item = get_object_or_404(CartItem, pk=pk, cart=cart)
|
||||
cart_item.delete()
|
||||
return HttpResponse(status=204, headers={"HX-Trigger": "updated-cart"})
|
||||
|
||||
|
||||
index = IndexView.as_view()
|
||||
product_detail = ProductDetail.as_view()
|
||||
Reference in New Issue
Block a user