sync site config during boot; customize site logo

This commit is contained in:
Your Name 2023-08-20 18:27:20 +00:00 committed by Henri Dickson
parent 4702b4feb3
commit c45e9f6016
11 changed files with 69 additions and 12 deletions

View file

@ -1,14 +1,15 @@
import os import os
# import django_stubs_ext import environ
# django_stubs_ext.monkeypatch() env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
NEODB_VERSION = "0.8" NEODB_VERSION = "0.8"
DATABASE_ROUTERS = ["takahe.db_routes.TakaheRouter"] DATABASE_ROUTERS = ["takahe.db_routes.TakaheRouter"]
PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__))
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@ -406,3 +407,9 @@ OAUTH2_PROVIDER = {
OAUTH2_PROVIDER_APPLICATION_MODEL = "developer.Application" OAUTH2_PROVIDER_APPLICATION_MODEL = "developer.Application"
DEVELOPER_CONSOLE_APPLICATION_CLIENT_ID = "NEODB_DEVELOPER_CONSOLE" DEVELOPER_CONSOLE_APPLICATION_CLIENT_ID = "NEODB_DEVELOPER_CONSOLE"
SETUP_ADMIN_USERNAMES = [
u for u in os.environ.get("NEODB_ADMIN_USERNAMES", "").split(",") if u
]
SITE_INFO["site_logo"] = os.environ.get("NEODB_SITE_LOGO", "/s/img/logo.svg")

View file

@ -1,15 +1,54 @@
from django.conf import settings
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from loguru import logger
from catalog.search.typesense import Indexer from catalog.search.typesense import Indexer
from takahe.models import Config as TakaheConfig
from takahe.models import Domain as TakaheDomain
from takahe.models import Identity as TakaheIdentity
from takahe.models import User as TakaheUser
from users.models import User
class Command(BaseCommand): class Command(BaseCommand):
help = "Post-Migration Setup" help = "Post-Migration Setup"
def sync_site_config(self):
domain = settings.SITE_INFO["site_domain"]
name = settings.SITE_INFO["site_name"]
service_domain = settings.SITE_INFO.get("site_service_domain")
TakaheConfig.objects.update_or_create(
key="site_name",
user=None,
identity=None,
domain=None,
defaults={"json": name},
)
TakaheConfig.objects.update_or_create(
key="site_name",
user=None,
identity=None,
domain_id=domain,
defaults={"json": name},
)
def sync_admin_user(self):
users = User.objects.filter(username__in=settings.SETUP_ADMIN_USERNAMES)
for user in users:
if user.is_superuser:
logger.debug(f"User {user.username} is already admin")
else:
user.is_superuser = True
user.save(update_fields=["is_superuser"])
TakaheUser.objects.filter(email="@" + user.username).update(admin=True)
logger.info(f"Updated user {user.username} as admin")
def handle(self, *args, **options): def handle(self, *args, **options):
# Update site name if changed # Update site name if changed
self.sync_site_config()
# Create/update admin user if configured in env # Create/update admin user if configured in env
self.sync_admin_user()
# Create basic emoji if not exists # Create basic emoji if not exists

View file

@ -6,7 +6,7 @@
<nav> <nav>
<ul class="nav-logo"> <ul class="nav-logo">
<a href="{% url 'common:home' %}"> <a href="{% url 'common:home' %}">
<img src="{% static 'img/logo.svg' %}" alt="" /> <img src="{{ site_logo }}" alt="" />
</a> </a>
</ul> </ul>
<ul class="nav-search {% if request.GET.q %}unhide{% endif %}"> <ul class="nav-search {% if request.GET.q %}unhide{% endif %}">

View file

@ -51,7 +51,7 @@
} }
})(); })();
</script> </script>
<link rel="icon" href="{% static 'img/logo-icon.png' %}"> <link rel="icon" href="{{ site_logo }}">
<link rel="apple-touch-icon" href="{% static 'img/logo-icon.png' %}"> <link rel="apple-touch-icon" href="{{ site_logo }}">
<meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="{{ site_name }}"> <meta name="apple-mobile-web-app-title" content="{{ site_name }}">

View file

@ -17,8 +17,10 @@ x-shared:
environment: environment:
- NEODB_SITE_NAME - NEODB_SITE_NAME
- NEODB_SITE_DOMAIN - NEODB_SITE_DOMAIN
- NEODB_SITE_LOGO
- NEODB_DEBUG - NEODB_DEBUG
- NEODB_SECRET_KEY - NEODB_SECRET_KEY
- NEODB_ADMIN_USERNAMES
- NEODB_DB_NAME=neodb - NEODB_DB_NAME=neodb
- NEODB_DB_USER=neodb - NEODB_DB_USER=neodb
- NEODB_DB_PASSWORD=aubergine - NEODB_DB_PASSWORD=aubergine

View file

@ -4,15 +4,21 @@
# Change these before start the instance for the first time # Change these before start the instance for the first time
NEODB_SECRET_KEY=change_me NEODB_SECRET_KEY=change_me
NEODB_SITE_NAME=Example Site
NEODB_SITE_DOMAIN=example.site NEODB_SITE_DOMAIN=example.site
# Change these
NEODB_SITE_NAME=Example Site
NEODB_SITE_LOGO=/logo.svg
# HTTP port your reverse proxy should set request to # HTTP port your reverse proxy should set request to
# NEODB_PORT=8000 # NEODB_PORT=8000
# Path to store db/media/cache/etc, must be writable # Path to store db/media/cache/etc, must be writable
# NEODB_DATA=/var/lib/neodb # NEODB_DATA=/var/lib/neodb
# Users with these names will be promoted to admin during next boot/migration
# NEODB_ADMIN_USERNAMES = eggplant,aubergine
# Scaling parameters # Scaling parameters
# NEODB_WEB_WORKER_NUM=32 # NEODB_WEB_WORKER_NUM=32
# TAKAHE_WEB_WORKER_NUM=32 # TAKAHE_WEB_WORKER_NUM=32

View file

@ -6,6 +6,7 @@ django-anymail
django-auditlog @ git+https://github.com/jazzband/django-auditlog.git@45591463e8192b4ac0095e259cc4dcea0ac2fd6c django-auditlog @ git+https://github.com/jazzband/django-auditlog.git@45591463e8192b4ac0095e259cc4dcea0ac2fd6c
django-bleach django-bleach
django-compressor django-compressor
django-environ
django-hijack django-hijack
django-jsonform django-jsonform
django-maintenance-mode django-maintenance-mode

View file

@ -40,7 +40,9 @@ class APIdentity(models.Model):
@property @property
def is_active(self): def is_active(self):
return self.user.is_active and self.takahe_identity.deleted is None return (
self.user and self.user.is_active and self.takahe_identity.deleted is None
)
@property @property
def name(self): def name(self):

View file

@ -44,7 +44,7 @@
<body> <body>
<article> <article>
<header style="text-align: center;"> <header style="text-align: center;">
<img src="{% static 'img/logo.svg' %}" class="logo" alt="logo"> <img src="{{ site_logo }}" class="logo" alt="logo">
</header> </header>
<div> <div>
{% if request.user.is_authenticated %} {% if request.user.is_authenticated %}

View file

@ -12,7 +12,7 @@
<div class="container"> <div class="container">
<article> <article>
<header style="text-align: center;"> <header style="text-align: center;">
<img src="{% static 'img/logo.svg' %}" class="logo" alt="logo"> <img src="{{ site_logo }}" class="logo" alt="logo">
</header> </header>
{% if request.session.new_user %} {% if request.session.new_user %}
<h4>欢迎来到{{ site_name }}{{ request.user.mastodon_acct }}</h4> <h4>欢迎来到{{ site_name }}{{ request.user.mastodon_acct }}</h4>

View file

@ -12,7 +12,7 @@
<div class="container"> <div class="container">
<article> <article>
<header style="text-align: center;"> <header style="text-align: center;">
<img src="{% static 'img/logo.svg' %}" class="logo" alt="logo"> <img src="{{ site_logo }}" class="logo" alt="logo">
</header> </header>
<h4>验证电子邮件</h4> <h4>验证电子邮件</h4>
{% if success %} {% if success %}