2022-11-18 02:21:00 +00:00
|
|
|
from functools import partial
|
2022-11-17 00:23:46 +00:00
|
|
|
from typing import ClassVar
|
|
|
|
|
|
|
|
import pydantic
|
2022-11-18 02:21:00 +00:00
|
|
|
from django.core.files import File
|
2022-11-17 00:23:46 +00:00
|
|
|
from django.db import models
|
2022-11-18 02:21:00 +00:00
|
|
|
from django.templatetags.static import static
|
2022-11-17 00:23:46 +00:00
|
|
|
|
2022-11-18 02:21:00 +00:00
|
|
|
from core.uploads import upload_namer
|
|
|
|
from takahe import __version__
|
|
|
|
|
|
|
|
|
|
|
|
class UploadedImage(str):
|
|
|
|
"""
|
|
|
|
Type used to indicate a setting is an image
|
|
|
|
"""
|
|
|
|
|
2022-11-17 00:23:46 +00:00
|
|
|
|
|
|
|
class Config(models.Model):
|
|
|
|
"""
|
|
|
|
A configuration setting for either the server or a specific user or identity.
|
|
|
|
|
|
|
|
The possible options and their defaults are defined at the bottom of the file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
key = models.CharField(max_length=500)
|
|
|
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
"users.user",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
related_name="configs",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
|
|
|
|
identity = models.ForeignKey(
|
|
|
|
"users.identity",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
related_name="configs",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
|
|
|
|
json = models.JSONField(blank=True, null=True)
|
2022-11-18 02:21:00 +00:00
|
|
|
image = models.ImageField(
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
upload_to=partial(upload_namer, "config"),
|
|
|
|
)
|
2022-11-17 00:23:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = [
|
|
|
|
("key", "user", "identity"),
|
|
|
|
]
|
|
|
|
|
|
|
|
system: ClassVar["Config.ConfigOptions"] # type: ignore
|
|
|
|
|
|
|
|
@classmethod
|
2022-11-18 02:21:00 +00:00
|
|
|
def load_values(cls, options_class, filters):
|
2022-11-17 00:23:46 +00:00
|
|
|
"""
|
2022-11-18 02:21:00 +00:00
|
|
|
Loads config options and returns an object with them
|
2022-11-17 00:23:46 +00:00
|
|
|
"""
|
|
|
|
values = {}
|
2022-11-18 02:21:00 +00:00
|
|
|
for config in cls.objects.filter(**filters):
|
|
|
|
values[config.key] = config.image.url if config.image else config.json
|
|
|
|
if values[config.key] is None:
|
|
|
|
del values[config.key]
|
|
|
|
values["version"] = __version__
|
|
|
|
return options_class(**values)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_system(cls):
|
|
|
|
"""
|
|
|
|
Loads the system config options object
|
|
|
|
"""
|
|
|
|
return cls.load_values(
|
|
|
|
cls.SystemOptions,
|
|
|
|
{"identity__isnull": True, "user__isnull": True},
|
|
|
|
)
|
2022-11-17 00:23:46 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_user(cls, user):
|
|
|
|
"""
|
2022-11-18 02:21:00 +00:00
|
|
|
Loads a user config options object
|
2022-11-17 00:23:46 +00:00
|
|
|
"""
|
2022-11-18 02:21:00 +00:00
|
|
|
return cls.load_values(
|
|
|
|
cls.SystemOptions,
|
|
|
|
{"identity__isnull": True, "user": user},
|
|
|
|
)
|
2022-11-17 00:23:46 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_identity(cls, identity):
|
|
|
|
"""
|
2022-11-18 02:21:00 +00:00
|
|
|
Loads a user config options object
|
2022-11-17 00:23:46 +00:00
|
|
|
"""
|
2022-11-18 02:21:00 +00:00
|
|
|
return cls.load_values(
|
|
|
|
cls.IdentityOptions,
|
|
|
|
{"identity": identity, "user__isnull": True},
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_value(cls, key, value, options_class, filters):
|
|
|
|
config_field = options_class.__fields__[key]
|
|
|
|
if isinstance(value, File):
|
|
|
|
if config_field.type_ is not UploadedImage:
|
|
|
|
raise ValueError(f"Cannot save file to {key} of type: {type(value)}")
|
|
|
|
cls.objects.update_or_create(
|
|
|
|
key=key,
|
|
|
|
defaults={"json": None, "image": value},
|
|
|
|
**filters,
|
|
|
|
)
|
|
|
|
elif value is None:
|
|
|
|
cls.objects.filter(key=key, **filters).delete()
|
|
|
|
else:
|
|
|
|
if not isinstance(value, config_field.type_):
|
|
|
|
raise ValueError(f"Invalid type for {key}: {type(value)}")
|
|
|
|
if value == config_field.default:
|
|
|
|
cls.objects.filter(key=key, **filters).delete()
|
|
|
|
else:
|
|
|
|
cls.objects.update_or_create(
|
|
|
|
key=key,
|
|
|
|
defaults={"json": value},
|
|
|
|
**filters,
|
|
|
|
)
|
2022-11-17 00:23:46 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_system(cls, key, value):
|
2022-11-18 02:21:00 +00:00
|
|
|
cls.set_value(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
cls.SystemOptions,
|
|
|
|
{"identity__isnull": True, "user__isnull": True},
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_user(cls, user, key, value):
|
|
|
|
cls.set_value(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
cls.UserOptions,
|
|
|
|
{"identity__isnull": True, "user": user},
|
2022-11-17 00:23:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_identity(cls, identity, key, value):
|
2022-11-18 02:21:00 +00:00
|
|
|
cls.set_value(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
cls.IdentityOptions,
|
|
|
|
{"identity": identity, "user__isnull": True},
|
2022-11-17 00:23:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class SystemOptions(pydantic.BaseModel):
|
|
|
|
|
2022-11-18 02:21:00 +00:00
|
|
|
version: str = __version__
|
|
|
|
|
2022-11-21 01:29:19 +00:00
|
|
|
system_actor_public_key: str = ""
|
|
|
|
system_actor_private_key: str = ""
|
|
|
|
|
2022-11-18 02:16:34 +00:00
|
|
|
site_name: str = "Takahē"
|
2022-11-17 00:23:46 +00:00
|
|
|
highlight_color: str = "#449c8c"
|
2022-11-18 02:21:00 +00:00
|
|
|
site_about: str = "<h2>Welcome!</h2>\n\nThis is a community running Takahē."
|
|
|
|
site_icon: UploadedImage = static("img/icon-128.png")
|
|
|
|
site_banner: UploadedImage = static("img/fjords-banner-600.jpg")
|
|
|
|
|
2022-11-18 15:28:15 +00:00
|
|
|
signup_allowed: bool = True
|
2022-11-18 07:09:04 +00:00
|
|
|
signup_invite_only: bool = False
|
|
|
|
signup_text: str = ""
|
2022-11-18 02:16:34 +00:00
|
|
|
|
2022-11-18 00:43:00 +00:00
|
|
|
post_length: int = 500
|
2022-11-20 18:13:44 +00:00
|
|
|
identity_min_length: int = 2
|
2022-11-18 02:16:34 +00:00
|
|
|
identity_max_per_user: int = 5
|
2022-11-17 00:23:46 +00:00
|
|
|
identity_max_age: int = 24 * 60 * 60
|
|
|
|
|
2022-11-19 00:24:43 +00:00
|
|
|
restricted_usernames: str = "admin\nadmins\nadministrator\nadministrators\nsystem\nroot\nannounce\nannouncement\nannouncements"
|
|
|
|
|
2022-11-17 00:23:46 +00:00
|
|
|
class UserOptions(pydantic.BaseModel):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
class IdentityOptions(pydantic.BaseModel):
|
|
|
|
|
|
|
|
toot_mode: bool = False
|