Skip to content

Commit

Permalink
Correctly replace profile picture
Browse files Browse the repository at this point in the history
  • Loading branch information
williammck committed Dec 4, 2024
1 parent 3458dbb commit d36f5dd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apps/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Migration(migrations.Migration):
('email', models.EmailField(max_length=254)),
('first_name', models.CharField(max_length=32)),
('last_name', models.CharField(max_length=32)),
('profile', models.ImageField(blank=True, null=True, storage=zhu_core.utils.OverwriteStorage(), upload_to=apps.users.models.create_profile_path)),
('profile', models.ImageField(blank=True, null=True, upload_to='profile/')),
('biography', models.TextField(blank=True, null=True)),
('rating', models.CharField(choices=[('', 'Unknown'), ('OBS', 'Observer'), ('S1', 'Student 1'), ('S2', 'Student 2'), ('S3', 'Student 3'), ('C1', 'Controller'), ('C3', 'Senior Controller'), ('I1', 'Instructor'), ('I3', 'Senior Instructor'), ('SUP', 'Supervisor'), ('ADM', 'Administrator')], max_length=3)),
('home_facility', models.CharField(max_length=16)),
Expand Down
8 changes: 2 additions & 6 deletions apps/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
from requests.adapters import HTTPAdapter, Retry

from apps.mailer.models import Email
from zhu_core.utils import OverwriteStorage, base26decode, base26encode, rating_int_to_short


def create_profile_path(instance, filename):
return f"profile/{filename}"
from zhu_core.utils import base26decode, base26encode, rating_int_to_short


def default_endorsements():
Expand Down Expand Up @@ -98,7 +94,7 @@ class Meta:
email = models.EmailField()
first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32)
profile = models.ImageField(upload_to=create_profile_path, null=True, blank=True, storage=OverwriteStorage())
profile = models.ImageField(upload_to="profile/", null=True, blank=True)
biography = models.TextField(null=True, blank=True)

# VATSIM Details
Expand Down
11 changes: 6 additions & 5 deletions apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,19 @@ def put(self, request, cid):
user = get_object_or_404(User, cid=cid)

if "avatar" in request.data:
if user.profile:
os.remove(os.path.join(MEDIA_ROOT, user.profile.name))
user.profile = None

if request.data.get("avatar"):
img = Image.open(request.data.get("avatar"))
img = img.resize((500, 500), Image.LANCZOS)

profile_io = BytesIO()
img.save(profile_io, "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
user.profile = File(profile_io, name=f"{get_random_string(8)}.png")

if "biography" in request.data:
user.biography = request.data.get("biography")

Expand Down
7 changes: 0 additions & 7 deletions zhu_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ def to_representation(self, value):
return f"{int(hours):02}:{int(minutes):02}"


class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, max_length=None):
if self.exists(name):
os.remove(str(settings.MEDIA_ROOT) + "/" + name)
return name


class StrictReadOnlyFieldsMixin:
"""Raises error if read only fields passed to input data."""

Expand Down

0 comments on commit d36f5dd

Please sign in to comment.