Skip to content

Commit

Permalink
feat: 유저 이미지 업로드 수정
Browse files Browse the repository at this point in the history
이미지를 업로드할 경우 기존 이미지는 삭제되고 새로운 이미지가 저장됨
이름은 유저id + jpg
  • Loading branch information
ybkang1108 committed Aug 15, 2024
1 parent 1af382c commit e0384b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 17 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions accounts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e0384b3

Please sign in to comment.