Merge pull request #1289 from ae-utbm/product_history

Product history
This commit is contained in:
thomas girod
2026-02-17 22:05:59 +01:00
committed by GitHub
5 changed files with 86 additions and 2 deletions

View File

@@ -39,8 +39,9 @@ class ProductAdmin(SearchModelAdmin):
"code", "code",
"product_type", "product_type",
"selling_price", "selling_price",
"profit",
"archived", "archived",
"created_at",
"updated_at",
) )
list_select_related = ("product_type",) list_select_related = ("product_type",)
search_fields = ("name", "code") search_fields = ("name", "code")

View File

@@ -0,0 +1,67 @@
# Generated by Django 5.2.8 on 2026-02-10 15:40
from operator import attrgetter
import django.utils.timezone
from django.db import migrations, models
from django.db.migrations.state import StateApps
from django.db.models import OuterRef, Subquery
from counter.models import Selling
def apply_product_history_dates(apps: StateApps, schema_editor):
"""Approximate a posteriori the value of created_at and updated_at."""
Product = apps.get_model("counter", "Product")
sales_subquery = Selling.objects.filter(product=OuterRef("pk")).values("date")
# for products that have an associated sale, we set the creation date
# to the one of the first sale, and the update date to the one of the last sale
products = list(
Product.objects.exclude(sellings=None)
.annotate(
new_created_at=Subquery(sales_subquery.order_by("date")[:1]),
new_updated_at=Subquery(sales_subquery.order_by("-date")[:1]),
)
.only("id")
)
for product in products:
product.created_at = product.new_created_at
product.updated_at = product.new_updated_at
# For the remaining products (those without sale),
# they are given the creation and update date of the previous product having sales.
products_without_sale = list(Product.objects.filter(sellings=None).only("id"))
for product in products_without_sale:
previous_product = max(
(p for p in products if p.id < product.id), key=attrgetter("id")
)
product.created_at = previous_product.created_at
product.updated_at = previous_product.updated_at
products.extend(products_without_sale)
Product.objects.bulk_update(products, fields=["created_at", "updated_at"])
class Migration(migrations.Migration):
dependencies = [("counter", "0035_remove_selling_is_validated_and_more")]
operations = [
migrations.AddField(
model_name="product",
name="created_at",
field=models.DateTimeField(
auto_now_add=True,
default=django.utils.timezone.now,
verbose_name="created at",
),
preserve_default=False,
),
migrations.AddField(
model_name="product",
name="updated_at",
field=models.DateTimeField(auto_now=True, verbose_name="updated at"),
),
migrations.RunPython(
apply_product_history_dates, reverse_code=migrations.RunPython.noop
),
]

View File

@@ -399,6 +399,8 @@ class Product(models.Model):
Group, related_name="products", verbose_name=_("buying groups"), blank=True Group, related_name="products", verbose_name=_("buying groups"), blank=True
) )
archived = models.BooleanField(_("archived"), default=False) archived = models.BooleanField(_("archived"), default=False)
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
class Meta: class Meta:
verbose_name = _("product") verbose_name = _("product")

View File

@@ -3,6 +3,8 @@
{% block content %} {% block content %}
{% if object %} {% if object %}
<h2>{% trans name=object %}Edit product {{ name }}{% endtrans %}</h2> <h2>{% trans name=object %}Edit product {{ name }}{% endtrans %}</h2>
<p><i>{% trans %}Creation date{% endtrans %} : {{ object.created_at|date }}</i></p>
<p><i>{% trans %}Last update{% endtrans %} : {{ object.updated_at|date }}</i></p>
{% else %} {% else %}
<h2>{% trans %}Product creation{% endtrans %}</h2> <h2>{% trans %}Product creation{% endtrans %}</h2>
{% endif %} {% endif %}

View File

@@ -1566,7 +1566,7 @@ msgstr "Visiteur"
msgid "ban type" msgid "ban type"
msgstr "type de ban" msgstr "type de ban"
#: core/models.py #: core/models.py counter/models.py
msgid "created at" msgid "created at"
msgstr "créé le" msgstr "créé le"
@@ -3109,6 +3109,10 @@ msgstr "groupe d'achat"
msgid "archived" msgid "archived"
msgstr "archivé" msgstr "archivé"
#: counter/models.py
msgid "updated at"
msgstr "mis à jour le"
#: counter/models.py #: counter/models.py
msgid "product" msgid "product"
msgstr "produit" msgstr "produit"
@@ -3664,6 +3668,14 @@ msgstr ""
msgid "Edit product %(name)s" msgid "Edit product %(name)s"
msgstr "Édition du produit %(name)s" msgstr "Édition du produit %(name)s"
#: counter/templates/counter/product_form.jinja
msgid "Creation date"
msgstr "Date de création"
#: counter/templates/counter/product_form.jinja
msgid "Last update"
msgstr "Dernière mise à jour"
#: counter/templates/counter/product_form.jinja #: counter/templates/counter/product_form.jinja
msgid "Product creation" msgid "Product creation"
msgstr "Création de produit" msgstr "Création de produit"