From 1b25ce3bb69a6daaad2601d1dd3e0c68d9d3b98c Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 3 May 2023 14:35:41 -0600 Subject: [PATCH] Single user domain mode --- core/models/config.py | 1 + core/views.py | 2 ++ templates/admin/domain_edit.html | 1 + users/views/admin/domains.py | 13 +++++++++++++ 4 files changed, 17 insertions(+) diff --git a/core/models/config.py b/core/models/config.py index 48eda4e..3c46947 100644 --- a/core/models/config.py +++ b/core/models/config.py @@ -296,3 +296,4 @@ class Config(models.Model): site_icon: UploadedImage | None = None hide_login: bool = False custom_css: str = "" + single_user: str = "" diff --git a/core/views.py b/core/views.py index 4171736..1b4f348 100644 --- a/core/views.py +++ b/core/views.py @@ -18,6 +18,8 @@ from core.models import Config def homepage(request): if request.user.is_authenticated: return Home.as_view()(request) + elif request.domain.config_domain.single_user: + return redirect(f"/@{request.domain.config_domain.single_user}/") else: return About.as_view()(request) diff --git a/templates/admin/domain_edit.html b/templates/admin/domain_edit.html index d030370..f2bdadc 100644 --- a/templates/admin/domain_edit.html +++ b/templates/admin/domain_edit.html @@ -21,6 +21,7 @@ {% include "forms/_field.html" with field=form.site_name %} {% include "forms/_field.html" with field=form.site_icon %} {% include "forms/_field.html" with field=form.hide_login %} + {% include "forms/_field.html" with field=form.single_user %} {% include "forms/_field.html" with field=form.custom_css %}
diff --git a/users/views/admin/domains.py b/users/views/admin/domains.py index 97b2b03..d2a98ec 100644 --- a/users/views/admin/domains.py +++ b/users/views/admin/domains.py @@ -1,4 +1,5 @@ from django import forms +from django.contrib import messages from django.core.files import File from django.core.validators import RegexValidator from django.db import models @@ -183,6 +184,12 @@ class DomainEdit(FormView): required=False, ) + single_user = forms.CharField( + label="Single User Profile Redirect", + help_text="If provided, redirect the homepage of this domain to this identity's profile for logged-out users\nUse the identity's full handle - for example, takahe@jointakahe.org", + required=False, + ) + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["domain"].disabled = True @@ -194,6 +201,9 @@ class DomainEdit(FormView): def clean_service_domain(self): return self.cleaned_data["service_domain"] + def clean_single_user(self): + return self.cleaned_data["single_user"].lstrip("@") + def dispatch(self, request, domain): self.domain = get_object_or_404( Domain.objects.filter(local=True), domain=domain @@ -218,6 +228,8 @@ class DomainEdit(FormView): if isinstance(form.cleaned_data["site_icon"], File): Config.set_domain(self.domain, "site_icon", form.cleaned_data["site_icon"]) Config.set_domain(self.domain, "custom_css", form.cleaned_data["custom_css"]) + Config.set_domain(self.domain, "single_user", form.cleaned_data["single_user"]) + messages.success(self.request, f"Domain {self.domain} saved.") return redirect(Domain.urls.root) def get_initial(self): @@ -232,6 +244,7 @@ class DomainEdit(FormView): "site_icon": self.domain.config_domain.site_icon, "hide_login": self.domain.config_domain.hide_login, "custom_css": self.domain.config_domain.custom_css, + "single_user": self.domain.config_domain.single_user, }