feat: ruff'ed

This commit is contained in:
2025-05-02 08:38:57 +02:00
parent 32b36b81b3
commit 48b53d851a
62 changed files with 2019 additions and 3655 deletions
+47 -81
View File
@@ -10,14 +10,7 @@ from django.views.generic import TemplateView
from config.mixins import HTMXFormComponent
from shop.filters import ProductFilter
from shop.models import (
CartItem,
CustomerAddress,
Product,
ProductPrice,
ShippingMethod,
WishlistedProduct,
)
from shop.models import CartItem, CustomerAddress, Product, ProductPrice, ShippingMethod, WishlistedProduct
from users.forms.change_password import ChangePasswordForm
from users.forms.info import UserInfoForm
from web.forms import CustomerAddressForm
@@ -27,10 +20,9 @@ from web.utils import get_or_create_cart
class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
template_name = "web/list_products.html"
template_name = 'web/list_products.html'
queryset = Product.objects.filter(hidden=False).prefetch_related(
"images",
Prefetch("prices", queryset=ProductPrice.objects.filter(current=True), to_attr="current_prices")
'images', Prefetch('prices', queryset=ProductPrice.objects.filter(current=True), to_attr='current_prices')
)
filter_class = ProductFilter
@@ -39,27 +31,21 @@ class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
page = self.get_paginated_queryset(qs)
return {
"current_page": page.number,
"num_pages": page.paginator.num_pages,
"page": page,
"has_next_page": page.has_next(),
"has_previous_page": page.has_previous(),
'current_page': page.number,
'num_pages': page.paginator.num_pages,
'page': page,
'has_next_page': page.has_next(),
'has_previous_page': page.has_previous(),
}
class ListWishlistedProducts(
TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin
):
template_name = "web/list_products.html"
queryset = Product.objects.filter(hidden=False).prefetch_related(
"images",
)
class ListWishlistedProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
template_name = 'web/list_products.html'
queryset = Product.objects.filter(hidden=False).prefetch_related('images')
filter_class = ProductFilter
def get_queryset(self):
product_ids = WishlistedProduct.objects.filter(
user=self.request.user
).values_list("product", flat=True)
product_ids = WishlistedProduct.objects.filter(user=self.request.user).values_list('product', flat=True)
return super().get_queryset().filter(pk__in=product_ids).distinct()
def get_context_data(self, **kwargs):
@@ -67,27 +53,21 @@ class ListWishlistedProducts(
page = self.get_paginated_queryset(qs)
return {
"current_page": page.number,
"num_pages": page.paginator.num_pages,
"page": page,
"has_next_page": page.has_next(),
"has_previous_page": page.has_previous(),
'current_page': page.number,
'num_pages': page.paginator.num_pages,
'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,
},
)
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, samesite="strict")
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid, samesite='strict')
return response
@@ -95,15 +75,13 @@ def cart(request, *args, **kwargs):
cart, created = get_or_create_cart(request)
items = CartItem.objects.filter(cart=cart)
base_total = Decimal("0.00")
tax_total = Decimal("0.00")
base_total = Decimal('0.00')
tax_total = Decimal('0.00')
for item in items:
price = ProductPrice.objects.filter(product=item.product, current=True).first()
base_total += price.price * item.quantity
tax_total += round(
price.price * Decimal(price.tax.value / 100) * item.quantity, 2
)
tax_total += round(price.price * Decimal(price.tax.value / 100) * item.quantity, 2)
total = base_total + tax_total
@@ -112,8 +90,7 @@ def cart(request, *args, **kwargs):
user=request.user, address_type=CustomerAddress.Types.BILLING, default=True
).first()
shipping_address = CustomerAddress.objects.filter(
user=request.user,
address_type=CustomerAddress.Types.SHIPPING,
user=request.user, address_type=CustomerAddress.Types.SHIPPING
).first()
else:
billing_address = None
@@ -123,62 +100,53 @@ def cart(request, *args, **kwargs):
response = render(
request,
"components/cart/cart.html",
'components/cart/cart.html',
context={
"cart": cart,
"items": items,
"total": total,
"base_total": base_total,
"tax_total": tax_total,
"billing_address": billing_address,
"shipping_address": shipping_address,
"shipping_methods": shipping_methods,
'cart': cart,
'items': items,
'total': total,
'base_total': base_total,
'tax_total': tax_total,
'billing_address': billing_address,
'shipping_address': shipping_address,
'shipping_methods': shipping_methods,
},
)
if not request.user.is_authenticated:
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid, samesite="strict")
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid, samesite='strict')
return response
class WishlistButton(TemplateView):
template_name = "components/products/wishlist_button.html"
template_name = 'components/products/wishlist_button.html'
def get_context_data(self, **kwargs):
product = get_object_or_404(Product, pk=self.kwargs.get("pk"))
is_wishlisted = WishlistedProduct.objects.filter(
user=self.request.user, product=product
).exists()
product = get_object_or_404(Product, pk=self.kwargs.get('pk'))
is_wishlisted = WishlistedProduct.objects.filter(user=self.request.user, product=product).exists()
return {
"is_wishlisted": is_wishlisted,
"product": product,
}
return {'is_wishlisted': is_wishlisted, 'product': product}
class UserInfoFormComponentView(HTMXFormComponent):
form_class = UserInfoForm
hx_trigger = "updated_user_info"
save_button_text = _("Actualizar datos")
hx_trigger = 'updated_user_info'
save_button_text = _('Actualizar datos')
def get_object(self) -> models.Model:
return self.request.user
def get_view_name(self):
return reverse("web:user_info_component")
return reverse('web:user_info_component')
def get_initial_values(self, instance):
return {
"email": instance.email,
"first_name": instance.first_name,
"last_name": instance.last_name,
}
return {'email': instance.email, 'first_name': instance.first_name, 'last_name': instance.last_name}
class ChangePasswordFormComponentView(HTMXFormComponent):
form_class = ChangePasswordForm
hx_trigger = "changed_password"
save_button_text = _("Cambiar contraseña")
hx_trigger = 'changed_password'
save_button_text = _('Cambiar contraseña')
def get_form(self, initial=True):
if initial:
@@ -190,16 +158,14 @@ class ChangePasswordFormComponentView(HTMXFormComponent):
return self.request.user
def get_view_name(self):
return reverse("web:change_password_component")
return reverse('web:change_password_component')
class ListCustomerAddressComponent(TemplateView):
template_name = "components/users/retrieve_update_address.html"
template_name = 'components/users/retrieve_update_address.html'
def get_context_data(self, **kwargs):
return {
"customer_addresses": CustomerAddress.objects.filter(user=self.request.user)
}
return {'customer_addresses': CustomerAddress.objects.filter(user=self.request.user)}
def update_customer_address(request, pk):
@@ -208,7 +174,7 @@ def update_customer_address(request, pk):
if form.is_valid():
form.save()
return HttpResponse(status=200, headers={"HX-Trigger": "updated_addresses"})
return HttpResponse(status=200, headers={'HX-Trigger': 'updated_addresses'})
return HttpResponse(form.errors, status=400)