mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 11:59:23 +00:00
Make ProductType
an OrderedModel
This commit is contained in:
@ -129,7 +129,7 @@ class PermanencyAdmin(SearchModelAdmin):
|
||||
|
||||
@admin.register(ProductType)
|
||||
class ProductTypeAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "priority")
|
||||
list_display = ("name", "order")
|
||||
|
||||
|
||||
@admin.register(CashRegisterSummary)
|
||||
|
@ -71,7 +71,7 @@ class ProductController(ControllerBase):
|
||||
def search_products(self, filters: Query[ProductFilterSchema]):
|
||||
return filters.filter(
|
||||
Product.objects.order_by(
|
||||
F("product_type__priority").desc(nulls_last=True),
|
||||
F("product_type__order").asc(nulls_last=True),
|
||||
"product_type",
|
||||
"name",
|
||||
).values()
|
||||
@ -95,7 +95,7 @@ class ProductController(ControllerBase):
|
||||
.prefetch_related("buying_groups")
|
||||
.select_related("product_type")
|
||||
.order_by(
|
||||
F("product_type__priority").desc(nulls_last=True),
|
||||
F("product_type__order").asc(nulls_last=True),
|
||||
"product_type",
|
||||
"name",
|
||||
)
|
||||
|
@ -0,0 +1,62 @@
|
||||
# Generated by Django 4.2.17 on 2024-12-15 17:53
|
||||
|
||||
from django.db import migrations, models
|
||||
from django.db.migrations.state import StateApps
|
||||
|
||||
|
||||
def move_priority_to_order(apps: StateApps, schema_editor):
|
||||
"""Migrate the previous homemade `priority` to `OrderedModel.order`.
|
||||
|
||||
`priority` was a system were click managers set themselves the priority
|
||||
of a ProductType.
|
||||
The higher the priority, the higher it was to be displayed in the eboutic.
|
||||
Multiple product types could share the same priority, in which
|
||||
case they were ordered by alphabetic order.
|
||||
|
||||
The new field is unique per object, and works in the other way :
|
||||
the nearer from 0, the higher it should appear.
|
||||
"""
|
||||
ProductType = apps.get_model("counter", "ProductType")
|
||||
product_types = list(ProductType.objects.order_by("-priority", "name"))
|
||||
for order, product_type in enumerate(product_types):
|
||||
product_type.order = order
|
||||
ProductType.objects.bulk_update(product_types, ["order"])
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("counter", "0027_alter_refilling_payment_method")]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="producttype",
|
||||
name="comment",
|
||||
field=models.TextField(
|
||||
default="",
|
||||
help_text="A text that will be shown on the eboutic.",
|
||||
verbose_name="comment",
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="producttype",
|
||||
name="description",
|
||||
field=models.TextField(default="", verbose_name="description"),
|
||||
),
|
||||
migrations.AlterModelOptions(
|
||||
name="producttype",
|
||||
options={"ordering": ["order"], "verbose_name": "product type"},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="producttype",
|
||||
name="order",
|
||||
field=models.PositiveIntegerField(
|
||||
db_index=True, default=0, editable=False, verbose_name="order"
|
||||
),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.RunPython(
|
||||
move_priority_to_order,
|
||||
reverse_code=migrations.RunPython.noop,
|
||||
elidable=True,
|
||||
),
|
||||
migrations.RemoveField(model_name="producttype", name="priority"),
|
||||
]
|
@ -35,6 +35,7 @@ from django.utils import timezone
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django_countries.fields import CountryField
|
||||
from ordered_model.models import OrderedModel
|
||||
from phonenumber_field.modelfields import PhoneNumberField
|
||||
|
||||
from accounting.models import CurrencyField
|
||||
@ -289,26 +290,26 @@ class AccountDump(models.Model):
|
||||
)
|
||||
|
||||
|
||||
class ProductType(models.Model):
|
||||
class ProductType(OrderedModel):
|
||||
"""A product type.
|
||||
|
||||
Useful only for categorizing.
|
||||
"""
|
||||
|
||||
name = models.CharField(_("name"), max_length=30)
|
||||
description = models.TextField(_("description"), null=True, blank=True)
|
||||
comment = models.TextField(_("comment"), null=True, blank=True)
|
||||
description = models.TextField(_("description"), default="")
|
||||
comment = models.TextField(
|
||||
_("comment"),
|
||||
default="",
|
||||
help_text=_("A text that will be shown on the eboutic."),
|
||||
)
|
||||
icon = ResizedImageField(
|
||||
height=70, force_format="WEBP", upload_to="products", null=True, blank=True
|
||||
)
|
||||
|
||||
# priority holds no real backend logic but helps to handle the order in which
|
||||
# the items are to be shown to the user
|
||||
priority = models.PositiveIntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("product type")
|
||||
ordering = ["-priority", "name"]
|
||||
ordering = ["order"]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
@ -109,7 +109,7 @@ class ProductTypeCreateView(CounterAdminTabsMixin, CounterAdminMixin, CreateView
|
||||
"""A create view for the admins."""
|
||||
|
||||
model = ProductType
|
||||
fields = ["name", "description", "comment", "icon", "priority"]
|
||||
fields = ["name", "description", "comment", "icon"]
|
||||
template_name = "core/create.jinja"
|
||||
current_tab = "products"
|
||||
|
||||
@ -119,7 +119,7 @@ class ProductTypeEditView(CounterAdminTabsMixin, CounterAdminMixin, UpdateView):
|
||||
|
||||
model = ProductType
|
||||
template_name = "core/edit.jinja"
|
||||
fields = ["name", "description", "comment", "icon", "priority"]
|
||||
fields = ["name", "description", "comment", "icon"]
|
||||
pk_url_kwarg = "type_id"
|
||||
current_tab = "products"
|
||||
|
||||
@ -129,7 +129,7 @@ class ProductListView(CounterAdminTabsMixin, CounterAdminMixin, ListView):
|
||||
queryset = Product.objects.values("id", "name", "code", "product_type__name")
|
||||
template_name = "counter/product_list.jinja"
|
||||
ordering = [
|
||||
F("product_type__priority").desc(nulls_last=True),
|
||||
F("product_type__order").asc(nulls_last=True),
|
||||
"product_type",
|
||||
"name",
|
||||
]
|
||||
|
Reference in New Issue
Block a user