Skip to content

Commit

Permalink
fix: profile field serialization in compiler view (#1273)
Browse files Browse the repository at this point in the history
* fix: profile field serialization in compiler view

* fix: formatting

* Update backend/coreapp/tests/test_preset.py
  • Loading branch information
axel7083 authored Jun 10, 2024
1 parent 758e06f commit 7eb1d88
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions backend/coreapp/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .models.scratch import Scratch


def serialize_profile(request: Request, profile: Profile) -> Dict[str, Any]:
def serialize_profile(profile: Profile) -> Dict[str, Any]:
if profile.user is None:
return {
"is_anonymous": True,
Expand Down Expand Up @@ -55,7 +55,7 @@ def serialize_profile(request: Request, profile: Profile) -> Dict[str, Any]:

class ProfileField(ProfileFieldBaseClass):
def to_representation(self, profile: Profile) -> Dict[str, Any]:
return serialize_profile(self.context["request"], profile)
return serialize_profile(profile)


class LibrarySerializer(serializers.Serializer[Library]):
Expand Down
16 changes: 16 additions & 0 deletions backend/coreapp/tests/test_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ def test_user_create_preset(self) -> None:
assert preset.owner is not None
assert preset.owner.pk == self.user.pk

def test_list_compiler_with_custom_presets(self) -> None:
user = self.create_user()
self.create_preset(DUMMY_PRESET_DICT)
response = self.client.get(reverse("compiler"))
body = response.json()

assert "platforms" in body
assert "dummy" in body["platforms"]
assert "presets" in body["platforms"]["dummy"]
assert len(body["platforms"]["dummy"]["presets"]) == 1
assert (
body["platforms"]["dummy"]["presets"][0]["name"]
== DUMMY_PRESET_DICT["name"]
)
assert body["platforms"]["dummy"]["presets"][0]["owner"]["id"] == user.pk

def test_owner_can_delete_preset(self) -> None:
self.create_user()
preset = self.create_preset(DUMMY_PRESET_DICT)
Expand Down
6 changes: 3 additions & 3 deletions backend/coreapp/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CurrentUser(APIView):
"""

def get(self, request: Request) -> Response:
user = serialize_profile(request, request.profile)
user = serialize_profile(request.profile)
return Response(user)

def post(self, request: Request) -> Response:
Expand Down Expand Up @@ -72,11 +72,11 @@ def get_queryset(self) -> QuerySet[Scratch]:


@api_view(["GET"]) # type: ignore
def user(request: Request, username: str) -> Response:
def user(username: str) -> Response:
"""
Gets a user's basic data
"""

return Response(
serialize_profile(request, get_object_or_404(Profile, user__username=username))
serialize_profile(get_object_or_404(Profile, user__username=username))
)

0 comments on commit 7eb1d88

Please sign in to comment.