fix: ordering
This commit is contained in:
+16
-3
@@ -2,7 +2,7 @@ import django_filters
|
||||
from django.utils.text import gettext_lazy as _
|
||||
from django_filters import OrderingFilter
|
||||
|
||||
from shop.models import Product
|
||||
from shop.models import Product, ProductPrice
|
||||
|
||||
|
||||
class ProductFilter(django_filters.FilterSet):
|
||||
@@ -10,7 +10,21 @@ class ProductFilter(django_filters.FilterSet):
|
||||
field_name="name", lookup_expr="icontains", label=_("Nombre")
|
||||
)
|
||||
tags = django_filters.BaseInFilter(field_name="tags", label=_("Etiquetas"))
|
||||
ordering = OrderingFilter(fields={"name": "name"})
|
||||
price_lt = django_filters.NumberFilter(method="filter_price_lt")
|
||||
o = OrderingFilter(
|
||||
fields=(
|
||||
("name", "name"),
|
||||
("id", "id"),
|
||||
)
|
||||
)
|
||||
|
||||
def filter_price_lt(self, queryset, name, value):
|
||||
ids = queryset.values("id")
|
||||
product_ids = ProductPrice.objects.filter(
|
||||
product_id__in=ids, current=True, price_with_tax__lte=value
|
||||
).values("product_id")
|
||||
|
||||
return queryset.filter(pk__in=product_ids)
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
@@ -19,5 +33,4 @@ class ProductFilter(django_filters.FilterSet):
|
||||
"stock",
|
||||
"is_digital_asset",
|
||||
"tags",
|
||||
"ordering",
|
||||
)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Generated by Django 5.1.3 on 2024-11-24 12:26
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("shop", "0004_alter_product_options_product_slug"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="productprice",
|
||||
name="price_with_tax",
|
||||
field=models.DecimalField(
|
||||
blank=True,
|
||||
decimal_places=2,
|
||||
max_digits=11,
|
||||
null=True,
|
||||
verbose_name="precio con impuestos",
|
||||
),
|
||||
),
|
||||
]
|
||||
+10
-3
@@ -113,15 +113,22 @@ class ProductPrice(TimestampedModel):
|
||||
on_delete=models.PROTECT,
|
||||
verbose_name=_("impuesto aplicable"),
|
||||
)
|
||||
price_with_tax = models.DecimalField(
|
||||
max_digits=11,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
verbose_name=_("precio con impuestos"),
|
||||
)
|
||||
current = models.BooleanField(default=False, verbose_name=_("es el precio actual"))
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.price} - {self.tax.code}"
|
||||
|
||||
@property
|
||||
def price_with_tax(self) -> Decimal:
|
||||
def save(self, *args, **kwargs):
|
||||
tax_value = self.price * Decimal(self.tax.value / 100)
|
||||
return round(self.price + tax_value, 2)
|
||||
self.price_with_tax = round(self.price + tax_value, 2)
|
||||
super().save()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("precio de producto")
|
||||
|
||||
@@ -80,3 +80,19 @@ class TestLogin(APITestCase):
|
||||
response = self.client.post(reverse("users:logout"))
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("web:index")
|
||||
|
||||
def test_login_then_get_logout(self):
|
||||
response = self.client.post(
|
||||
reverse("users:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("web:index")
|
||||
|
||||
response = self.client.get(reverse("users:logout"))
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("web:index")
|
||||
|
||||
+1
-1
@@ -2,8 +2,8 @@ from django.shortcuts import get_object_or_404
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from shop.filters import ProductFilter
|
||||
from shop.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
|
||||
from shop.models import Product
|
||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
|
||||
|
||||
|
||||
class IndexView(TemplateView):
|
||||
|
||||
Reference in New Issue
Block a user