Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add UUID to user model #4488

Merged
merged 6 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/custom_auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class Meta(UserCreateSerializer.Meta):
"is_active",
"marketing_consent_given",
"key",
"uuid",
)
read_only_fields = ("is_active",)
read_only_fields = ("is_active", "uuid")
write_only_fields = ("sign_up_type",)
extra_kwargs = {
"email": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ <h3><a href="mailto:{% for user in organisation.users.all %}{{user.email}},{% en
<th>Email Address</th>
<th>Date Registered</th>
<th>Last Logged In</th>
<th>User UUID</th>
</tr>
</thead>
<tbody>
Expand All @@ -166,6 +167,7 @@ <h3><a href="mailto:{% for user in organisation.users.all %}{{user.email}},{% en
<td><a href="mailto:{{user.email}}">{{user.email}}</a></td>
<td>{{ user.date_joined }}</td>
<td>{{ user.last_login }}</td>
<td><span id="user-uuid">{{ user.uuid }}</span> <button onclick='navigator.clipboard.writeText(document.getElementById("user-uuid").innerText)'>copy</button></td>
</tr>
{% endfor %}
</tbody>
Expand Down
22 changes: 22 additions & 0 deletions api/tests/unit/custom_auth/test_unit_custom_auth_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient

from users.models import FFAdminUser


def test_get_current_user(staff_user: FFAdminUser, staff_client: APIClient) -> None:
# Given
url = reverse("api-v1:custom_auth:ffadminuser-me")

# When
response = staff_client.get(url)

# Then
assert response.status_code == status.HTTP_200_OK

response_json = response.json()
assert response_json["email"] == staff_user.email
assert response_json["first_name"] == staff_user.first_name
assert response_json["last_name"] == staff_user.last_name
assert response_json["uuid"] == str(staff_user.uuid)
3 changes: 3 additions & 0 deletions api/tests/unit/features/test_unit_features_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def test_remove_owners_only_remove_specified_owners(
"first_name": user_3.first_name,
"last_name": user_3.last_name,
"last_login": None,
"uuid": mock.ANY,
}


Expand Down Expand Up @@ -1565,13 +1566,15 @@ def test_add_owners_adds_owner(
"first_name": staff_user.first_name,
"last_name": staff_user.last_name,
"last_login": None,
"uuid": mock.ANY,
}
assert json_response["owners"][1] == {
"id": admin_user.id,
"email": admin_user.email,
"first_name": admin_user.first_name,
"last_name": admin_user.last_name,
"last_login": None,
"uuid": mock.ANY,
}


Expand Down
2 changes: 2 additions & 0 deletions api/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CustomUserAdmin(UserAdmin):
"is_staff",
"is_active",
"date_joined",
"uuid",
)

list_filter = (
Expand All @@ -57,6 +58,7 @@ class CustomUserAdmin(UserAdmin):
"username",
"first_name",
"last_name",
"uuid",
)

inlines = [UserOrganisationInline]
37 changes: 37 additions & 0 deletions api/users/migrations/0037_add_uuid_field_to_user_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 3.2.25 on 2024-08-12 14:21
from django.apps.registry import Apps
from django.db import migrations, models
import uuid

from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def set_default_uuids(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
user_model = apps.get_model("users", "FFAdminUser")

users = list(user_model.objects.all())
for user in users:
user.uuid = uuid.uuid4()

user_model.objects.bulk_update(users, fields=["uuid"])


class Migration(migrations.Migration):

dependencies = [
('users', '0036_create_hubspot_lead'),
]

operations = [
migrations.AddField(
model_name='ffadminuser',
name='uuid',
field=models.UUIDField(default=uuid.uuid4),
),
migrations.RunPython(set_default_uuids, reverse_code=migrations.RunPython.noop),
migrations.AlterField(
model_name='ffadminuser',
name='uuid',
field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
),
]
3 changes: 3 additions & 0 deletions api/users/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import typing
import uuid
from datetime import timedelta

from django.conf import settings
Expand Down Expand Up @@ -112,6 +113,8 @@ class FFAdminUser(LifecycleModel, AbstractUser):
choices=SignUpType.choices, max_length=100, blank=True, null=True
)

uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)

USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["first_name", "last_name", "sign_up_type"]

Expand Down
6 changes: 4 additions & 2 deletions api/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class UserFullSerializer(serializers.ModelSerializer):

class Meta:
model = FFAdminUser
fields = ("id", "email", "first_name", "last_name", "organisations")
fields = ("id", "email", "first_name", "last_name", "organisations", "uuid")


class UserLoginSerializer(serializers.ModelSerializer):
Expand All @@ -57,7 +57,7 @@ class UserListSerializer(serializers.ModelSerializer):
role = serializers.SerializerMethodField(read_only=True)
join_date = serializers.SerializerMethodField(read_only=True)

default_fields = ("id", "email", "first_name", "last_name", "last_login")
default_fields = ("id", "email", "first_name", "last_name", "last_login", "uuid")
organisation_users_fields = (
"role",
"date_joined",
Expand Down Expand Up @@ -134,12 +134,14 @@ class UserPermissionGroupSerializerDetail(UserPermissionGroupSerializer):
class CustomCurrentUserSerializer(DjoserUserSerializer):
auth_type = serializers.CharField(read_only=True)
is_superuser = serializers.BooleanField(read_only=True)
uuid = serializers.UUIDField(read_only=True)

class Meta(DjoserUserSerializer.Meta):
fields = DjoserUserSerializer.Meta.fields + (
"auth_type",
"is_superuser",
"date_joined",
"uuid",
)


Expand Down
Loading