diff --git a/counter/migrations/0038_countersellers.py b/counter/migrations/0038_countersellers.py new file mode 100644 index 00000000..bdf50f51 --- /dev/null +++ b/counter/migrations/0038_countersellers.py @@ -0,0 +1,73 @@ +# Generated by Django 5.2.11 on 2026-03-04 15:26 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("counter", "0037_productformula"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + # cf. https://docs.djangoproject.com/fr/stable/howto/writing-migrations/#changing-a-manytomanyfield-to-use-a-through-model + migrations.SeparateDatabaseAndState( + database_operations=[ + migrations.RunSQL( + sql="ALTER TABLE counter_counter_sellers RENAME TO counter_countersellers", + reverse_sql="ALTER TABLE counter_countersellers RENAME TO counter_counter_sellers", + ), + ], + state_operations=[ + migrations.CreateModel( + name="CounterSellers", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "counter", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="counter.counter", + ), + ), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + options={ + "constraints": [ + models.UniqueConstraint( + fields=("counter", "user"), + name="counter_counter_sellers_counter_id_subscriber_id_key", + ) + ], + }, + ), + migrations.AlterField( + model_name="counter", + name="sellers", + field=models.ManyToManyField( + blank=True, + related_name="counters", + through="counter.CounterSellers", + to=settings.AUTH_USER_MODEL, + verbose_name="sellers", + ), + ), + ], + ) + ] diff --git a/counter/models.py b/counter/models.py index d53d129c..7b336957 100644 --- a/counter/models.py +++ b/counter/models.py @@ -551,7 +551,11 @@ class Counter(models.Model): choices=[("BAR", _("Bar")), ("OFFICE", _("Office")), ("EBOUTIC", _("Eboutic"))], ) sellers = models.ManyToManyField( - User, verbose_name=_("sellers"), related_name="counters", blank=True + User, + verbose_name=_("sellers"), + related_name="counters", + blank=True, + through="CounterSellers", ) edit_groups = models.ManyToManyField( Group, related_name="editable_counters", blank=True @@ -743,6 +747,23 @@ class Counter(models.Model): ] +class CounterSellers(models.Model): + counter = models.ForeignKey(Counter, on_delete=models.CASCADE) + user = models.ForeignKey(User, on_delete=models.CASCADE) + + class Meta: + verbose_name = _("counter seller") + constraints = [ + models.UniqueConstraint( + fields=["counter", "user"], + name="counter_counter_sellers_counter_id_subscriber_id_key", + ) + ] + + def __str__(self): + return f"counter {self.counter_id} - user {self.user_id}" + + class RefillingQuerySet(models.QuerySet): def annotate_total(self) -> Self: """Annotate the Queryset with the total amount.