@@ -10,3 +10,58 @@ class DefaultConfig(AppConfig):
1010 def ready (self ):
1111 if "sqlite3" in settings .DATABASES ["default" ]["ENGINE" ]: # pragma: no cover
1212 TextField .register_lookup (IContains , lookup_name = "search" )
13+ mitigate_docxtpl_corruption_bug ()
14+
15+
16+ def mitigate_docxtpl_corruption_bug ():
17+ # This is basically monkey-patching this PR:
18+ # https://github.com/python-openxml/python-docx/pull/1436
19+
20+ # Hold my beer!
21+ from docx .opc .constants import RELATIONSHIP_TYPE
22+
23+ if hasattr (RELATIONSHIP_TYPE , "CORE_PROPERTIES_OFFICEDOCUMENT" ): # pragma: no cover
24+ raise Exception (
25+ "The docxtpl mitigation is no longer required, please remove the monkeypatch code"
26+ )
27+
28+ RELATIONSHIP_TYPE .CORE_PROPERTIES_OFFICEDOCUMENT = (
29+ "http://schemas.openxmlformats.org/officedocument/2006/relationships"
30+ "/metadata/core-properties"
31+ )
32+
33+ from docx .opc .package import RT , CorePropertiesPart , OpcPackage , cast
34+
35+ @property
36+ def _core_properties_part (self ) -> CorePropertiesPart :
37+ """|CorePropertiesPart| object related to this package.
38+
39+ Creates a default core properties part if one is not present (not common).
40+ """
41+ try :
42+ return cast (CorePropertiesPart , self .part_related_by (RT .CORE_PROPERTIES ))
43+ except KeyError :
44+ try :
45+ office_document_part = self .part_related_by (
46+ RT .CORE_PROPERTIES_OFFICEDOCUMENT # type: ignore
47+ )
48+ rel = self .relate_to (
49+ office_document_part ,
50+ RT .CORE_PROPERTIES_OFFICEDOCUMENT , # type: ignore
51+ )
52+ self .rels [rel ].reltype = RT .CORE_PROPERTIES
53+ return cast (CorePropertiesPart , office_document_part )
54+ except KeyError :
55+ core_properties_part = CorePropertiesPart .default (self )
56+ self .relate_to (core_properties_part , RT .CORE_PROPERTIES )
57+ return core_properties_part
58+
59+ OpcPackage ._core_properties_part = _core_properties_part
60+
61+ from docx .opc .rel import _Relationship
62+
63+ @_Relationship .reltype .setter
64+ def reltype (self , value : str ):
65+ self ._reltype = value
66+
67+ _Relationship .reltype = reltype
0 commit comments