fix: ukloni DOCX zaštitu za uređivanje izvještaja
Generirani servisni DOCX preuzimao je document/write zaštitu iz predloška pa je Word datoteka bila neurediva.\n\nDodano je uklanjanje documentProtection/writeProtection/readOnlyRecommended postavki pri kreiranju DOCX dokumenta te regresijski test koji provjerava da zaštita nije prisutna u word/settings.xml.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -268,3 +268,6 @@ class WorkOrderImagesEndpointTests(TestCase):
|
|||||||
archive = zipfile.ZipFile(BytesIO(response.content))
|
archive = zipfile.ZipFile(BytesIO(response.content))
|
||||||
media_files = [name for name in archive.namelist() if name.startswith('word/media/')]
|
media_files = [name for name in archive.namelist() if name.startswith('word/media/')]
|
||||||
self.assertTrue(media_files, archive.namelist())
|
self.assertTrue(media_files, archive.namelist())
|
||||||
|
settings_xml = archive.read('word/settings.xml').decode('utf-8')
|
||||||
|
self.assertNotIn('documentProtection', settings_xml)
|
||||||
|
self.assertNotIn('writeProtection', settings_xml)
|
||||||
|
|||||||
@@ -1607,11 +1607,32 @@ def _create_docx_document(template_name=None):
|
|||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
raise DRFValidationError({"detail": "DOCX generiranje nije dostupno: nedostaje python-docx paket."})
|
raise DRFValidationError({"detail": "DOCX generiranje nije dostupno: nedostaje python-docx paket."})
|
||||||
if not template_name:
|
if not template_name:
|
||||||
return Document()
|
document = Document()
|
||||||
|
_remove_docx_edit_restrictions(document)
|
||||||
|
return document
|
||||||
template_path = DOCX_TEMPLATE_DIR / template_name
|
template_path = DOCX_TEMPLATE_DIR / template_name
|
||||||
if not template_path.exists():
|
if not template_path.exists():
|
||||||
raise DRFValidationError({"detail": f"DOCX template nije pronađen: {template_name}."})
|
raise DRFValidationError({"detail": f"DOCX template nije pronađen: {template_name}."})
|
||||||
return Document(str(template_path))
|
document = Document(str(template_path))
|
||||||
|
_remove_docx_edit_restrictions(document)
|
||||||
|
return document
|
||||||
|
|
||||||
|
|
||||||
|
def _remove_docx_edit_restrictions(document):
|
||||||
|
try:
|
||||||
|
settings_element = document.settings.element
|
||||||
|
except AttributeError:
|
||||||
|
return document
|
||||||
|
|
||||||
|
protected_tags = (
|
||||||
|
'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}documentProtection',
|
||||||
|
'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}writeProtection',
|
||||||
|
'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}readOnlyRecommended',
|
||||||
|
)
|
||||||
|
for tag_name in protected_tags:
|
||||||
|
for element in list(settings_element.findall(tag_name)):
|
||||||
|
settings_element.remove(element)
|
||||||
|
return document
|
||||||
|
|
||||||
|
|
||||||
def _set_docx_cell_text(table, row_index, col_index, value):
|
def _set_docx_cell_text(table, row_index, col_index, value):
|
||||||
|
|||||||
Reference in New Issue
Block a user