2022-11-19 12:38:25 -08:00
|
|
|
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
|
|
|
|
|
2022-12-01 17:46:49 -08:00
|
|
|
from core.files import resize_image
|
2022-12-21 12:48:39 -08:00
|
|
|
from core.html import html_to_plaintext
|
2022-12-14 09:15:46 -08:00
|
|
|
from core.models.config import Config
|
2022-11-19 12:38:25 -08:00
|
|
|
from users.decorators import identity_required
|
2022-12-21 09:13:39 -08:00
|
|
|
from users.models import IdentityStates
|
2022-12-21 12:48:39 -08:00
|
|
|
from users.services import IdentityService
|
2022-11-19 12:38:25 -08:00
|
|
|
|
|
|
|
|
|
|
|
@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"
|
|
|
|
)
|
2022-11-25 17:32:45 -08:00
|
|
|
discoverable = forms.BooleanField(
|
|
|
|
help_text="If this user is visible on the frontpage and in user directories.",
|
|
|
|
widget=forms.Select(
|
|
|
|
choices=[(True, "Discoverable"), (False, "Not Discoverable")]
|
|
|
|
),
|
|
|
|
required=False,
|
|
|
|
)
|
2022-12-14 09:15:46 -08:00
|
|
|
visible_follows = forms.BooleanField(
|
|
|
|
help_text="Whether or not to show your following and follower counts in your profile.",
|
|
|
|
widget=forms.Select(choices=[(True, "Visible"), (False, "Hidden")]),
|
|
|
|
required=False,
|
|
|
|
)
|
2022-12-27 15:56:50 -08:00
|
|
|
metadata = forms.JSONField(
|
|
|
|
label="Profile Metadata Fields",
|
|
|
|
help_text="These values will appear on your profile below your Bio",
|
2022-12-27 16:45:51 -08:00
|
|
|
widget=forms.HiddenInput(attrs={"data-min-empty": 2}),
|
2022-12-27 15:56:50 -08:00
|
|
|
required=False,
|
|
|
|
)
|
2022-11-19 12:38:25 -08:00
|
|
|
|
2022-12-27 16:45:51 -08:00
|
|
|
def clean_metadata(self):
|
|
|
|
metadata = self.cleaned_data["metadata"]
|
|
|
|
if metadata:
|
|
|
|
metadata = [x for x in metadata if x["name"] and x["value"]]
|
|
|
|
if not metadata:
|
|
|
|
return None
|
|
|
|
return metadata
|
|
|
|
|
2022-11-19 12:38:25 -08:00
|
|
|
def get_initial(self):
|
2022-11-25 17:32:45 -08:00
|
|
|
identity = self.request.identity
|
2022-11-19 12:38:25 -08:00
|
|
|
return {
|
2022-11-25 17:32:45 -08:00
|
|
|
"name": identity.name,
|
2022-12-21 17:01:26 -08:00
|
|
|
"summary": html_to_plaintext(identity.summary) if identity.summary else "",
|
2022-11-25 17:32:45 -08:00
|
|
|
"icon": identity.icon and identity.icon.url,
|
|
|
|
"image": identity.image and identity.image.url,
|
|
|
|
"discoverable": identity.discoverable,
|
2022-12-14 09:15:46 -08:00
|
|
|
"visible_follows": identity.config_identity.visible_follows,
|
2022-12-27 16:45:51 -08:00
|
|
|
"metadata": identity.metadata or [],
|
2022-11-19 12:38:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2022-11-25 17:32:45 -08:00
|
|
|
# Update basic info
|
|
|
|
identity = self.request.identity
|
|
|
|
identity.name = form.cleaned_data["name"]
|
|
|
|
identity.discoverable = form.cleaned_data["discoverable"]
|
2022-12-21 12:48:39 -08:00
|
|
|
IdentityService(identity).set_summary(form.cleaned_data["summary"])
|
2022-11-19 12:38:25 -08:00
|
|
|
# Resize images
|
|
|
|
icon = form.cleaned_data.get("icon")
|
|
|
|
image = form.cleaned_data.get("image")
|
|
|
|
if isinstance(icon, File):
|
2022-11-25 17:32:45 -08:00
|
|
|
identity.icon.save(
|
2022-11-20 10:18:14 -08:00
|
|
|
icon.name,
|
2022-12-01 17:46:49 -08:00
|
|
|
resize_image(icon, size=(400, 400)),
|
2022-11-20 10:18:14 -08:00
|
|
|
)
|
2022-11-19 12:38:25 -08:00
|
|
|
if isinstance(image, File):
|
2022-11-25 17:32:45 -08:00
|
|
|
identity.image.save(
|
2022-11-20 10:18:14 -08:00
|
|
|
image.name,
|
2022-12-01 17:46:49 -08:00
|
|
|
resize_image(image, size=(1500, 500)),
|
2022-11-20 10:18:14 -08:00
|
|
|
)
|
2022-12-27 16:45:51 -08:00
|
|
|
identity.metadata = form.cleaned_data.get("metadata")
|
2022-11-25 17:32:45 -08:00
|
|
|
identity.save()
|
2022-12-21 09:13:39 -08:00
|
|
|
identity.transition_perform(IdentityStates.edited)
|
|
|
|
|
2022-12-14 09:15:46 -08:00
|
|
|
# Save profile-specific identity Config
|
|
|
|
Config.set_identity(
|
|
|
|
identity, "visible_follows", form.cleaned_data["visible_follows"]
|
|
|
|
)
|
2022-11-19 12:38:25 -08:00
|
|
|
return redirect(".")
|