forked from CGM_Public/pretix_original
* Voucher: Add creation date (Z#23202621) * Migration fix * Update doc/api/resources/vouchers.rst Co-authored-by: luelista <weller@rami.io> * Update src/pretix/base/migrations/0285_voucher_created.py Co-authored-by: luelista <weller@rami.io> --------- Co-authored-by: luelista <weller@rami.io>
47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
# Generated by Django 4.2.16 on 2025-08-08 09:13
|
|
|
|
from django.db import migrations, models
|
|
from django.db.models import Min
|
|
from django.utils.timezone import now
|
|
|
|
|
|
def backfill_voucher_created(apps, schema_editor):
|
|
Voucher = apps.get_model("pretixbase", "Voucher")
|
|
LogEntry = apps.get_model("pretixbase", "LogEntry")
|
|
ContentType = apps.get_model("contenttypes", "ContentType")
|
|
ct = None
|
|
|
|
for v in Voucher.objects.filter(created__isnull=True).iterator():
|
|
if not ct:
|
|
# "Lazy-loading" to prevent this to be executed on new DBs where the content type does not yet
|
|
# exist -- but also no vouchers do
|
|
ct = ContentType.objects.get(app_label='pretixbase', model='voucher')
|
|
v.created = LogEntry.objects.filter(
|
|
content_type=ct,
|
|
object_id=v.pk,
|
|
).aggregate(m=Min("datetime"))["m"] or now()
|
|
v.save(update_fields=["created"])
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("pretixbase", "0284_ordersyncresult_ordersyncqueue"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AddField(
|
|
model_name="voucher",
|
|
name="created",
|
|
field=models.DateTimeField(auto_now_add=True, null=True),
|
|
),
|
|
migrations.RunPython(
|
|
backfill_voucher_created,
|
|
migrations.RunPython.noop,
|
|
),
|
|
migrations.AlterField(
|
|
model_name="voucher",
|
|
name="created",
|
|
field=models.DateTimeField(auto_now_add=True),
|
|
),
|
|
]
|