From 3d7fcfd8ee872360229d4fc8f7230a82450640cb Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Wed, 6 Mar 2024 15:39:40 +0100
Subject: [PATCH] lazy-load lxml only when needed

---
 scipost_django/journals/forms.py | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/scipost_django/journals/forms.py b/scipost_django/journals/forms.py
index 2595bb96f..74ebb7acf 100644
--- a/scipost_django/journals/forms.py
+++ b/scipost_django/journals/forms.py
@@ -243,8 +243,17 @@ class AuthorsTableOrganizationSelectForm(forms.ModelForm):
 
 
 class CreateMetadataXMLForm(forms.ModelForm):
-    schema = ET.XMLSchema(file=settings.STATIC_ROOT + settings.CROSSREF_SCHEMA_FILE)
-    parser = ET.XMLParser(schema=schema)
+    schema = None
+    parser = None
+
+    @classmethod
+    def initialize_lxml(cls):
+        if cls.schema is None:
+            cls.schema = ET.XMLSchema(
+                file=settings.STATIC_ROOT + settings.CROSSREF_SCHEMA_FILE
+            )
+        if cls.parser is None:
+            cls.parser = ET.XMLParser(schema=cls.schema)
 
     class Meta:
         model = Publication
@@ -255,6 +264,9 @@ class CreateMetadataXMLForm(forms.ModelForm):
         self.xml_str = self.format_xml(self.decode_html_entities(xml))
         kwargs["initial"] = {"metadata_xml": self.xml_str}
 
+        if self.schema is None or self.parser is None:
+            self.initialize_lxml()
+
         super().__init__(*args, **kwargs)
 
     @staticmethod
-- 
GitLab