diff options
author | Andrew Godwin | 2022-11-19 13:38:25 -0700 |
---|---|---|
committer | Andrew Godwin | 2022-11-19 13:38:25 -0700 |
commit | 6e79527bb5743481148f492274b7a19da057366c (patch) | |
tree | 344a2080cf5d5f38996a9f991732da98aa33ac5f /users/views | |
parent | 774e91c8a24b85130eb61f2eebf9834ac38fa468 (diff) | |
download | takahe-6e79527bb5743481148f492274b7a19da057366c.tar.gz takahe-6e79527bb5743481148f492274b7a19da057366c.tar.bz2 takahe-6e79527bb5743481148f492274b7a19da057366c.zip |
A few more fixes from going live
Diffstat (limited to 'users/views')
-rw-r--r-- | users/views/settings/__init__.py (renamed from users/views/settings.py) | 55 | ||||
-rw-r--r-- | users/views/settings/profile.py | 63 |
2 files changed, 64 insertions, 54 deletions
diff --git a/users/views/settings.py b/users/views/settings/__init__.py index 1403821..65be1c5 100644 --- a/users/views/settings.py +++ b/users/views/settings/__init__.py @@ -6,10 +6,10 @@ from django.core.files import File from django.shortcuts import redirect from django.utils.decorators import method_decorator from django.views.generic import FormView, RedirectView -from PIL import Image, ImageOps from core.models.config import Config, UploadedImage from users.decorators import identity_required +from users.views.settings.profile import ProfilePage # noqa @method_decorator(identity_required, name="dispatch") @@ -120,59 +120,6 @@ class InterfacePage(SettingsPage): @method_decorator(identity_required, name="dispatch") -class ProfilePage(FormView): - """ - Lets the identity's profile be edited - """ - - template_name = "settings/profile.html" - extra_context = {"section": "profile"} - - class form_class(forms.Form): - name = forms.CharField(max_length=500) - summary = forms.CharField( - widget=forms.Textarea, - required=False, - help_text="Describe you and your interests", - label="Bio", - ) - icon = forms.ImageField( - required=False, help_text="Shown next to all your posts and activities" - ) - image = forms.ImageField( - required=False, help_text="Shown at the top of your profile" - ) - - def get_initial(self): - return { - "name": self.request.identity.name, - "summary": self.request.identity.summary, - "icon": self.request.identity.icon and self.request.identity.icon.url, - "image": self.request.identity.image and self.request.identity.image.url, - } - - def form_valid(self, form): - # Update identity name and summary - self.request.identity.name = form.cleaned_data["name"] - self.request.identity.summary = form.cleaned_data["summary"] - # Resize images - icon = form.cleaned_data.get("icon") - image = form.cleaned_data.get("image") - if isinstance(icon, File): - resized_image = ImageOps.fit(Image.open(icon), (400, 400)) - icon.open() - resized_image.save(icon) - self.request.identity.icon = icon - if isinstance(image, File): - resized_image = ImageOps.fit(Image.open(image), (1500, 500)) - image.open() - resized_image.save(image) - self.request.identity.image = image - self.request.identity.save() - return redirect(".") - - -@method_decorator(identity_required, name="dispatch") class SecurityPage(FormView): """ Lets the identity's profile be edited diff --git a/users/views/settings/profile.py b/users/views/settings/profile.py new file mode 100644 index 0000000..3b16d69 --- /dev/null +++ b/users/views/settings/profile.py @@ -0,0 +1,63 @@ +import io + +from django import forms +from django.core.files import File +from django.shortcuts import redirect +from django.utils.decorators import method_decorator +from django.views.generic import FormView +from PIL import Image, ImageOps + +from users.decorators import identity_required + + +@method_decorator(identity_required, name="dispatch") +class ProfilePage(FormView): + """ + Lets the identity's profile be edited + """ + + template_name = "settings/profile.html" + extra_context = {"section": "profile"} + + class form_class(forms.Form): + name = forms.CharField(max_length=500) + summary = forms.CharField( + widget=forms.Textarea, + required=False, + help_text="Describe you and your interests", + label="Bio", + ) + icon = forms.ImageField( + required=False, help_text="Shown next to all your posts and activities" + ) + image = forms.ImageField( + required=False, help_text="Shown at the top of your profile" + ) + + def get_initial(self): + return { + "name": self.request.identity.name, + "summary": self.request.identity.summary, + "icon": self.request.identity.icon and self.request.identity.icon.url, + "image": self.request.identity.image and self.request.identity.image.url, + } + + def form_valid(self, form): + # Update identity name and summary + self.request.identity.name = form.cleaned_data["name"] + self.request.identity.summary = form.cleaned_data["summary"] + # Resize images + icon = form.cleaned_data.get("icon") + image = form.cleaned_data.get("image") + if isinstance(icon, File): + resized_image = ImageOps.fit(Image.open(icon), (400, 400)) + new_icon_bytes = io.BytesIO() + resized_image.save(new_icon_bytes, format=icon.format) + self.request.identity.icon.save(icon.name, File(new_icon_bytes)) + if isinstance(image, File): + resized_image = ImageOps.fit(Image.open(image), (400, 400)) + new_image_bytes = io.BytesIO() + resized_image.save(new_image_bytes, format=image.format) + self.request.identity.image.save(image.name, File(new_image_bytes)) + self.request.identity.save() + return redirect(".") |