Skip to content

Commit

Permalink
Randomize profile picture filename
Browse files Browse the repository at this point in the history
  • Loading branch information
williammck committed Dec 3, 2024
1 parent 7dd1844 commit 3458dbb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.files import File
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.utils.crypto import get_random_string
from PIL import Image
from rest_framework import status
from rest_framework.response import Response
Expand All @@ -13,7 +14,7 @@
from apps.feedback.models import Feedback
from apps.feedback.serializers import BasicFeedbackSerializer
from zhu_core.permissions import IsAdmin, IsController, IsDelete, IsGet, IsPut, IsStaff, IsTrainingStaff
from zhu_core.settings import BASE_DIR
from zhu_core.settings import MEDIA_ROOT

from .models import Status, User
from .serializers import AdminEditUserSerializer, AuthenticatedUserSerializer, BasicUserSerializer, UserSerializer
Expand Down Expand Up @@ -68,9 +69,10 @@ def put(self, request, cid):
profile_io = BytesIO()
img.save(profile_io, "PNG")

user.profile = File(profile_io, name=f"{user.cid}.png")
else:
os.remove(BASE_DIR / f"media/profile/{user.cid}.png")
filename = get_random_string(length=8) + ".png"
user.profile = File(profile_io, name=filename)
elif user.profile:
os.remove(os.path.join(MEDIA_ROOT, user.profile.name))
user.profile = None
if "biography" in request.data:
user.biography = request.data.get("biography")
Expand Down

0 comments on commit 3458dbb

Please sign in to comment.