File: 0002_prefix_hashed_key.py

package info (click to toggle)
djangorestframework-api-key 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: python: 926; makefile: 53; sh: 3
file content (45 lines) | stat: -rw-r--r-- 1,421 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Generated by Django 2.2.2 on 2019-06-29 10:38

from django.db import migrations, models

APP_NAME = "heroes"
MODEL_NAME = "heroapikey"
DEPENDENCIES = [(APP_NAME, "0001_initial")]


def populate_prefix_hashed_key(apps, schema_editor):  # type: ignore
    model = apps.get_model(APP_NAME, MODEL_NAME)

    for api_key in model.objects.using(schema_editor.connection.alias).all():
        prefix, _, hashed_key = api_key.id.partition(".")
        api_key.prefix = prefix
        api_key.hashed_key = hashed_key
        api_key.save()


class Migration(migrations.Migration):
    dependencies = DEPENDENCIES

    operations = [
        migrations.AddField(
            model_name=MODEL_NAME,
            name="hashed_key",
            field=models.CharField(max_length=100, null=True),
        ),
        migrations.AddField(
            model_name=MODEL_NAME,
            name="prefix",
            field=models.CharField(max_length=8, unique=True, null=True),
        ),
        migrations.RunPython(populate_prefix_hashed_key, migrations.RunPython.noop),
        migrations.AlterField(
            model_name=MODEL_NAME,
            name="hashed_key",
            field=models.CharField(max_length=100, editable=False),
        ),
        migrations.AlterField(
            model_name=MODEL_NAME,
            name="prefix",
            field=models.CharField(max_length=8, unique=True, editable=False),
        ),
    ]