Skip to content

Commit 7c55b22

Browse files
Merge pull request #42 from torchbox/choices-caching
Add caching to choices
2 parents b1cef1c + cc8d107 commit 7c55b22

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

wagtail_jotform/models.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.core.cache import cache
12
from django.db import models
23
from django.forms.widgets import Select
34
from django.shortcuts import render
@@ -12,16 +13,25 @@
1213
from .utils import JotFormAPI
1314

1415

16+
CHOICES_CACHE_KEY = "jot_form_choices"
17+
18+
1519
def jot_form_choices():
16-
jot_form_data = []
17-
if wagtail_jotform_settings.API_URL and wagtail_jotform_settings.API_KEY:
18-
jotform = JotFormAPI()
19-
jotform.fetch_from_api()
20-
data = jotform.get_data()
21-
if "content" in data:
22-
for item in data["content"]:
23-
jot_form_data.append((item["id"], item["title"]))
24-
return jot_form_data
20+
# Use a `None` check to allow empty choices to still be cached
21+
if (form_choices := cache.get(CHOICES_CACHE_KEY)) is None:
22+
23+
form_choices = []
24+
if wagtail_jotform_settings.API_URL and wagtail_jotform_settings.API_KEY:
25+
jotform = JotFormAPI()
26+
jotform.fetch_from_api()
27+
data = jotform.get_data()
28+
if "content" in data:
29+
for item in data["content"]:
30+
form_choices.append((item["id"], item["title"]))
31+
32+
cache.set(CHOICES_CACHE_KEY, form_choices, timeout=300)
33+
34+
return form_choices
2535

2636

2737
class EmbeddedFormPageAdminForm(WagtailAdminPageForm):
@@ -34,10 +44,6 @@ class EmbeddedFormPage(RoutablePageMixin, Page):
3444

3545
base_form_class = EmbeddedFormPageAdminForm
3646

37-
def __init__(self, *args, **kwargs):
38-
super().__init__(*args, **kwargs)
39-
jot_form_choices()
40-
4147
thank_you_template = "wagtail_jotform/thank_you.html"
4248
subpage_types = []
4349

wagtail_jotform/tests/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@
7777
WAGTAILADMIN_BASE_URL = "http://localhost:8000"
7878

7979
STATIC_URL = "/static/"
80+
81+
CACHES = {
82+
"default": {
83+
"BACKEND": "django.core.cache.backends.dummy.DummyCache",
84+
}
85+
}

wagtail_jotform/tests/tests.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -499,15 +499,6 @@ def test_jot_form_choices_with_content(self, mock_api):
499499
self.assertEqual(choices, [("1", "Form 1"), ("2", "Form 2")])
500500
mock_api_instance.fetch_from_api.assert_called_once()
501501

502-
@mock.patch("wagtail_jotform.models.jot_form_choices")
503-
def test_form_choices_called_in_model_init(self, mock_jot_form_choices):
504-
"""Test that jot_form_choices is called when initializing EmbeddedFormPage."""
505-
# Create a new instance to trigger the __init__ method
506-
EmbeddedFormPage(title="Test Form Page")
507-
508-
# Verify that jot_form_choices was called
509-
mock_jot_form_choices.assert_called_once()
510-
511502
def test_thank_you_page_route(self):
512503
"""Test the thank you page route renders correctly."""
513504
response = self.client.get("/embeded-form-page/thank-you/")

0 commit comments

Comments
 (0)