1
0
mirror of https://github.com/ae-utbm/sith.git synced 2025-07-25 03:09:56 +00:00
Files
.github
accounting
antispam
club
com
core
counter
management
migrations
0001_initial.py
0002_auto_20160826_1342.py
0003_permanency_activity.py
0004_auto_20160826_1907.py
0005_auto_20160826_2330.py
0006_auto_20160831_1304.py
0007_product_archived.py
0008_counter_token.py
0009_eticket.py
0010_auto_20161003_1900.py
0011_auto_20161004_2039.py
0012_auto_20170515_2202.py
0013_customer_recorded_products.py
0014_auto_20170816_1521.py
0014_auto_20170817_1537.py
0015_merge.py
0016_producttype_comment.py
0017_studentcard.py
0018_producttype_priority.py
0019_billinginfo.py
0020_auto_20221215_1709.py
0021_rename_check_cashregistersummaryitem_is_checked.py
0022_alter_product_icon.py
0023_billinginfo_phone_number.py
0024_accountdump_accountdump_unique_ongoing_dump.py
0025_remove_product_parent_product_and_more.py
0026_alter_studentcard_customer.py
0027_alter_refilling_payment_method.py
0028_alter_producttype_comment_and_more.py
0029_alter_selling_label.py
0030_returnableproduct_returnableproductbalance_and_more.py
0031_alter_counter_options.py
__init__.py
static
templates
tests
views
widgets
__init__.py
admin.py
api.py
apps.py
baker_recipes.py
fields.py
forms.py
models.py
schemas.py
signals.py
urls.py
utils.py
docs
eboutic
election
forum
galaxy
launderette
locale
matmat
pedagogy
rootplace
sas
sith
staticfiles
subscription
trombi
.coveragerc
.env.example
.envrc
.gitattributes
.gitignore
.mailmap
.npmrc
.pre-commit-config.yaml
.python-version
CONTRIBUTING.rst
LICENSE
Procfile.service
Procfile.static
README.md
biome.json
conftest.py
manage.py
mkdocs.yml
openapi-csrf.ts
openapi-ts.config.ts
package-lock.json
package.json
pyproject.toml
tsconfig.json
uv.lock
vite.config.mts
Sith/counter/migrations/0026_alter_studentcard_customer.py
2024-12-15 16:49:24 +01:00

54 lines
1.7 KiB
Python

# Generated by Django 4.2.17 on 2024-12-08 13:30
from operator import attrgetter
import django.db.models.deletion
from django.db import migrations, models
from django.db.migrations.state import StateApps
from django.db.models import Count
def delete_duplicates(apps: StateApps, schema_editor):
"""Delete cards of users with more than one student cards.
For all users who have more than one registered student card, all
the cards except the last one are deleted.
"""
Customer = apps.get_model("counter", "Customer")
StudentCard = apps.get_model("counter", "StudentCard")
customers = (
Customer.objects.annotate(nb_cards=Count("student_cards"))
.filter(nb_cards__gt=1)
.prefetch_related("student_cards")
)
to_delete = [
card.id
for customer in customers
for card in sorted(customer.student_cards.all(), key=attrgetter("id"))[:-1]
]
StudentCard.objects.filter(id__in=to_delete).delete()
class Migration(migrations.Migration):
dependencies = [("counter", "0025_remove_product_parent_product_and_more")]
operations = [
migrations.RunPython(delete_duplicates, migrations.RunPython.noop),
migrations.AlterField(
model_name="studentcard",
name="customer",
field=models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="student_card",
to="counter.customer",
verbose_name="student card",
),
),
migrations.AlterModelOptions(
name="studentcard",
options={
"verbose_name": "student card",
"verbose_name_plural": "student cards",
},
),
]