Switch to django-compressor

This commit is contained in:
Gabriel Augendre 2021-01-04 17:58:41 +01:00
parent d26ee137bf
commit 159585e6cd
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
11 changed files with 97 additions and 114 deletions

View file

@ -121,18 +121,6 @@ class Article(models.Model):
filter(None, map(lambda k: k.strip().lower(), self.keywords.split(",")))
)
@cached_property
def get_minified_custom_css(self):
def reducer(res, next_char):
if len(res) == 0:
return next_char
if res[-1] == next_char == " ":
return res
return res + next_char
css = self.custom_css.replace("\n", " ")
return reduce(reducer, css, "")
def get_admin_url(self):
content_type = ContentType.objects.get_for_model(self.__class__)
return reverse(

View file

@ -1,8 +1,8 @@
{% extends 'articles/base.html' %}
{% block append_header %}
{% block append_css %}
<style>
{{ article.get_minified_custom_css }}
{{ article.custom_css }}
</style>
{% endblock %}

View file

@ -1,8 +1,8 @@
{% extends 'articles/base.html' %}
{% block append_header %}
{% block append_css %}
<style>
{{ article.get_minified_custom_css }}
{{ article.custom_css }}
</style>
<style>
.pagination {

View file

@ -1,3 +1,4 @@
{% load static compress %}
<!DOCTYPE html>
<html lang="en">
{% spaceless %}
@ -9,10 +10,29 @@
<title>{% block title %}Home | {% endblock %}{{ blog_title }} by {{ blog_author }}</title>
<link rel="alternate" type="application/rss+xml" title="Gab's Notes » Feed" href="{% url 'complete-feed' %}">
{% include "articles/snippets/page_metadata.html" %}
{% include "articles/snippets/load_styles_and_scripts.html" %}
{% compress css inline %}
<link rel="stylesheet" href="{% static "vendor/newcss.css" %}" type="text/css">
<link rel="stylesheet" href="{% static "public.css" %}" type="text/css">
<link rel="stylesheet" href="{% static "admonitions.css" %}" type="text/css">
{% if article and article.has_code %}
<link rel="stylesheet" href="{% static "vendor/codehilite.css" %}" type="text/css">
{% endif %}
{% if user.is_authenticated %}
<link rel="stylesheet" href="{% static "authenticated.css" %}">
{% endif %}
{% block append_css %}
{% endblock %}
{% endcompress %}
{% compress js inline %}
{% if user.is_authenticated %}
<script src="{% static 'edit-keymap.js' %}" async></script>
{% endif %}
{% endcompress %}
{% include "articles/snippets/favicon.html" %}
{% include "articles/snippets/analytics.html" %}
{% block append_header %}{% endblock %}
</head>
<body>
<header>

View file

@ -1,16 +0,0 @@
{% load assets %}
{% load static %}
{% if article and article.has_code %}
{% assets "public_with_code" %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
{% else %}
{% assets "public_no_code" %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
{% endif %}
{% if user.is_authenticated %}
<link rel="stylesheet" href="{% static "authenticated.css" %}">
<script src="{% static 'edit-keymap.js' %}" async></script>
{% endif %}

View file

@ -44,9 +44,3 @@ def unpublished_article(author: User) -> Article:
content="## some draft article markdown\n\n[a draft article link](https://article.com)",
draft_key=uuid.uuid4(),
)
@pytest.fixture(autouse=True, scope="session")
def collect_static():
call_command("collectstatic", "--no-input", "--clear")
call_command("assets", "build", "--manifest", "django")

View file

@ -39,43 +39,3 @@ def test_save_article_doesnt_change_existing_slug(published_article: Article):
published_article.title = "This is a brand new title"
published_article.save()
assert published_article.slug == original_slug
@pytest.mark.django_db
def test_empty_custom_css_minified(published_article):
published_article.custom_css = ""
assert published_article.get_minified_custom_css == ""
@pytest.mark.django_db
def test_simple_custom_css_minified(published_article):
published_article.custom_css = ".cls {\n background-color: red;\n}"
assert (
published_article.get_minified_custom_css == ".cls { background-color: red; }"
)
@pytest.mark.django_db
def test_larger_custom_css_minified(published_article):
published_article.custom_css = """\
.profile {
display: flex;
justify-content: space-evenly;
flex-wrap: wrap;
}
.profile img {
max-width: 200px;
min-width: 100px;
max-height: 200px;
min-height: 100px;
border-radius: 10%;
padding: 1rem;
flex-shrink: 1;
flex-grow: 0;
padding: 0;
}"""
assert (
published_article.get_minified_custom_css
== """.profile { display: flex; justify-content: space-evenly; flex-wrap: wrap; } .profile img { max-width: 200px; min-width: 100px; max-height: 200px; min-height: 100px; border-radius: 10%; padding: 1rem; flex-shrink: 1; flex-grow: 0; padding: 0; }"""
)

View file

@ -70,7 +70,7 @@ INSTALLED_APPS = [
"attachments",
"anymail",
"django_cleanup.apps.CleanupConfig",
"django_assets",
"compressor",
]
MIDDLEWARE = [
@ -165,7 +165,7 @@ STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"django_assets.finders.AssetsFinder",
"compressor.finders.CompressorFinder",
]
MEDIA_URL = "/media/"
@ -194,5 +194,16 @@ PLAUSIBLE_DOMAIN = os.getenv("PLAUSIBLE_DOMAIN")
LOGIN_URL = "admin:login"
ASSETS_AUTO_BUILD = DEBUG
ASSETS_DEBUG = False
# ASSETS_AUTO_BUILD = DEBUG
# ASSETS_DEBUG = False
COMPRESS_ENABLED = True
COMPRESS_FILTERS = {
"css": [
"compressor.filters.css_default.CssAbsoluteFilter",
"compressor.filters.cssmin.rCSSMinFilter",
],
"js": ["compressor.filters.jsmin.JSMinFilter"],
}
if DEBUG:
COMPRESS_DEBUG_TOGGLE = "nocompress"

View file

@ -2,5 +2,4 @@
set -eux
yes yes | python manage.py migrate
python manage.py collectstatic --noinput --clear
python manage.py assets build --manifest django
gunicorn blog.wsgi -b 0.0.0.0:8000 --log-file -

82
poetry.lock generated
View file

@ -115,7 +115,7 @@ python-versions = "*"
[[package]]
name = "django"
version = "3.1.4"
version = "3.1.5"
description = "A high-level Python Web framework that encourages rapid development and clean, pragmatic design."
category = "main"
optional = false
@ -148,16 +148,15 @@ amazon_ses = ["boto3"]
sparkpost = ["sparkpost"]
[[package]]
name = "django-assets"
version = "2.0"
description = "Asset management for Django, to compress and merge CSS and Javascript files."
name = "django-appconf"
version = "1.0.4"
description = "A helper class for handling configuration defaults of packaged apps gracefully."
category = "main"
optional = false
python-versions = "*"
[package.dependencies]
Django = ">=1.7"
webassets = ">=2.0"
django = "*"
[[package]]
name = "django-cleanup"
@ -167,6 +166,20 @@ category = "main"
optional = false
python-versions = "*"
[[package]]
name = "django-compressor"
version = "2.4"
description = "Compresses linked and inline JavaScript or CSS into single cached files."
category = "main"
optional = false
python-versions = "*"
[package.dependencies]
django-appconf = ">=1.0.3"
rcssmin = "1.0.6"
rjsmin = "1.1.0"
six = ">=1.12.0"
[[package]]
name = "filelock"
version = "3.0.12"
@ -199,7 +212,7 @@ python-versions = ">=3.5"
[[package]]
name = "identify"
version = "1.5.10"
version = "1.5.11"
description = "File identification library for Python"
category = "dev"
optional = false
@ -485,6 +498,14 @@ urllib3 = ">=1.21.1,<1.27"
security = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)"]
socks = ["PySocks (>=1.5.6,!=1.5.7)", "win-inet-pton"]
[[package]]
name = "rjsmin"
version = "1.1.0"
description = "Javascript Minifier"
category = "main"
optional = false
python-versions = "*"
[[package]]
name = "six"
version = "1.15.0"
@ -562,14 +583,6 @@ six = ">=1.9.0,<2"
docs = ["proselint (>=0.10.2)", "sphinx (>=3)", "sphinx-argparse (>=0.2.5)", "sphinx-rtd-theme (>=0.4.3)", "towncrier (>=19.9.0rc1)"]
testing = ["coverage (>=4)", "coverage-enable-subprocess (>=1)", "flaky (>=3)", "pytest (>=4)", "pytest-env (>=0.6.2)", "pytest-freezegun (>=0.4.1)", "pytest-mock (>=2)", "pytest-randomly (>=1)", "pytest-timeout (>=1)", "pytest-xdist (>=1.31.0)", "packaging (>=20.0)", "xonsh (>=0.9.16)"]
[[package]]
name = "webassets"
version = "2.0"
description = "Media asset management for Python, with glue code for various web frameworks"
category = "main"
optional = false
python-versions = "*"
[[package]]
name = "wrapt"
version = "1.12.1"
@ -593,7 +606,7 @@ multidict = ">=4.0"
[metadata]
lock-version = "1.1"
python-versions = "^3.8"
content-hash = "5080ed4f95447cb68fd53b157e6bce0d57e3250883c1e2f1de0bc4eaafd683a8"
content-hash = "0b9ac9fa575f31f30485f510b5da016453e0387bc8805e56ea52f5dc09de095d"
[metadata.files]
appdirs = [
@ -693,21 +706,25 @@ distlib = [
{file = "distlib-0.3.1.zip", hash = "sha256:edf6116872c863e1aa9d5bb7cb5e05a022c519a4594dc703843343a9ddd9bff1"},
]
django = [
{file = "Django-3.1.4-py3-none-any.whl", hash = "sha256:5c866205f15e7a7123f1eec6ab939d22d5bde1416635cab259684af66d8e48a2"},
{file = "Django-3.1.4.tar.gz", hash = "sha256:edb10b5c45e7e9c0fb1dc00b76ec7449aca258a39ffd613dbd078c51d19c9f03"},
{file = "Django-3.1.5-py3-none-any.whl", hash = "sha256:efa2ab96b33b20c2182db93147a0c3cd7769d418926f9e9f140a60dca7c64ca9"},
{file = "Django-3.1.5.tar.gz", hash = "sha256:2d78425ba74c7a1a74b196058b261b9733a8570782f4e2828974777ccca7edf7"},
]
django-anymail = [
{file = "django-anymail-7.2.1.tar.gz", hash = "sha256:7fadf9781573b30af5bb6ca2f39b345c4ca2547723ce677c3025292d0a309a96"},
{file = "django_anymail-7.2.1-py2.py3-none-any.whl", hash = "sha256:dc5d835049a5dbf738a0f4bd793c4655cd53ba455e2ead275845bc158d48cc63"},
]
django-assets = [
{file = "django-assets-2.0.tar.gz", hash = "sha256:8abb81e66b4db602bb7b5e5d1f1b5c6511c3e7d8d4804be50bbb77e5ce898639"},
{file = "django_assets-2.0-py3-none-any.whl", hash = "sha256:3e4267a453bb6a30a4bcfb4f971a6876a542b5f500167afda892973335c8ac60"},
django-appconf = [
{file = "django-appconf-1.0.4.tar.gz", hash = "sha256:be58deb54a43d77d2e1621fe59f787681376d3cd0b8bd8e4758ef6c3a6453380"},
{file = "django_appconf-1.0.4-py2.py3-none-any.whl", hash = "sha256:1b1d0e1069c843ebe8ae5aa48ec52403b1440402b320c3e3a206a0907e97bb06"},
]
django-cleanup = [
{file = "django-cleanup-5.1.0.tar.gz", hash = "sha256:8976aec12a22913afb3d1fcb541b1aedde2f5ec243e4260c5ff78bb6aa75a089"},
{file = "django_cleanup-5.1.0-py2.py3-none-any.whl", hash = "sha256:71e098c7d9ac3f3da40b95cff9c0bc51218d40ef419261232f46ba3141c50acc"},
]
django-compressor = [
{file = "django_compressor-2.4-py2.py3-none-any.whl", hash = "sha256:57ac0a696d061e5fc6fbc55381d2050f353b973fb97eee5593f39247bc0f30af"},
{file = "django_compressor-2.4.tar.gz", hash = "sha256:d2ed1c6137ddaac5536233ec0a819e14009553fee0a869bea65d03e5285ba74f"},
]
filelock = [
{file = "filelock-3.0.12-py3-none-any.whl", hash = "sha256:929b7d63ec5b7d6b71b0fa5ac14e030b3f70b75747cef1b10da9b879fef15836"},
{file = "filelock-3.0.12.tar.gz", hash = "sha256:18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59"},
@ -721,8 +738,8 @@ html2text = [
{file = "html2text-2020.1.16.tar.gz", hash = "sha256:e296318e16b059ddb97f7a8a1d6a5c1d7af4544049a01e261731d2d5cc277bbb"},
]
identify = [
{file = "identify-1.5.10-py2.py3-none-any.whl", hash = "sha256:cc86e6a9a390879dcc2976cef169dd9cc48843ed70b7380f321d1b118163c60e"},
{file = "identify-1.5.10.tar.gz", hash = "sha256:943cd299ac7f5715fcb3f684e2fc1594c1e0f22a90d15398e5888143bd4144b5"},
{file = "identify-1.5.11-py2.py3-none-any.whl", hash = "sha256:7aef7a5104d6254c162990e54a203cdc0fd202046b6c415bd5d636472f6565c4"},
{file = "identify-1.5.11.tar.gz", hash = "sha256:b2c71bf9f5c482c389cef816f3a15f1c9d7429ad70f497d4a2e522442d80c6de"},
]
idna = [
{file = "idna-2.10-py2.py3-none-any.whl", hash = "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"},
@ -927,6 +944,21 @@ requests = [
{file = "requests-2.25.1-py2.py3-none-any.whl", hash = "sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e"},
{file = "requests-2.25.1.tar.gz", hash = "sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804"},
]
rjsmin = [
{file = "rjsmin-1.1.0-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:799890bd07a048892d8d3deb9042dbc20b7f5d0eb7da91e9483c561033b23ce2"},
{file = "rjsmin-1.1.0-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:211c2fe8298951663bbc02acdffbf714f6793df54bfc50e1c6c9e71b3f2559a3"},
{file = "rjsmin-1.1.0-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:585e75a84d9199b68056fd4a083d9a61e2a92dfd10ff6d4ce5bdb04bc3bdbfaf"},
{file = "rjsmin-1.1.0-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:e487a7783ac4339e79ec610b98228eb9ac72178973e3dee16eba0e3feef25924"},
{file = "rjsmin-1.1.0-cp34-cp34m-manylinux1_i686.whl", hash = "sha256:0ab825839125eaca57cc59581d72e596e58a7a56fbc0839996b7528f0343a0a8"},
{file = "rjsmin-1.1.0-cp34-cp34m-manylinux1_x86_64.whl", hash = "sha256:6044ca86e917cd5bb2f95e6679a4192cef812122f28ee08c677513de019629b3"},
{file = "rjsmin-1.1.0-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:ecd29f1b3e66a4c0753105baec262b331bcbceefc22fbe6f7e8bcd2067bcb4d7"},
{file = "rjsmin-1.1.0-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:975b69754d6a76be47c0bead12367a1ca9220d08e5393f80bab0230d4625d1f4"},
{file = "rjsmin-1.1.0-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:466fe70cc5647c7c51b3260c7e2e323a98b2b173564247f9c89e977720a0645f"},
{file = "rjsmin-1.1.0-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:e3908b21ebb584ce74a6ac233bdb5f29485752c9d3be5e50c5484ed74169232c"},
{file = "rjsmin-1.1.0-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:714329db774a90947e0e2086cdddb80d5e8c4ac1c70c9f92436378dedb8ae345"},
{file = "rjsmin-1.1.0-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:dd0f4819df4243ffe4c964995794c79ca43943b5b756de84be92b445a652fb86"},
{file = "rjsmin-1.1.0.tar.gz", hash = "sha256:b15dc75c71f65d9493a8c7fa233fdcec823e3f1b88ad84a843ffef49b338ac32"},
]
six = [
{file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"},
{file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"},
@ -955,10 +987,6 @@ virtualenv = [
{file = "virtualenv-20.2.2-py2.py3-none-any.whl", hash = "sha256:54b05fc737ea9c9ee9f8340f579e5da5b09fb64fd010ab5757eb90268616907c"},
{file = "virtualenv-20.2.2.tar.gz", hash = "sha256:b7a8ec323ee02fb2312f098b6b4c9de99559b462775bc8fe3627a73706603c1b"},
]
webassets = [
{file = "webassets-2.0-py3-none-any.whl", hash = "sha256:a31a55147752ba1b3dc07dee0ad8c8efff274464e08bbdb88c1fd59ffd552724"},
{file = "webassets-2.0.tar.gz", hash = "sha256:167132337677c8cedc9705090f6d48da3fb262c8e0b2773b29f3352f050181cd"},
]
wrapt = [
{file = "wrapt-1.12.1.tar.gz", hash = "sha256:b62ffa81fb85f4332a4f609cab4ac40709470da05643a082ec1eb88e6d9b97d7"},
]

View file

@ -32,9 +32,8 @@ pillow = "^7.2"
django-cleanup = "^5.0"
requests = "^2.24"
html2text = "^2020.1.16"
django-assets = "^2.0"
rcssmin = "^1.0.6"
readtime = "^1.1.1"
django-compressor = "^2.4"
[tool.poetry.dev-dependencies]
pre-commit = "^2.7"