Skip to content

Commit

Permalink
Switch to an autoincremented integer ID
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Jun 30, 2019
1 parent e91b8eb commit a4f77e0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
26 changes: 26 additions & 0 deletions rest_framework_api_key/migrations/0005_auto_id.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
7 changes: 0 additions & 7 deletions rest_framework_api_key/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions tests/project/heroes/migrations/0003_auto_id.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]

0 comments on commit a4f77e0

Please sign in to comment.