From c1fabe1e1ca506ebd082732a93a54ee915ca226c Mon Sep 17 00:00:00 2001 From: florimondmanca Date: Sat, 29 Jun 2019 12:03:30 +0200 Subject: [PATCH] Switch to an autoincremented integer ID --- .../migrations/0005_auto_id.py | 26 +++++++++++++++++++ rest_framework_api_key/models.py | 7 ----- .../project/heroes/migrations/0003_auto_id.py | 26 +++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 rest_framework_api_key/migrations/0005_auto_id.py create mode 100644 tests/project/heroes/migrations/0003_auto_id.py diff --git a/rest_framework_api_key/migrations/0005_auto_id.py b/rest_framework_api_key/migrations/0005_auto_id.py new file mode 100644 index 0000000..dbb75ff --- /dev/null +++ b/rest_framework_api_key/migrations/0005_auto_id.py @@ -0,0 +1,26 @@ +# Generated by Django 2.2.2 on 2019-06-29 11:49 + +from django.db import migrations, models + +APP_NAME = "rest_framework_api_key" +MODEL_NAME = "apikey" +DEPENDENCIES = [(APP_NAME, "0004_prefix_hashed_key")] + + +class Migration(migrations.Migration): + + dependencies = DEPENDENCIES + + operations = [ + migrations.RemoveField(model_name=MODEL_NAME, name="id"), + migrations.AddField( + model_name=MODEL_NAME, + name="id", + field=models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ] diff --git a/rest_framework_api_key/models.py b/rest_framework_api_key/models.py index 15cf50c..2736053 100644 --- a/rest_framework_api_key/models.py +++ b/rest_framework_api_key/models.py @@ -15,12 +15,8 @@ def assign_key(self, obj: "AbstractAPIKey") -> str: key, prefix, hashed_key = self.key_generator.generate() except TypeError: # Compatibility with < 1.4 key, hashed_key = self.key_generator.generate() - pk = hashed_key prefix, hashed_key = split(hashed_key) - else: - pk = concatenate(prefix, hashed_key) - obj.id = pk obj.prefix = prefix obj.hashed_key = hashed_key @@ -63,9 +59,6 @@ class APIKeyManager(BaseAPIKeyManager): class AbstractAPIKey(models.Model): objects = APIKeyManager() - id = models.CharField( - max_length=100, unique=True, primary_key=True, editable=False - ) prefix = models.CharField(max_length=8, unique=True, editable=False) hashed_key = models.CharField(max_length=100, editable=False) created = models.DateTimeField(auto_now_add=True, db_index=True) diff --git a/tests/project/heroes/migrations/0003_auto_id.py b/tests/project/heroes/migrations/0003_auto_id.py new file mode 100644 index 0000000..23193b4 --- /dev/null +++ b/tests/project/heroes/migrations/0003_auto_id.py @@ -0,0 +1,26 @@ +# Generated by Django 2.2.2 on 2019-06-29 11:49 + +from django.db import migrations, models + +APP_NAME = "heroes" +MODEL_NAME = "heroapikey" +DEPENDENCIES = [(APP_NAME, "0002_prefix_hashed_key")] + + +class Migration(migrations.Migration): + + dependencies = DEPENDENCIES + + operations = [ + migrations.RemoveField(model_name=MODEL_NAME, name="id"), + migrations.AddField( + model_name=MODEL_NAME, + name="id", + field=models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ]