From 0fc99c2a0c0398a8cbd98d8914703cfaa44a36a7 Mon Sep 17 00:00:00 2001
From: Gabriel Augendre
Date: Tue, 5 Jan 2021 17:59:58 +0100
Subject: [PATCH] Switch to offline compressed assets
---
articles/compressor.py | 37 ++++++++++++++++++
articles/models.py | 7 +++-
.../templates/articles/article_detail.html | 4 +-
articles/templates/articles/article_list.html | 11 +-----
articles/templates/articles/base.html | 16 ++++----
articles/tests/conftest.py | 12 ++++++
articles/tests/test_models.py | 38 +++++++++++++++++++
blog/settings.py | 15 ++++++--
docker/run.sh | 1 +
9 files changed, 117 insertions(+), 24 deletions(-)
create mode 100644 articles/compressor.py
diff --git a/articles/compressor.py b/articles/compressor.py
new file mode 100644
index 0000000..ed3a014
--- /dev/null
+++ b/articles/compressor.py
@@ -0,0 +1,37 @@
+import itertools
+
+from django.conf import settings
+
+
+class DummyArticleWithCode:
+ has_code = True
+
+
+class DummyArticleNoCode:
+ has_code = False
+
+
+class DummyNonAuthenticatedUser:
+ is_authenticated = False
+
+
+class DummyAuthenticatedUser:
+ is_authenticated = True
+
+
+def offline_context():
+ article_possibilities = [None, DummyArticleWithCode(), DummyArticleNoCode()]
+ user_possibilities = [DummyAuthenticatedUser(), DummyNonAuthenticatedUser()]
+ goatcounter_possibilities = [None, settings.GOATCOUNTER_DOMAIN]
+ all_possibilities = [
+ article_possibilities,
+ user_possibilities,
+ goatcounter_possibilities,
+ ]
+ for _tuple in itertools.product(*all_possibilities):
+ yield {
+ "STATIC_URL": settings.STATIC_URL,
+ "article": _tuple[0],
+ "user": _tuple[1],
+ "goatcounter_domain": _tuple[2],
+ }
diff --git a/articles/models.py b/articles/models.py
index 22a97c1..c98d700 100644
--- a/articles/models.py
+++ b/articles/models.py
@@ -1,7 +1,8 @@
import random
import uuid
-from functools import cached_property, reduce
+from functools import cached_property
+import rcssmin
import readtime
from django.contrib.auth.models import AbstractUser
from django.contrib.contenttypes.models import ContentType
@@ -121,6 +122,10 @@ class Article(models.Model):
filter(None, map(lambda k: k.strip().lower(), self.keywords.split(",")))
)
+ @cached_property
+ def get_minified_custom_css(self):
+ return rcssmin.cssmin(self.custom_css)
+
def get_admin_url(self):
content_type = ContentType.objects.get_for_model(self.__class__)
return reverse(
diff --git a/articles/templates/articles/article_detail.html b/articles/templates/articles/article_detail.html
index 9c5f076..db14712 100644
--- a/articles/templates/articles/article_detail.html
+++ b/articles/templates/articles/article_detail.html
@@ -1,9 +1,7 @@
{% extends 'articles/base.html' %}
{% block append_css %}
-
+
{% endblock %}
{% block title %}{{ article.title }} | {% endblock %}
diff --git a/articles/templates/articles/article_list.html b/articles/templates/articles/article_list.html
index 2f1d137..b489771 100644
--- a/articles/templates/articles/article_list.html
+++ b/articles/templates/articles/article_list.html
@@ -1,15 +1,8 @@
{% extends 'articles/base.html' %}
{% block append_css %}
-
-
+
+
{% endblock %}
{% block title %}{% endblock %}
diff --git a/articles/templates/articles/base.html b/articles/templates/articles/base.html
index 2d0bd1e..6b49484 100644
--- a/articles/templates/articles/base.html
+++ b/articles/templates/articles/base.html
@@ -22,15 +22,10 @@
{% if user.is_authenticated %}
{% endif %}
- {% block append_css %}
- {% endblock %}
{% endcompress %}
- {% compress js inline %}
- {% if user.is_authenticated %}
-
- {% endif %}
- {% endcompress %}
+ {% block append_css %}
+ {% endblock %}
{% include "articles/snippets/favicon.html" %}
@@ -60,7 +55,14 @@
for ongoing builds here.
+
{% include "articles/snippets/analytics.html" %}
+{% compress js inline %}
+ {% if user.is_authenticated %}
+
+ {% endif %}
+{% endcompress %}
+