|
| 1 | +# Generated by Django 5.0.12 on 2025-02-27 04:32 |
| 2 | + |
| 3 | +import authentik.lib.utils.time |
| 4 | +from authentik.lib.utils.time import timedelta_from_string |
| 5 | +from django.db import migrations, models |
| 6 | + |
| 7 | + |
| 8 | +def convert_integer_to_string_format(apps, schema_editor): |
| 9 | + db_alias = schema_editor.connection.alias |
| 10 | + EmailStage = apps.get_model("authentik_stages_email", "EmailStage") |
| 11 | + for stage in EmailStage.objects.using(db_alias).all(): |
| 12 | + stage.token_expiry = f"minutes={stage.token_expiry}" |
| 13 | + stage.save(using=db_alias) |
| 14 | + |
| 15 | + |
| 16 | +def convert_string_to_integer_format(apps, schema_editor): |
| 17 | + db_alias = schema_editor.connection.alias |
| 18 | + EmailStage = apps.get_model("authentik_stages_email", "EmailStage") |
| 19 | + for stage in EmailStage.objects.using(db_alias).all(): |
| 20 | + # Check if token_expiry is a string |
| 21 | + if isinstance(stage.token_expiry, str): |
| 22 | + try: |
| 23 | + # Use the timedelta_from_string utility to convert to timedelta |
| 24 | + # then convert to minutes by dividing seconds by 60 |
| 25 | + td = timedelta_from_string(stage.token_expiry) |
| 26 | + minutes_value = int(td.total_seconds() / 60) |
| 27 | + stage.token_expiry = minutes_value |
| 28 | + stage.save(using=db_alias) |
| 29 | + except (ValueError, TypeError): |
| 30 | + # If the string can't be parsed or converted properly, skip |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +class Migration(migrations.Migration): |
| 35 | + |
| 36 | + dependencies = [ |
| 37 | + ("authentik_stages_email", "0004_emailstage_activate_user_on_success"), |
| 38 | + ] |
| 39 | + |
| 40 | + operations = [ |
| 41 | + migrations.AlterField( |
| 42 | + model_name="emailstage", |
| 43 | + name="token_expiry", |
| 44 | + field=models.TextField( |
| 45 | + default="minutes=30", |
| 46 | + help_text="Time the token sent is valid (Format: hours=3,minutes=17,seconds=300).", |
| 47 | + validators=[authentik.lib.utils.time.timedelta_string_validator], |
| 48 | + ), |
| 49 | + ), |
| 50 | + migrations.RunPython( |
| 51 | + convert_integer_to_string_format, |
| 52 | + convert_string_to_integer_format, |
| 53 | + ), |
| 54 | + ] |
0 commit comments