diff --git a/accounts/models.py b/accounts/models.py index da9f3c4..75e0254 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -1,5 +1,19 @@ +import os +from django.conf import settings from django.db import models from django.contrib.auth.models import AbstractUser +from django.core.files.storage import FileSystemStorage + + +def profile_img_upload_to(instance, filename): + return f"profile_img/{instance.id}.jpg" + + +class OverwriteStorage(FileSystemStorage): + def get_aavailable_name(self, name): + if self.exists(name): + os.remove(os.path.join(settings.MEDIA_ROOT, name)) + return name class User(AbstractUser): @@ -13,7 +27,9 @@ class User(AbstractUser): ) profile_img = models.ImageField( - default="default_profile_img.jpg", upload_to="profile_img/" + default="default_profile_img.jpg", + upload_to=profile_img_upload_to, + storage=OverwriteStorage(), ) reliability = models.SmallIntegerField(default=80) diff --git a/accounts/serializers.py b/accounts/serializers.py index b9fef82..73e0218 100644 --- a/accounts/serializers.py +++ b/accounts/serializers.py @@ -52,3 +52,6 @@ class Meta: def get_friend_count(self, obj): return Friend.objects.filter(user=obj).count() + + def update(self, instance, validated_data): + return super().update(instance, validated_data) diff --git a/friends/views.py b/friends/views.py index 492d333..7a4cafb 100644 --- a/friends/views.py +++ b/friends/views.py @@ -55,7 +55,7 @@ def friend_restaurant_list(request, pk): { "message": "Friend evaluated successfully", "reliability": friend.reliability, - "is_Evaluated": Friend.objects.get( + "is_evaluated": Friend.objects.get( user=request.user, friend=friend ).is_evaluated, },