1+ from django .core .cache import cache
12from django .db import models
23from django .forms .widgets import Select
34from django .shortcuts import render
1213from .utils import JotFormAPI
1314
1415
16+ CHOICES_CACHE_KEY = "jot_form_choices"
17+
18+
1519def 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
2737class 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
0 commit comments