From e644a00878754b4bec235d05dbb7b90ef0bfbf84 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 14 Nov 2024 20:47:26 -0800 Subject: [PATCH] Adding further cache optimizations, gzip compression, and minifying js --- crank/settings/base.py | 4 +--- crank/templatetags/socialapp_cache.py | 2 +- crank/tests/views/test_admin.py | 2 ++ crank/tests/views/test_fundinground.py | 3 +++ crank/tests/views/test_logout.py | 2 ++ crank/tests/views/test_organization_view.py | 2 ++ crank/tests/views/test_rtopolicy.py | 3 +++ package-lock.json | 1 + package.json | 1 + webpack.config.js | 4 +++- 10 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crank/settings/base.py b/crank/settings/base.py index 19907310..801a45ff 100644 --- a/crank/settings/base.py +++ b/crank/settings/base.py @@ -15,11 +15,8 @@ import os from pathlib import Path -from django.conf.global_settings import STATICFILES_DIRS, CACHE_MIDDLEWARE_SECONDS -from django.contrib.staticfiles.storage import ManifestStaticFilesStorage from dotenv import load_dotenv from opentelemetry.instrumentation.django import DjangoInstrumentor -from tests.test_settings import MANIFEST_LOADER load_dotenv() DjangoInstrumentor().instrument(is_sql_commentor_enabled=True) @@ -92,6 +89,7 @@ ) MIDDLEWARE = [ + 'django.middleware.gzip.GZipMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', diff --git a/crank/templatetags/socialapp_cache.py b/crank/templatetags/socialapp_cache.py index 0a0b2285..e9880f23 100644 --- a/crank/templatetags/socialapp_cache.py +++ b/crank/templatetags/socialapp_cache.py @@ -12,6 +12,6 @@ def get_cached_social_app(provider): cache_key = f'social_app_{provider}' social_app = cache.get(cache_key) if not social_app: - social_app = SocialApp.objects.filter(provider=provider).first() + social_app = SocialApp.objects.filter(provider=provider).prefetch_related('sites').first() cache.set(cache_key, social_app, timeout=settings.CACHE_MIDDLEWARE_SECONDS) return social_app \ No newline at end of file diff --git a/crank/tests/views/test_admin.py b/crank/tests/views/test_admin.py index 776a3377..ad8d23db 100644 --- a/crank/tests/views/test_admin.py +++ b/crank/tests/views/test_admin.py @@ -1,5 +1,6 @@ # Copyright (c) 2024 Isaac Adams # Licensed under the MIT License. See LICENSE file in the project root for full license information. +from django.core.cache import cache from django.test import TestCase, RequestFactory from django.contrib.admin.sites import AdminSite from crank.admin import ScoreInline, ScoreAlgorithmWeightInline @@ -16,6 +17,7 @@ def __init__(self, user=None): class ScoreInlineTest(TestCase): def setUp(self): + cache.clear() self.factory = RequestFactory() self.site = AdminSite() self.inline = ScoreInline(Organization, self.site) diff --git a/crank/tests/views/test_fundinground.py b/crank/tests/views/test_fundinground.py index adebe17c..b3b9c744 100644 --- a/crank/tests/views/test_fundinground.py +++ b/crank/tests/views/test_fundinground.py @@ -1,6 +1,8 @@ # Copyright (c) 2024 Isaac Adams # Licensed under the MIT License. See LICENSE file in the project root for full license information. import json + +from django.core.cache import cache from django.test import TestCase, RequestFactory from crank.views.fundinground import FundingRoundChoicesView from crank.models.organization import Organization @@ -8,6 +10,7 @@ class FundingRoundViewTest(TestCase): def setUp(self): + cache.clear() self.factory = RequestFactory() self.view = FundingRoundChoicesView.as_view() diff --git a/crank/tests/views/test_logout.py b/crank/tests/views/test_logout.py index 84cd8883..79fb803d 100644 --- a/crank/tests/views/test_logout.py +++ b/crank/tests/views/test_logout.py @@ -1,6 +1,7 @@ # Copyright (c) 2024 Isaac Adams # Licensed under the MIT License. See LICENSE file in the project root for full license information. # crank/tests/views/test_logout.py +from django.core.cache import cache from django.test import TestCase, Client from django.urls import reverse from django.contrib.auth.models import User @@ -8,6 +9,7 @@ class LogoutViewTests(TestCase): def setUp(self): + cache.clear() self.client = Client() self.user = User.objects.create_user(username='testuser', password='testpassword') self.client.login(username='testuser', password='testpassword') diff --git a/crank/tests/views/test_organization_view.py b/crank/tests/views/test_organization_view.py index fee9e155..1cc3ee3f 100644 --- a/crank/tests/views/test_organization_view.py +++ b/crank/tests/views/test_organization_view.py @@ -2,6 +2,7 @@ # Licensed under the MIT License. See LICENSE file in the project root for full license information. from allauth.socialaccount.models import SocialApp from django.contrib.sites.models import Site +from django.core.cache import cache from django.test import TestCase from django.urls import reverse from crank.models.organization import Organization @@ -9,6 +10,7 @@ class OrganizationViewTest(TestCase): def setUp(self): + cache.clear() self.active_org = Organization.objects.create(name="Active Org", status=1) self.inactive_org = Organization.objects.create(name="Inactive Org", status=0) self.social_app = SocialApp.objects.create( diff --git a/crank/tests/views/test_rtopolicy.py b/crank/tests/views/test_rtopolicy.py index 1059a3d5..402ea1fe 100644 --- a/crank/tests/views/test_rtopolicy.py +++ b/crank/tests/views/test_rtopolicy.py @@ -1,6 +1,8 @@ # Copyright (c) 2024 Isaac Adams # Licensed under the MIT License. See LICENSE file in the project root for full license information. import json + +from django.core.cache import cache from django.test import TestCase, RequestFactory from crank.views.rtopolicy import RTOPolicyChoicesView from crank.models.organization import Organization @@ -8,6 +10,7 @@ class RTOPolicyChoicesViewTest(TestCase): def setUp(self): + cache.clear() self.factory = RequestFactory() self.view = RTOPolicyChoicesView.as_view() diff --git a/package-lock.json b/package-lock.json index be998c16..ef8e47b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ "jest-environment-jsdom": "^29.7.0", "jest-fetch-mock": "^3.0.3", "nodemon": "^3.1.7", + "terser-webpack-plugin": "^5.3.10", "ts-jest": "^29.2.5", "ts-loader": "^9.5.1", "ts-node": "^10.9.2", diff --git a/package.json b/package.json index 619b2737..3e5f2653 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "jest-environment-jsdom": "^29.7.0", "jest-fetch-mock": "^3.0.3", "nodemon": "^3.1.7", + "terser-webpack-plugin": "^5.3.10", "ts-jest": "^29.2.5", "ts-loader": "^9.5.1", "ts-node": "^10.9.2", diff --git a/webpack.config.js b/webpack.config.js index 1ce1a8dc..19b0acd3 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,6 +1,7 @@ const path = require('path'); const {CleanWebpackPlugin} = require('clean-webpack-plugin'); const {WebpackManifestPlugin} = require('webpack-manifest-plugin'); +const TerserPlugin = require('terser-webpack-plugin'); const options = {}; @@ -24,7 +25,8 @@ module.exports = { }, mode: 'production', optimization: { - minimize: false + minimize: true, + minimizer: [new TerserPlugin()], }, plugins: [ new CleanWebpackPlugin(options),