-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2030] Add option to upload custom fonts
- Loading branch information
Showing
11 changed files
with
288 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.db import models | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
class FontFileName(models.TextChoices): | ||
body = _("text_body_font"), _("Text body font") | ||
heading = _("heading_font"), _("Heading font") |
69 changes: 69 additions & 0 deletions
69
src/open_inwoner/configurations/migrations/0058_customfontset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Generated by Django 3.2.23 on 2024-01-29 10:45 | ||
|
||
import django.core.validators | ||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
import open_inwoner.configurations.models | ||
import open_inwoner.utils.files | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("configurations", "0057_siteconfiguration_theme_stylesheet"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="CustomFontSet", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"text_body_font", | ||
open_inwoner.configurations.models.CustomFontField( | ||
blank=True, | ||
help_text="Upload text body font. TTF font types only.", | ||
null=True, | ||
storage=open_inwoner.utils.files.OverwriteStorage(), | ||
upload_to=open_inwoner.configurations.models.CustomFontSet.update_filename_body, | ||
validators=[ | ||
django.core.validators.FileExtensionValidator(["ttf"]) | ||
], | ||
verbose_name="Text body font", | ||
), | ||
), | ||
( | ||
"heading_font", | ||
open_inwoner.configurations.models.CustomFontField( | ||
blank=True, | ||
help_text="Upload heading font. TTF font types only.", | ||
null=True, | ||
storage=open_inwoner.utils.files.OverwriteStorage(), | ||
upload_to=open_inwoner.configurations.models.CustomFontSet.update_filename_heading, | ||
validators=[ | ||
django.core.validators.FileExtensionValidator(["ttf"]) | ||
], | ||
verbose_name="Heading font", | ||
), | ||
), | ||
( | ||
"site_configuration", | ||
models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="custom_fonts", | ||
to="configurations.siteconfiguration", | ||
verbose_name="Configuration", | ||
), | ||
), | ||
], | ||
), | ||
] |
13 changes: 13 additions & 0 deletions
13
src/open_inwoner/configurations/migrations/0059_merge_20240129_1645.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Generated by Django 3.2.23 on 2024-01-29 15:45 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("configurations", "0058_customfontset"), | ||
("configurations", "0058_siteconfiguration_recipients_email_digest"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from django.urls import reverse | ||
|
||
from django_webtest import WebTest | ||
from webtest import Upload | ||
|
||
from open_inwoner.accounts.tests.factories import UserFactory | ||
|
||
from ...utils.test import ClearCachesMixin | ||
from ..models import CustomFontSet, SiteConfiguration | ||
|
||
|
||
class CustomFontsTest(ClearCachesMixin, WebTest): | ||
def setUp(self): | ||
self.user = UserFactory(is_superuser=True, is_staff=True) | ||
|
||
self.config = SiteConfiguration.get_solo() | ||
self.config.custom_fonts = CustomFontSet() | ||
self.config.save() | ||
|
||
self.form = self.app.get( | ||
reverse("admin:configurations_siteconfiguration_change"), user=self.user | ||
).forms["siteconfiguration_form"] | ||
|
||
def test_upload_font_correct_filetype(self): | ||
font_file = Upload("valid.ttf", b"content", content_type="font/ttf") | ||
self.form["name"] = "Test" | ||
self.form["custom_fonts-0-text_body_font"] = font_file | ||
self.form["custom_fonts-0-heading_font"] = font_file | ||
|
||
self.form.submit() | ||
|
||
custom_font_set = CustomFontSet.objects.first() | ||
body_font = custom_font_set.text_body_font | ||
heading_font = custom_font_set.heading_font | ||
|
||
self.assertEqual(body_font.name, "custom_fonts/text_body_font.ttf") | ||
self.assertEqual(heading_font.name, "custom_fonts/heading_font.ttf") | ||
|
||
# test file overwrite: upload again | ||
another_font_file = Upload( | ||
"valid_encore.ttf", b"content", content_type="font/ttf" | ||
) | ||
self.form["custom_fonts-0-text_body_font"] = another_font_file | ||
self.form["custom_fonts-0-heading_font"] = another_font_file | ||
|
||
self.form.submit() | ||
|
||
self.assertEqual(len(CustomFontSet.objects.all()), 1) | ||
|
||
custom_font_set = CustomFontSet.objects.first() | ||
body_font = custom_font_set.text_body_font | ||
heading_font = custom_font_set.heading_font | ||
|
||
self.assertEqual(body_font.name, "custom_fonts/text_body_font.ttf") | ||
self.assertEqual(heading_font.name, "custom_fonts/heading_font.ttf") | ||
|
||
def test_upload_font_incorrect_filetype(self): | ||
font_file = Upload("invalid.svg", b"content", content_type="font/svg") | ||
self.form["name"] = "Test" | ||
self.form["custom_fonts-0-text_body_font"] = font_file | ||
|
||
response = self.form.submit() | ||
|
||
self.assertEquals( | ||
response.context["errors"], | ||
[ | ||
[ | ||
"Bestandsextensie ‘svg’ is niet toegestaan. Toegestane extensies zijn: ‘ttf’." | ||
] | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.