Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1938] Added theme stylesheet field to SiteConfiguration #911

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works really nice! I do wonder if it is wise to put this button in this section here? We decided to 'hide' the other CSS field much lower in the Admin config at 'Geavanceerde opties' - and I guess that's the one that overrides everything as a final last load? So this uploaded file is not the last one loaded? (which is fine actually)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiromaykin Good point, I moved it in the admin to the same part as the extra CSS.

And you are correct this uploaded file is placed before the extra CSS field, seems most sensible but we can change it if necessary.

)
},
),
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
Loading