chore: splitted views file into separated files

This commit is contained in:
2024-11-30 13:28:44 +01:00
parent e40cdee31b
commit e6680463ec
12 changed files with 99 additions and 98 deletions
+1 -2
View File
@@ -1,9 +1,8 @@
import os import os
from pathlib import Path from pathlib import Path
import environ
from shutil import which from shutil import which
import environ
BASE_DIR = Path(".") BASE_DIR = Path(".")
+2 -1
View File
@@ -1,7 +1,8 @@
# Generated by Django 5.1.3 on 2024-11-27 12:10 # Generated by Django 5.1.3 on 2024-11-27 12:10
import django.db.models.deletion
import uuid import uuid
import django.db.models.deletion
from django.conf import settings from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
+1 -2
View File
@@ -1,13 +1,12 @@
from decimal import Decimal from decimal import Decimal
from uuid import uuid4 from uuid import uuid4
from django.contrib.auth import get_user_model
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from django.utils.text import gettext_lazy as _ from django.utils.text import gettext_lazy as _
from django.utils.text import slugify from django.utils.text import slugify
from django.contrib.auth import get_user_model
User = get_user_model() User = get_user_model()
+11 -5
View File
@@ -2,16 +2,22 @@ from decimal import Decimal
from django.test import TestCase from django.test import TestCase
from shop.tests.mixins import CreateProductsMixin
from shop.models import Product
from shop.filters import ProductFilter from shop.filters import ProductFilter
from shop.models import Product
from shop.tests.mixins import CreateProductsMixin
class TestProductFilter(TestCase, CreateProductsMixin): class TestProductFilter(TestCase, CreateProductsMixin):
def setUp(self): def setUp(self):
product_a = self.create_product(name="producto 1", sku='001', price=Decimal("40.00")) product_a = self.create_product(
product_b = self.create_product(name="producto 2", sku='002', price=Decimal("70.00")) name="producto 1", sku="001", price=Decimal("40.00")
product_c = self.create_product(name="producto 3", sku='003', price=Decimal("100.00")) )
product_b = self.create_product(
name="producto 2", sku="002", price=Decimal("70.00")
)
product_c = self.create_product(
name="producto 3", sku="003", price=Decimal("100.00")
)
def test_product_filter_by_price(self): def test_product_filter_by_price(self):
f = ProductFilter({"price_gt": "40"}) f = ProductFilter({"price_gt": "40"})
+2 -4
View File
@@ -1,11 +1,9 @@
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.admin import GroupAdmin as BaseGroupAdmin from django.contrib.auth.admin import GroupAdmin as BaseGroupAdmin
from django.contrib.auth.models import User, Group from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import Group, User
from unfold.admin import ModelAdmin from unfold.admin import ModelAdmin
admin.site.unregister(User) admin.site.unregister(User)
admin.site.unregister(Group) admin.site.unregister(Group)
+1 -1
View File
@@ -34,5 +34,5 @@ class LoginForm(AuthenticationForm):
"class": "w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-primary-300 " "class": "w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-primary-300 "
"dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-primary-600 dark:ring-offset-gray-800" "dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-primary-600 dark:ring-offset-gray-800"
} }
) ),
) )
-3
View File
@@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.
+3 -5
View File
@@ -1,7 +1,8 @@
from django.urls import path from django.urls import path
from web.views import index, product_detail, add_cart_item, delete_cart_item, login, logout, register, reset_password from web.views.components import cart_dropdown, list_products
from web.components.views import list_products, cart_dropdown from web.views.users import login, logout, register, reset_password
from web.views.web import add_cart_item, delete_cart_item, index, product_detail
app_name = "web" app_name = "web"
@@ -9,17 +10,14 @@ app_name = "web"
urlpatterns = [ urlpatterns = [
path("", index, name="index"), path("", index, name="index"),
path("products/<int:pk>/<str:slug>/", product_detail, name="product_detail"), path("products/<int:pk>/<str:slug>/", product_detail, name="product_detail"),
# users # users
path("login/", login, name="login"), path("login/", login, name="login"),
path("logout/", logout, name="logout"), path("logout/", logout, name="logout"),
path("register/", register, name="register"), path("register/", register, name="register"),
path("reset-password/", reset_password, name="reset-password"), path("reset-password/", reset_password, name="reset-password"),
# components # components
path("web/components/products/", list_products, name="list_products"), path("web/components/products/", list_products, name="list_products"),
path("web/components/cart-dropdown/", cart_dropdown, name="cart_dropdown"), path("web/components/cart-dropdown/", cart_dropdown, name="cart_dropdown"),
# api # api
path("web/add-cart-item/", add_cart_item, name="add_cart_item"), path("web/add-cart-item/", add_cart_item, name="add_cart_item"),
path("web/delete-cart-item/<int:pk>/", delete_cart_item, name="delete_cart_item"), path("web/delete-cart-item/<int:pk>/", delete_cart_item, name="delete_cart_item"),
@@ -1,7 +1,8 @@
from django.shortcuts import render from django.shortcuts import render
from django.views.generic import TemplateView from django.views.generic import TemplateView
from shop.filters import ProductFilter from shop.filters import ProductFilter
from shop.models import Product, Cart, ProductPrice from shop.models import Cart, Product, ProductPrice
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
from web.utils import get_or_create_cart from web.utils import get_or_create_cart
@@ -9,7 +10,9 @@ from web.utils import get_or_create_cart
class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin): class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
template_name = "web/list_products.html" template_name = "web/list_products.html"
queryset = Product.objects.all().prefetch_related('images', ) queryset = Product.objects.all().prefetch_related(
"images",
)
filter_class = ProductFilter filter_class = ProductFilter
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
+68
View File
@@ -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()
+4 -72
View File
@@ -1,74 +1,11 @@
from django.shortcuts import get_object_or_404
from django.http.response import HttpResponse 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.decorators.http import require_http_methods
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 django.views.generic import TemplateView, View
from users.forms import LoginForm from shop.models import CartItem, Product
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
from web.utils import get_or_create_cart
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): class IndexView(TemplateView):
@@ -139,8 +76,3 @@ def delete_cart_item(request, pk, *args, **kwargs):
index = IndexView.as_view() index = IndexView.as_view()
product_detail = ProductDetail.as_view() product_detail = ProductDetail.as_view()
login = LoginView.as_view()
logout = LogoutView.as_view()
register = RegisterView.as_view()
reset_password = ResetPasswordView.as_view()