diff --git a/frontend/static/js/api/profile.js b/frontend/static/js/api/profile.js index 8fcc6bf..65390e0 100644 --- a/frontend/static/js/api/profile.js +++ b/frontend/static/js/api/profile.js @@ -52,7 +52,7 @@ class Profile let response_data = await response.json(); this.id = response_data.user_id; this.username = response_data.username; - this.avatar_url = response_data.avatar_url; + this.avatar_url = response_data.avatar; await this.getBlock(); await this.getFriend(); diff --git a/profiles/models.py b/profiles/models.py index 581ab0b..8b310eb 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -14,7 +14,7 @@ def upload_to(instance, filename: str): # Create your models here. class ProfileModel(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) - avatar_url = models.ImageField(upload_to=upload_to, default="./profiles/static/avatars/default.avif") + avatar = models.ImageField(upload_to=upload_to, default="./profiles/static/avatars/default.avif") def get_game(self) -> int: for game in game_manager._game_list: diff --git a/profiles/serializers.py b/profiles/serializers.py index 1fea89f..b53bb03 100644 --- a/profiles/serializers.py +++ b/profiles/serializers.py @@ -4,8 +4,8 @@ from .models import ProfileModel class ProfileSerializer(serializers.ModelSerializer): username = serializers.ReadOnlyField(source='user.username') - avatar_url = serializers.ImageField(required=False) + avatar = serializers.ImageField(required=False) class Meta: model = ProfileModel - fields = ["username", "avatar_url", "user_id"] \ No newline at end of file + fields = ["username", "avatar", "user_id"] diff --git a/profiles/tests.py b/profiles/tests.py index 087957c..a963a69 100644 --- a/profiles/tests.py +++ b/profiles/tests.py @@ -7,7 +7,7 @@ class ProfileTest(TestCase): def setUp(self): self.user: User = User.objects.create(username='bozo', password='password') self.user.save() - self.expected_response = {'avatar_url': '/static/avatars/default.avif', 'user_id': 1, 'username': 'bozo'} + self.expected_response = {'avatar': '/static/avatars/default.avif', 'user_id': 1, 'username': 'bozo'} self.url = "/api/profiles/" @@ -15,4 +15,4 @@ class ProfileTest(TestCase): response: HttpResponse = self.client.get(self.url + str(self.user.pk)) response_dict: dict = eval(response.content) self.assertDictEqual(self.expected_response, response_dict) - \ No newline at end of file + diff --git a/profiles/viewsets.py b/profiles/viewsets.py index 60725e1..7272bf7 100644 --- a/profiles/viewsets.py +++ b/profiles/viewsets.py @@ -25,7 +25,7 @@ class ProfileViewSet(viewsets.ModelViewSet): if (not user): return Response({"detail": "Profile not found."}, status=status.HTTP_404_NOT_FOUND) instance = self.queryset().get(pk=user[0].pk) - instance.avatar_url.name = instance.avatar_url.name[instance.avatar_url.name.find("static") - 1:] + instance.avatar.name = instance.avatar.name[instance.avatar.name.find("static") - 1:] return Response(self.serializer_class(instance).data, status=status.HTTP_200_OK) @@ -34,14 +34,14 @@ class ProfileViewSet(viewsets.ModelViewSet): if (not user): return Response({"detail": "Profile not found."}, status=status.HTTP_404_NOT_FOUND) instance = self.queryset().get(pk=user[0].pk) - instance.avatar_url.name = instance.avatar_url.name[instance.avatar_url.name.find("static") - 1:] + instance.avatar.name = instance.avatar.name[instance.avatar.name.find("static") - 1:] return Response(self.serializer_class(instance).data, status=status.HTTP_200_OK) def list(self, request: HttpRequest): serializer = ProfileSerializer(self.queryset(), many=True) for profile in serializer.data: - profile["avatar_url"] = profile["avatar_url"][profile["avatar_url"].find("static") - 1:] + profile["avatar"] = profile["avatar"][profile["avatar"].find("static") - 1:] return Response(serializer.data) class MyProfileViewSet(viewsets.ModelViewSet): @@ -59,13 +59,13 @@ class MyProfileViewSet(viewsets.ModelViewSet): profile: ProfileModel = self.get_object() avatar : InMemoryUploadedFile = self.request.data.get("file", None) if (avatar is not None and avatar.size <= settings.PROFILE_PICTURE_MAX_SIZE): - if (profile.avatar_url.name != "./profiles/static/avatars/default.avif"): - profile.avatar_url.storage.delete(profile.avatar_url.name) - profile.avatar_url = avatar + if (profile.avatar.name != "./profiles/static/avatars/default.avif"): + profile.avatar.storage.delete(profile.avatar.name) + profile.avatar = avatar profile.save() def retrieve(self, request: HttpRequest, pk=None): instance: ProfileModel = self.get_object() - instance.avatar_url.name = instance.avatar_url.name[instance.avatar_url.name.find("static") - 1:] + instance.avatar.name = instance.avatar.name[instance.avatar.name.find("static") - 1:] return Response(self.serializer_class(instance).data, status=status.HTTP_200_OK)