2022-11-17 07:21:42 -08:00
|
|
|
from functools import partial
|
2022-11-17 16:43:00 -08:00
|
|
|
from typing import ClassVar, Dict, List
|
2022-11-17 07:21:42 -08:00
|
|
|
|
|
|
|
from django import forms
|
2022-11-17 18:21:00 -08:00
|
|
|
from django.core.files import File
|
2022-11-17 07:21:42 -08:00
|
|
|
from django.shortcuts import redirect
|
2022-11-16 16:23:46 -08:00
|
|
|
from django.utils.decorators import method_decorator
|
2022-11-17 07:21:42 -08:00
|
|
|
from django.views.generic import FormView, RedirectView
|
|
|
|
from PIL import Image, ImageOps
|
2022-11-16 16:23:46 -08:00
|
|
|
|
2022-11-17 18:21:00 -08:00
|
|
|
from core.models.config import Config, UploadedImage
|
2022-11-16 16:23:46 -08:00
|
|
|
from users.decorators import identity_required
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
2022-11-16 20:42:25 -08:00
|
|
|
class SettingsRoot(RedirectView):
|
2022-11-17 18:21:00 -08:00
|
|
|
pattern_name = "settings_profile"
|
2022-11-16 16:23:46 -08:00
|
|
|
|
|
|
|
|
2022-11-17 07:21:42 -08:00
|
|
|
@method_decorator(identity_required, name="dispatch")
|
|
|
|
class SettingsPage(FormView):
|
2022-11-16 16:23:46 -08:00
|
|
|
"""
|
|
|
|
Shows a settings page dynamically created from our settings layout
|
|
|
|
at the bottom of the page. Don't add this to a URL directly - subclass!
|
|
|
|
"""
|
|
|
|
|
|
|
|
options_class = Config.IdentityOptions
|
2022-11-16 20:42:25 -08:00
|
|
|
template_name = "settings/settings.html"
|
2022-11-17 07:21:42 -08:00
|
|
|
section: ClassVar[str]
|
|
|
|
options: Dict[str, Dict[str, str]]
|
2022-11-17 16:43:00 -08:00
|
|
|
layout: Dict[str, List[str]]
|
2022-11-17 07:21:42 -08:00
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
# Create the fields dict from the config object
|
|
|
|
fields = {}
|
|
|
|
for key, details in self.options.items():
|
|
|
|
config_field = self.options_class.__fields__[key]
|
|
|
|
if config_field.type_ is bool:
|
|
|
|
form_field = partial(
|
|
|
|
forms.BooleanField,
|
|
|
|
widget=forms.Select(
|
|
|
|
choices=[(True, "Enabled"), (False, "Disabled")]
|
|
|
|
),
|
|
|
|
)
|
2022-11-17 18:21:00 -08:00
|
|
|
elif config_field.type_ is UploadedImage:
|
|
|
|
form_field = forms.ImageField
|
2022-11-17 07:21:42 -08:00
|
|
|
elif config_field.type_ is str:
|
2022-11-17 18:21:00 -08:00
|
|
|
if details.get("display") == "textarea":
|
|
|
|
form_field = partial(
|
|
|
|
forms.CharField,
|
|
|
|
widget=forms.Textarea,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
form_field = forms.CharField
|
2022-11-17 16:43:00 -08:00
|
|
|
elif config_field.type_ is int:
|
|
|
|
form_field = forms.IntegerField
|
2022-11-17 07:21:42 -08:00
|
|
|
else:
|
|
|
|
raise ValueError(f"Cannot render settings type {config_field.type_}")
|
|
|
|
fields[key] = form_field(
|
|
|
|
label=details["title"],
|
|
|
|
help_text=details.get("help_text", ""),
|
|
|
|
required=details.get("required", False),
|
|
|
|
)
|
|
|
|
# Create a form class dynamically (yeah, right?) and return that
|
|
|
|
return type("SettingsForm", (forms.Form,), fields)
|
2022-11-16 16:23:46 -08:00
|
|
|
|
|
|
|
def load_config(self):
|
|
|
|
return Config.load_identity(self.request.identity)
|
|
|
|
|
|
|
|
def save_config(self, key, value):
|
|
|
|
Config.set_identity(self.request.identity, key, value)
|
|
|
|
|
2022-11-17 07:21:42 -08:00
|
|
|
def get_initial(self):
|
|
|
|
config = self.load_config()
|
|
|
|
initial = {}
|
|
|
|
for key in self.options.keys():
|
|
|
|
initial[key] = getattr(config, key)
|
|
|
|
return initial
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
context = super().get_context_data()
|
|
|
|
context["section"] = self.section
|
2022-11-17 16:43:00 -08:00
|
|
|
# Gather fields into fieldsets
|
|
|
|
context["fieldsets"] = {}
|
|
|
|
for title, fields in self.layout.items():
|
|
|
|
context["fieldsets"][title] = [context["form"][field] for field in fields]
|
2022-11-17 07:21:42 -08:00
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
# Save each key
|
|
|
|
for field in form:
|
2022-11-17 18:21:00 -08:00
|
|
|
if field.field.__class__.__name__ == "ImageField":
|
|
|
|
# These can be cleared with an extra checkbox
|
|
|
|
if self.request.POST.get(f"{field.name}__clear"):
|
|
|
|
self.save_config(field.name, None)
|
|
|
|
continue
|
|
|
|
# We shove the preview values in initial_data, so only save file
|
|
|
|
# fields if they have a File object.
|
|
|
|
if not isinstance(form.cleaned_data[field.name], File):
|
|
|
|
continue
|
2022-11-17 07:21:42 -08:00
|
|
|
self.save_config(
|
|
|
|
field.name,
|
|
|
|
form.cleaned_data[field.name],
|
|
|
|
)
|
|
|
|
return redirect(".")
|
|
|
|
|
2022-11-16 16:23:46 -08:00
|
|
|
|
2022-11-16 20:42:25 -08:00
|
|
|
class InterfacePage(SettingsPage):
|
2022-11-16 16:23:46 -08:00
|
|
|
|
|
|
|
section = "interface"
|
|
|
|
|
|
|
|
options = {
|
|
|
|
"toot_mode": {
|
|
|
|
"title": "I Will Toot As I Please",
|
2022-11-17 16:43:00 -08:00
|
|
|
"help_text": "Changes all 'Post' buttons to 'Toot!'",
|
2022-11-16 16:23:46 -08:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 07:21:42 -08:00
|
|
|
|
2022-11-17 16:43:00 -08:00
|
|
|
layout = {"Posting": ["toot_mode"]}
|
|
|
|
|
2022-11-17 07:21:42 -08:00
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
|
|
|
class ProfilePage(FormView):
|
|
|
|
"""
|
|
|
|
Lets the identity's profile be edited
|
|
|
|
"""
|
|
|
|
|
|
|
|
template_name = "settings/profile.html"
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
|
|
|
name = forms.CharField(max_length=500)
|
2022-11-17 16:43:00 -08:00
|
|
|
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-17 07:21:42 -08:00
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
return {
|
|
|
|
"name": self.request.identity.name,
|
|
|
|
"summary": self.request.identity.summary,
|
2022-11-17 18:21:00 -08:00
|
|
|
"icon": self.request.identity.icon.url,
|
|
|
|
"image": self.request.identity.image.url,
|
2022-11-17 07:21:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
context = super().get_context_data()
|
|
|
|
context["section"] = "profile"
|
|
|
|
return context
|
|
|
|
|
|
|
|
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")
|
2022-11-17 18:21:00 -08:00
|
|
|
if isinstance(icon, File):
|
2022-11-17 07:21:42 -08:00
|
|
|
resized_image = ImageOps.fit(Image.open(icon), (400, 400))
|
|
|
|
icon.open()
|
|
|
|
resized_image.save(icon)
|
|
|
|
self.request.identity.icon = icon
|
2022-11-17 18:21:00 -08:00
|
|
|
if isinstance(image, File):
|
2022-11-17 07:21:42 -08:00
|
|
|
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(".")
|