Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Reduce size of generated HTML

Roland Geiderc073666835d6
diff --git a/docs/settings.rst b/docs/settings.rst
index 2a038a713..b41f5d8c0 100644
--- a/docs/settings.rst
+++ b/docs/settings.rst
@@ -4,8 +4,14 @@ Settings
You can configure some of the application behaviour with the ``WGER_SETTINGS``
dictionary in your settings file. Currently the following options are supported:
-* **USE_RECAPTCHA**: Default ``False``. Controls whether a captcha challenge
- will be presented when new users register.
+**USE_RECAPTCHA**: Default ``False``.
+ Controls whether a captcha challenge will be presented when new users register.
+
+**REMOVE_WHITESPACE**: Default ``False``.
+ Removes whitespaces around HTML tags to reduce the size of the resulting HTML.
+ If you are not serving the site using TLS you probably want to use the GZip
+ middleware instead. Read the django documentation on the security implications
+ (BREACH attack).
.. note::
diff --git a/wger/core/templates/mobile/template.html b/wger/core/templates/mobile/template.html
index 5b3cb7b26..4f31343eb 100644
--- a/wger/core/templates/mobile/template.html
+++ b/wger/core/templates/mobile/template.html
@@ -22,6 +22,7 @@
{% clear_breadcrumbs %}
{% endblock %}
<html>
+{% spaceless_config %}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8">
@@ -199,4 +200,5 @@
<script src="{% static 'js/shariff.min.js' %}"></script>
<script src="https://login.persona.org/include.js" type="text/javascript"></script>
</body>
+{% endspaceless_config %}
</html>
diff --git a/wger/core/templates/template.html b/wger/core/templates/template.html
index cdee0fa40..0d926951d 100644
--- a/wger/core/templates/template.html
+++ b/wger/core/templates/template.html
@@ -21,6 +21,7 @@
{% clear_breadcrumbs %}
{% endblock %}
<html lang="{{language.short_name}}">
+{% spaceless_config %}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8">
@@ -219,4 +220,5 @@
<script src="{% static 'js/shariff.min.js' %}"></script>
</body>
+{% endspaceless_config %}
</html>
diff --git a/wger/core/templates/template_features.html b/wger/core/templates/template_features.html
index 6bf168cda..fa0d19798 100644
--- a/wger/core/templates/template_features.html
+++ b/wger/core/templates/template_features.html
@@ -17,8 +17,9 @@
-->
-{% load i18n staticfiles compress %}
+{% load i18n staticfiles compress wger_extras %}
<html lang="{{language.short_name}}">
+{% spaceless_config %}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8">
@@ -108,4 +109,5 @@
<script src="{% static 'js/shariff.min.js' %}"></script>
</body>
+{% endspaceless_config %}
</html>
diff --git a/wger/core/templatetags/wger_extras.py b/wger/core/templatetags/wger_extras.py
index 0fdaaa0ac..898cab06b 100644
--- a/wger/core/templatetags/wger_extras.py
+++ b/wger/core/templatetags/wger_extras.py
@@ -18,6 +18,7 @@ from django import template
from django.forms.widgets import CheckboxInput, ClearableFileInput
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import pgettext
+from django.conf import settings
from wger.utils.constants import PAGINATION_MAX_TOTAL_PAGES
from wger.utils.constants import PAGINATION_PAGES_AROUND_CURRENT
@@ -167,6 +168,29 @@ def format_username(user):
return user.username
+class SpacelessNode(template.base.Node):
+ def __init__(self, nodelist):
+ self.nodelist = nodelist
+
+ def render(self, context):
+ if settings.WGER_SETTINGS['REMOVE_WHITESPACE']:
+ from django.utils.html import strip_spaces_between_tags
+ return strip_spaces_between_tags(self.nodelist.render(context).strip())
+ else:
+ return self.nodelist.render(context)
+
+
+def spaceless_config(parser, token):
+ '''
+ This is django's spaceless tag, copied here to use our configurable
+ SpacelessNode
+ '''
+ nodelist = parser.parse(('endspaceless_config',))
+ parser.delete_first_token()
+ return SpacelessNode(nodelist)
+
+
#
# Form utils
#
diff --git a/wger/core/tests/test_spaceless.py b/wger/core/tests/test_spaceless.py
new file mode 100644
index 000000000..6f7130e59
--- /dev/null
+++ b/wger/core/tests/test_spaceless.py
@@ -0,0 +1,41 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+import logging
+
+from django.template import Template, Context
+
+from wger.manager.tests.testcase import WorkoutManagerTestCase
+
+logger = logging.getLogger(__name__)
+
+
+class SpacelessTestCase(WorkoutManagerTestCase):
+ '''
+ Tests the custom spaceless template tag
+ '''
+
+ def test_spaceless(self):
+ '''
+ Tests the custom spaceless template tag
+ '''
+ t = Template('{% load wger_extras %}'
+ '{% spaceless_config %}<p>A text</p> <p>more</p>{% endspaceless_config %}')
+ context = Context()
+
+ with self.settings(WGER_SETTINGS={'REMOVE_WHITESPACE': True}):
+ self.assertEqual(t.render(context), '<p>A text</p><p>more</p>')
+
+ with self.settings(WGER_SETTINGS={'REMOVE_WHITESPACE': False}):
+ self.assertEqual(t.render(context), '<p>A text</p> <p>more</p>')
diff --git a/wger/core/views/user.py b/wger/core/views/user.py
index b1cd42a13..75f647e78 100644
--- a/wger/core/views/user.py
+++ b/wger/core/views/user.py
@@ -31,8 +31,8 @@ from django.contrib.auth.models import User as Django_User, User
from django.contrib.auth.views import login as django_loginview
from django.contrib import messages
from django.views.generic import RedirectView, UpdateView, DetailView
+from django.conf import settings
from rest_framework.authtoken.models import Token
-from wger.gym.models import AdminUserNote, GymUserConfig
from wger.utils.constants import USER_TAB
from wger.utils.generic_views import WgerPermissionMixin
@@ -50,7 +50,7 @@ from wger.manager.models import Workout
from wger.nutrition.models import NutritionPlan
from wger.config.models import GymConfig
from wger.weight.models import WeightEntry
-from django.conf import settings
+from wger.gym.models import AdminUserNote, GymUserConfig
logger = logging.getLogger(__name__)
diff --git a/wger/settings_global.py b/wger/settings_global.py
index e1f67d234..59f11bc91 100644
--- a/wger/settings_global.py
+++ b/wger/settings_global.py
@@ -315,5 +315,6 @@ IGNORABLE_404_URLS = (
# Application specific configuration options
#
WGER_SETTINGS = {
- 'USE_RECAPTCHA': False
+ 'USE_RECAPTCHA': False,
+ 'REMOVE_WHITESPACE': False
}
diff --git a/wger/software/templates/changelog.html b/wger/software/templates/changelog.html
index 32ec3bba0..09471f103 100644
--- a/wger/software/templates/changelog.html
+++ b/wger/software/templates/changelog.html
@@ -19,6 +19,10 @@
Allow external access to workouts and other pages to allow for sharing
<a href="https://github.com/rolandgeider/wger/issues/102">#102</a>
</li>
+ <li>
+ Reduce size of generated html code
+ <a href="https://github.com/rolandgeider/wger/issues/125">#125</a>
+ </li>
<li>
Improve installation, development and maintenance documentation
<a href="https://github.com/rolandgeider/wger/issues/114">#114</a>