generate auth key

This commit is contained in:
Ethan Roseman 2022-08-24 10:38:09 +09:00
parent 3defb09002
commit 8bf138d8c9
No known key found for this signature in database
GPG Key ID: 27F9FCEB8E4969BD
2 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1,19 @@
# Generated by Django 4.1 on 2022-08-24 01:37
from django.db import migrations, models
import frog_api.models
class Migration(migrations.Migration):
dependencies = [
("frog_api", "0005_remove_entry_decompiled_bytes_and_more"),
]
operations = [
migrations.AlterField(
model_name="project",
name="auth_key",
field=models.CharField(default=frog_api.models.gen_auth_key, max_length=10),
),
]

View File

@ -1,4 +1,17 @@
from django.db import models
from django.utils.crypto import get_random_string
AUTH_KEY_LEN = 10
def gen_auth_key() -> str:
ret = get_random_string(length=AUTH_KEY_LEN)
if Project.objects.filter(auth_key=ret).exists():
return gen_auth_key()
return ret
# Example: OOT
class Project(models.Model):
@ -8,7 +21,7 @@ class Project(models.Model):
slug = models.SlugField(max_length=255, unique=True)
name = models.CharField(max_length=255)
auth_key = models.CharField(max_length=255)
auth_key = models.CharField(max_length=AUTH_KEY_LEN, default=gen_auth_key)
def __str__(self) -> str:
return self.slug