Skip to content

Commit

Permalink
[#1938] Added theme stylesheet field to SiteConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart van der Schoor committed Dec 19, 2023
1 parent c14479b commit 92d3b49
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/open_inwoner/configurations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class SiteConfigurarionAdmin(OrderedInlineModelAdminMixin, SingletonModelAdmin):
"primary_font_color",
"secondary_font_color",
"accent_font_color",
"theme_stylesheet",
)
},
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.23 on 2023-12-19 08:40

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("configurations", "0056_alter_siteconfiguration_eherkenning_enabled"),
]

operations = [
migrations.AddField(
model_name="siteconfiguration",
name="theme_stylesheet",
field=models.FileField(
blank=True,
help_text="Additional CSS file added to the page.",
null=True,
upload_to="themes/",
validators=[django.core.validators.FileExtensionValidator(["css"])],
verbose_name="Theme stylesheet",
),
),
]
12 changes: 10 additions & 2 deletions src/open_inwoner/configurations/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Optional

from django.contrib.flatpages.models import FlatPage
from django.core.validators import FileExtensionValidator
from django.db import models
from django.utils.translation import ugettext_lazy as _

from colorfield.fields import ColorField
from filer.fields.file import FilerFileField
from filer.fields.image import FilerImageField
from ordered_model.models import OrderedModel, OrderedModelManager
from solo.models import SingletonModel
Expand Down Expand Up @@ -476,15 +478,21 @@ class SiteConfiguration(SingletonModel):
"Enable sharing of products on social media (Facebook, LinkedIn...)",
),
)

theme_stylesheet = models.FileField(
upload_to="themes/",
verbose_name=_("Theme stylesheet"),
help_text=_("Additional CSS file added to the page."),
validators=[FileExtensionValidator(["css"])],
blank=True,
null=True,
)
extra_css = CSSField(
blank=True,
verbose_name=_("Extra CSS"),
help_text=_(
"Additional CSS added to the page. Note only a (safe) subset of CSS properties is supported."
),
)

# authentication options
eherkenning_enabled = models.BooleanField(
verbose_name=_("eHerkenning authentication enabled"),
Expand Down
19 changes: 18 additions & 1 deletion src/open_inwoner/configurations/tests/test_extra_css.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import override_settings
from django.urls import reverse
from django.utils.html import escape

from django_webtest import WebTest

from ...cms.tests import cms_tools
from ...utils.test import ClearCachesMixin
from ...utils.test import ClearCachesMixin, temp_media_root
from ..models import SiteConfiguration


@temp_media_root()
@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class ThemestylesheetTest(ClearCachesMixin, WebTest):
def test_theme_stylesheet_renders_tag(self):
cms_tools.create_homepage()

self.config = SiteConfiguration.get_solo()
self.config.theme_stylesheet = ContentFile("text", "my_custom_theme.css")
self.config.save()

response = self.app.get(reverse("pages-root"))
response.showbrowser()
self.assertContains(response, self.config.theme_stylesheet.url)


@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class ExtraCSSTest(ClearCachesMixin, WebTest):
def test_extra_css_is_cleaned_on_save(self):
Expand Down
5 changes: 4 additions & 1 deletion src/open_inwoner/templates/master.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
--color-font-accent: {{theming.accent_font_color}};
}
</style>
{% render_block "css" %}
{% render_block "css" %}
{% if theme_stylesheet %}
<link id="theme-css" nonce="{{ request.csp_nonce }}" media="all" rel="stylesheet" href="{{ theme_stylesheet }}" />
{% endif %}
{% block extra_css %}{% endblock %}
{% block extra_head %}{% endblock %}
{% if extra_css %}
Expand Down
3 changes: 3 additions & 0 deletions src/open_inwoner/utils/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def settings(request):
"cookie_link_text": config.cookie_link_text,
"cookie_link_url": config.cookie_link_url,
"extra_css": config.extra_css,
"theme_stylesheet": (
config.theme_stylesheet.url if config.theme_stylesheet else None
),
"menu_categories": (
Category.get_root_nodes().published().visible_for_user(request.user)
),
Expand Down

0 comments on commit 92d3b49

Please sign in to comment.