Skip to content

Commit

Permalink
Merge pull request #251 from norcalipa/norcalipa/view-caching-3
Browse files Browse the repository at this point in the history
Adding further cache optimizations, gzip compression, and minifying js
  • Loading branch information
norcalipa authored Nov 15, 2024
2 parents 5b0d005 + e644a00 commit 9802d7f
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 5 deletions.
4 changes: 1 addition & 3 deletions crank/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -92,6 +89,7 @@
)

MIDDLEWARE = [
'django.middleware.gzip.GZipMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
Expand Down
2 changes: 1 addition & 1 deletion crank/templatetags/socialapp_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions crank/tests/views/test_admin.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions crank/tests/views/test_fundinground.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# 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


class FundingRoundViewTest(TestCase):
def setUp(self):
cache.clear()
self.factory = RequestFactory()
self.view = FundingRoundChoicesView.as_view()

Expand Down
2 changes: 2 additions & 0 deletions crank/tests/views/test_logout.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# 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
from allauth.socialaccount.models import SocialApp

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')
Expand Down
2 changes: 2 additions & 0 deletions crank/tests/views/test_organization_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
# 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


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(
Expand Down
3 changes: 3 additions & 0 deletions crank/tests/views/test_rtopolicy.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# 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


class RTOPolicyChoicesViewTest(TestCase):
def setUp(self):
cache.clear()
self.factory = RequestFactory()
self.view = RTOPolicyChoicesView.as_view()

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -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 = {};


Expand All @@ -24,7 +25,8 @@ module.exports = {
},
mode: 'production',
optimization: {
minimize: false
minimize: true,
minimizer: [new TerserPlugin()],
},
plugins: [
new CleanWebpackPlugin(options),
Expand Down

0 comments on commit 9802d7f

Please sign in to comment.