# Generated by Django 2.2.3 on 2019-07-24 18:37
from django.conf import settings
from django.db import migrations
from tickets.models import create_ticket_token


def save_badge_tokens(apps, schema_editor):
    ShopTicket = apps.get_model("tickets", "ShopTicket")
    SponsorTicket = apps.get_model("tickets", "SponsorTicket")
    DiscountTicket = apps.get_model("tickets", "DiscountTicket")

    for model in (ShopTicket, SponsorTicket, DiscountTicket):

        for ticket in model.objects.all():
            badge_token = create_ticket_token(
                "{_id}{secret_key}-badge".format(
                    _id=ticket.uuid, secret_key=settings.SECRET_KEY
                ).encode("utf-8")
            )
            ticket.badge_token = badge_token
            ticket.save()


class Migration(migrations.Migration):

    dependencies = [("tickets", "0010_auto_20190724_2037")]

    operations = [migrations.RunPython(save_badge_tokens)]
