summaryrefslogtreecommitdiffstats
path: root/users
diff options
context:
space:
mode:
authorAndrew Godwin2022-11-16 17:23:46 -0700
committerAndrew Godwin2022-11-16 17:23:46 -0700
commit44af0d4c59eed1c3715e9044e75c159cfddf54cc (patch)
treed2c87f953de12a526a158f2c03def5eb08b2d203 /users
parent495e955378d62dc439c4c210785e5d401bc77f64 (diff)
downloadtakahe-44af0d4c59eed1c3715e9044e75c159cfddf54cc.tar.gz
takahe-44af0d4c59eed1c3715e9044e75c159cfddf54cc.tar.bz2
takahe-44af0d4c59eed1c3715e9044e75c159cfddf54cc.zip
Add start of a settings (config) system
Diffstat (limited to 'users')
-rw-r--r--users/views/identity.py4
-rw-r--r--users/views/settings_identity.py39
-rw-r--r--users/views/settings_system.py95
3 files changed, 136 insertions, 2 deletions
diff --git a/users/views/identity.py b/users/views/identity.py
index 64e3c62..4b92e14 100644
--- a/users/views/identity.py
+++ b/users/views/identity.py
@@ -7,7 +7,7 @@ from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.views.generic import FormView, TemplateView, View
-from core.config import Config
+from core.models import Config
from users.decorators import identity_required
from users.models import Domain, Follow, Identity, IdentityStates
from users.shortcuts import by_handle_or_404
@@ -25,7 +25,7 @@ class ViewIdentity(TemplateView):
fetch=True,
)
posts = identity.posts.all()[:100]
- if identity.data_age > Config.load().identity_max_age:
+ if identity.data_age > Config.system.identity_max_age:
identity.transition_perform(IdentityStates.outdated)
return {
"identity": identity,
diff --git a/users/views/settings_identity.py b/users/views/settings_identity.py
new file mode 100644
index 0000000..8c52f9e
--- /dev/null
+++ b/users/views/settings_identity.py
@@ -0,0 +1,39 @@
+from django.utils.decorators import method_decorator
+from django.views.generic import RedirectView
+
+from core.models import Config
+from users.decorators import identity_required
+from users.views.settings_system import SystemSettingsPage
+
+
+@method_decorator(identity_required, name="dispatch")
+class IdentitySettingsRoot(RedirectView):
+ url = "/settings/interface/"
+
+
+class IdentitySettingsPage(SystemSettingsPage):
+ """
+ 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
+ template_name = "settings/settings_identity.html"
+
+ 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)
+
+
+class InterfacePage(IdentitySettingsPage):
+
+ section = "interface"
+
+ options = {
+ "toot_mode": {
+ "title": "I Will Toot As I Please",
+ "help_text": "If enabled, changes all 'Post' buttons to 'Toot!'",
+ }
+ }
diff --git a/users/views/settings_system.py b/users/views/settings_system.py
new file mode 100644
index 0000000..52ba349
--- /dev/null
+++ b/users/views/settings_system.py
@@ -0,0 +1,95 @@
+from functools import partial
+from typing import ClassVar, Dict
+
+from django import forms
+from django.shortcuts import redirect
+from django.utils.decorators import method_decorator
+from django.views.generic import FormView, RedirectView
+
+from core.models import Config
+from users.decorators import identity_required
+
+
+@method_decorator(identity_required, name="dispatch")
+class SystemSettingsRoot(RedirectView):
+ url = "/settings/system/basic/"
+
+
+@method_decorator(identity_required, name="dispatch")
+class SystemSettingsPage(FormView):
+ """
+ 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!
+ """
+
+ template_name = "settings/settings_system.html"
+ options_class = Config.SystemOptions
+ section: ClassVar[str]
+ options: Dict[str, Dict[str, str]]
+
+ 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")]
+ ),
+ )
+ elif config_field.type_ is str:
+ form_field = forms.CharField
+ 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)
+
+ def load_config(self):
+ return Config.load_system()
+
+ def save_config(self, key, value):
+ Config.set_system(key, value)
+
+ 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
+ return context
+
+ def form_valid(self, form):
+ # Save each key
+ for field in form:
+ self.save_config(
+ field.name,
+ form.cleaned_data[field.name],
+ )
+ return redirect(".")
+
+
+class BasicPage(SystemSettingsPage):
+
+ section = "basic"
+
+ options = {
+ "site_name": {
+ "title": "Site Name",
+ "help_text": "Shown in the top-left of the page, and titles",
+ },
+ "highlight_color": {
+ "title": "Highlight Color",
+ "help_text": "Used for logo background and other highlights",
+ },
+ }