mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
Feature: app branding (#5357)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework import status
|
||||
@@ -49,10 +50,34 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
|
||||
"rotate_pages_threshold": None,
|
||||
"max_image_pixels": None,
|
||||
"color_conversion_strategy": None,
|
||||
"app_title": None,
|
||||
"app_logo": None,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
def test_api_get_ui_settings_with_config(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Existing config with app_title, app_logo specified
|
||||
WHEN:
|
||||
- API to retrieve uisettings is called
|
||||
THEN:
|
||||
- app_title and app_logo are included
|
||||
"""
|
||||
config = ApplicationConfiguration.objects.first()
|
||||
config.app_title = "Fancy New Title"
|
||||
config.app_logo = "/logo/example.jpg"
|
||||
config.save()
|
||||
response = self.client.get("/api/ui_settings/", format="json")
|
||||
self.assertDictContainsSubset(
|
||||
{
|
||||
"app_title": config.app_title,
|
||||
"app_logo": config.app_logo,
|
||||
},
|
||||
response.data["settings"],
|
||||
)
|
||||
|
||||
def test_api_update_config(self):
|
||||
"""
|
||||
GIVEN:
|
||||
@@ -100,3 +125,37 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
|
||||
config = ApplicationConfiguration.objects.first()
|
||||
self.assertEqual(config.user_args, None)
|
||||
self.assertEqual(config.language, None)
|
||||
|
||||
def test_api_replace_app_logo(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Existing config with app_logo specified
|
||||
WHEN:
|
||||
- API to replace app_logo is called
|
||||
THEN:
|
||||
- old app_logo file is deleted
|
||||
"""
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.jpg"),
|
||||
"rb",
|
||||
) as f:
|
||||
self.client.patch(
|
||||
f"{self.ENDPOINT}1/",
|
||||
{
|
||||
"app_logo": f,
|
||||
},
|
||||
)
|
||||
config = ApplicationConfiguration.objects.first()
|
||||
old_logo = config.app_logo
|
||||
self.assertTrue(os.path.exists(old_logo.path))
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.png"),
|
||||
"rb",
|
||||
) as f:
|
||||
self.client.patch(
|
||||
f"{self.ENDPOINT}1/",
|
||||
{
|
||||
"app_logo": f,
|
||||
},
|
||||
)
|
||||
self.assertFalse(os.path.exists(old_logo.path))
|
||||
|
@@ -35,6 +35,8 @@ class TestApiUiSettings(DirectoriesMixin, APITestCase):
|
||||
self.assertDictEqual(
|
||||
response.data["settings"],
|
||||
{
|
||||
"app_title": None,
|
||||
"app_logo": None,
|
||||
"update_checking": {
|
||||
"backend_setting": "default",
|
||||
},
|
||||
|
@@ -120,6 +120,7 @@ from documents.serialisers import WorkflowTriggerSerializer
|
||||
from documents.signals import document_updated
|
||||
from documents.tasks import consume_file
|
||||
from paperless import version
|
||||
from paperless.config import GeneralConfig
|
||||
from paperless.db import GnuPG
|
||||
from paperless.views import StandardPagination
|
||||
|
||||
@@ -1164,6 +1165,16 @@ class UiSettingsView(GenericAPIView):
|
||||
ui_settings["update_checking"] = {
|
||||
"backend_setting": settings.ENABLE_UPDATE_CHECK,
|
||||
}
|
||||
|
||||
general_config = GeneralConfig()
|
||||
|
||||
ui_settings["app_title"] = settings.APP_TITLE
|
||||
if general_config.app_title is not None and len(general_config.app_title) > 0:
|
||||
ui_settings["app_title"] = general_config.app_title
|
||||
ui_settings["app_logo"] = settings.APP_LOGO
|
||||
if general_config.app_logo is not None and len(general_config.app_logo) > 0:
|
||||
ui_settings["app_logo"] = general_config.app_logo
|
||||
|
||||
user_resp = {
|
||||
"id": user.id,
|
||||
"username": user.username,
|
||||
|
@@ -8,13 +8,11 @@ from paperless.models import ApplicationConfiguration
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class OutputTypeConfig:
|
||||
class BaseConfig:
|
||||
"""
|
||||
Almost all parsers care about the chosen PDF output format
|
||||
"""
|
||||
|
||||
output_type: str = dataclasses.field(init=False)
|
||||
|
||||
@staticmethod
|
||||
def _get_config_instance() -> ApplicationConfiguration:
|
||||
app_config = ApplicationConfiguration.objects.all().first()
|
||||
@@ -24,6 +22,15 @@ class OutputTypeConfig:
|
||||
app_config = ApplicationConfiguration.objects.all().first()
|
||||
return app_config
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class OutputTypeConfig(BaseConfig):
|
||||
"""
|
||||
Almost all parsers care about the chosen PDF output format
|
||||
"""
|
||||
|
||||
output_type: str = dataclasses.field(init=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
app_config = self._get_config_instance()
|
||||
|
||||
@@ -86,3 +93,19 @@ class OcrConfig(OutputTypeConfig):
|
||||
user_args = {}
|
||||
|
||||
self.user_args = user_args
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class GeneralConfig(BaseConfig):
|
||||
"""
|
||||
General application settings that require global scope
|
||||
"""
|
||||
|
||||
app_title: str = dataclasses.field(init=False)
|
||||
app_logo: str = dataclasses.field(init=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
app_config = self._get_config_instance()
|
||||
|
||||
self.app_title = app_config.app_title or None
|
||||
self.app_logo = app_config.app_logo.url if app_config.app_logo else None
|
||||
|
@@ -0,0 +1,33 @@
|
||||
# Generated by Django 4.2.9 on 2024-01-12 05:33
|
||||
|
||||
from django.db import migrations
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("paperless", "0001_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="applicationconfiguration",
|
||||
name="app_logo",
|
||||
field=models.FileField(
|
||||
blank=True,
|
||||
null=True,
|
||||
upload_to="",
|
||||
verbose_name="Application logo",
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="applicationconfiguration",
|
||||
name="app_title",
|
||||
field=models.CharField(
|
||||
blank=True,
|
||||
max_length=48,
|
||||
null=True,
|
||||
verbose_name="Application title",
|
||||
),
|
||||
),
|
||||
]
|
@@ -1,3 +1,4 @@
|
||||
from django.core.validators import FileExtensionValidator
|
||||
from django.core.validators import MinValueValidator
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -166,6 +167,23 @@ class ApplicationConfiguration(AbstractSingletonModel):
|
||||
null=True,
|
||||
)
|
||||
|
||||
app_title = models.CharField(
|
||||
verbose_name=_("Application title"),
|
||||
null=True,
|
||||
blank=True,
|
||||
max_length=48,
|
||||
)
|
||||
|
||||
app_logo = models.FileField(
|
||||
verbose_name=_("Application logo"),
|
||||
null=True,
|
||||
blank=True,
|
||||
validators=[
|
||||
FileExtensionValidator(allowed_extensions=["jpg", "png", "gif", "svg"]),
|
||||
],
|
||||
upload_to="logo/",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("paperless application settings")
|
||||
|
||||
|
@@ -132,6 +132,11 @@ class ApplicationConfigurationSerializer(serializers.ModelSerializer):
|
||||
data["language"] = None
|
||||
return super().run_validation(data)
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
if instance.app_logo and "app_logo" in validated_data:
|
||||
instance.app_logo.delete()
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
class Meta:
|
||||
model = ApplicationConfiguration
|
||||
fields = "__all__"
|
||||
|
@@ -367,6 +367,7 @@ STORAGES = {
|
||||
"staticfiles": {
|
||||
"BACKEND": _static_backend,
|
||||
},
|
||||
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
|
||||
}
|
||||
|
||||
_CELERY_REDIS_URL, _CHANNELS_REDIS_URL = _parse_redis_url(
|
||||
@@ -999,6 +1000,9 @@ ENABLE_UPDATE_CHECK = os.getenv("PAPERLESS_ENABLE_UPDATE_CHECK", "default")
|
||||
if ENABLE_UPDATE_CHECK != "default":
|
||||
ENABLE_UPDATE_CHECK = __get_boolean("PAPERLESS_ENABLE_UPDATE_CHECK")
|
||||
|
||||
APP_TITLE = os.getenv("PAPERLESS_APP_TITLE", None)
|
||||
APP_LOGO = os.getenv("PAPERLESS_APP_LOGO", None)
|
||||
|
||||
###############################################################################
|
||||
# Machine Learning #
|
||||
###############################################################################
|
||||
|
@@ -1,3 +1,5 @@
|
||||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from django.conf.urls import include
|
||||
from django.contrib import admin
|
||||
@@ -8,6 +10,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views.decorators.csrf import ensure_csrf_cookie
|
||||
from django.views.generic import RedirectView
|
||||
from django.views.static import serve
|
||||
from rest_framework.authtoken import views
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
@@ -181,6 +184,12 @@ urlpatterns = [
|
||||
url=settings.STATIC_URL + "frontend/en-US/assets/%(path)s",
|
||||
),
|
||||
),
|
||||
# App logo
|
||||
re_path(
|
||||
r"^logo(?P<path>.*)$",
|
||||
serve,
|
||||
kwargs={"document_root": os.path.join(settings.MEDIA_ROOT, "logo")},
|
||||
),
|
||||
# TODO: with localization, this is even worse! :/
|
||||
# login, logout
|
||||
path("accounts/", include("django.contrib.auth.urls")),
|
||||
|
Reference in New Issue
Block a user