Skip to content

Commit 8738543

Browse files
committed
Use uuid instead of id for User
1 parent 3916768 commit 8738543

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

api/serializers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import datetime
22
from typing import Literal, Optional
3-
import uuid
3+
from uuid import UUID
44

55
from django.contrib.auth import get_user_model
66
from django.db import transaction
@@ -52,15 +52,19 @@
5252

5353
class SimpleRegularUserSerializer(serializers.ModelSerializer):
5454

55+
uuid = serializers.SerializerMethodField()
5556
full_name = serializers.SerializerMethodField()
5657

58+
def get_uuid(self, obj) -> UUID:
59+
return UUID(int=obj.pk)
60+
5761
def get_full_name(self, obj) -> str:
5862
return obj.get_full_name()
5963

6064
class Meta:
6165
model = User
6266
fields = (
63-
'id',
67+
'uuid',
6468
'username',
6569
'first_name',
6670
'last_name',
@@ -151,7 +155,7 @@ def to_representation(self, instance):
151155
if isinstance(instance, User):
152156
# NOTE: this must be the same structure as defined.
153157
data = {}
154-
data['uuid'] = uuid.UUID(int=instance.pk)
158+
data['uuid'] = UUID(int=instance.pk)
155159
data['username'] = instance.get_username()
156160
data['registration_time'] = instance.date_joined
157161
data['locale'] = 'en'
@@ -717,7 +721,7 @@ def to_representation(self, instance):
717721
'app_label': UserStat._meta.app_label,
718722
'model_name': UserStat._meta.model_name
719723
}):
720-
new_instance = User(id=-1, username='expert', first_name='Expert', last_name='Annotator')
724+
new_instance = User(id=0, username='expert', first_name='Expert', last_name='Annotator')
721725

722726
return super().to_representation(new_instance)
723727

api/tests/integration/identification_tasks/annotations/create.tavern.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ stages:
6969
id: !anyint
7070
observation_uuid: "{identification_task.report.pk}"
7171
user:
72-
id: !int "{user.pk}"
72+
uuid: !re_fullmatch "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
7373
username: "{user.username}"
7474
first_name: "{user.first_name}"
7575
last_name: "{user.last_name}"
@@ -133,7 +133,7 @@ stages:
133133
id: !anyint
134134
observation_uuid: "{identification_task.report.pk}"
135135
user:
136-
id: !int "{user.pk}"
136+
uuid: !re_fullmatch "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
137137
username: "{user.username}"
138138
first_name: "{user.first_name}"
139139
last_name: "{user.last_name}"

api/tests/integration/identification_tasks/annotations/get.tavern.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ stages:
164164
- json:off
165165
json:
166166
user:
167-
id: !int "{user.pk}"
167+
uuid: !re_fullmatch "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
168168
username: "{user.username}"
169169
first_name: "{user.first_name}"
170170
last_name: "{user.last_name}"
@@ -197,7 +197,7 @@ stages:
197197
- json:off
198198
json:
199199
user:
200-
id: -1
200+
uuid: '00000000-0000-0000-0000-000000000000'
201201
username: 'expert'
202202
first_name: 'Expert'
203203
last_name: 'Annotator'
@@ -232,7 +232,7 @@ stages:
232232
- json:off
233233
json:
234234
user:
235-
id: !int "{another_user.pk}"
235+
uuid: !re_fullmatch "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
236236
username: "{another_user.username}"
237237
first_name: "{another_user.first_name}"
238238
last_name: "{another_user.last_name}"

api/tests/integration/identification_tasks/annotations/schema.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ variables:
88
id: !anyint
99
observation_uuid: !re_fullmatch "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
1010
user:
11-
id: !anyint
11+
uuid: !re_fullmatch "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
1212
username: !anystr
1313
first_name: !anystr
1414
last_name: !anystr

0 commit comments

Comments
 (0)