Skip to content

Commit

Permalink
Corrected md5 attributes management
Browse files Browse the repository at this point in the history
  • Loading branch information
sgeulette committed Oct 22, 2021
1 parent 722e6e5 commit 4bf8a54
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 26 deletions.
Expand Up @@ -6,10 +6,9 @@


def podtemplate_created(pod_template, event):
set_initial_md5(pod_template, event)
# clean notes, will only update odt_file if any notes cleaned
# do it before set_initial_md5 as md5 could change
clean_notes(pod_template)
set_initial_md5(pod_template, event)
pod_template.add_parent_pod_annotation()


Expand Down
20 changes: 3 additions & 17 deletions src/collective/documentgenerator/migrations/migrate_to_13.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from collective.documentgenerator.content.pod_template import IConfigurablePODTemplate
from collective.documentgenerator.content.pod_template import IPODTemplate
from collective.documentgenerator.utils import clean_notes
from imio.migrator.migrator import Migrator
from plone import api
Expand All @@ -20,23 +20,9 @@ def __init__(self, context):
def _clean_notes(self):
"""Clean notes of existing POD templates."""
logger.info('Cleaning notes for existing POD templates...')
for brain in self.catalog(object_provides=IConfigurablePODTemplate.__identifier__):
for brain in self.catalog(object_provides=IPODTemplate.__identifier__):
pod_template = brain.getObject()
# check if stored md5 is still the original value
md5_original = False
style_md5_original = False
if pod_template.current_md5 == pod_template.initial_md5:
# it is still correct, keep it correct
md5_original = True
if pod_template.current_md5 == pod_template.style_modification_md5:
style_md5_original = True
# clean and update md5 if it was still the original value
was_cleaned = clean_notes(pod_template)
if was_cleaned and md5_original:
pod_template.initial_md5 = pod_template.current_md5
if was_cleaned and style_md5_original:
pod_template.style_modification_md5 = pod_template.current_md5

clean_notes(pod_template)
logger.info('Done.')

def run(self):
Expand Down
8 changes: 3 additions & 5 deletions src/collective/documentgenerator/tests/test_events.py
Expand Up @@ -96,18 +96,16 @@ def test_clean_notes(self):
cleaned_content_xml = self.get_odt_content_xml(pod_template.odt_file.data)
self.assertFalse(dirty_note in cleaned_content_xml)
self.assertTrue(cleaned_note in cleaned_content_xml)
# clean_notes is done before set_initial_md5
# so when creating a new pod_template, initial_md5 is correct
self.assertEqual(pod_template.current_md5, pod_template.initial_md5)
# does not interact with style_modification_md5
self.assertEqual(pod_template.current_md5, pod_template.style_modification_md5)
# same behavior than style modification: template is considered unchanged
self.assertFalse(pod_template.has_been_modified())

# when updating file, new file is cleaned as well
pod_template.odt_file = NamedBlobFile(
data=dirty_data,
contentType='application/vnd.oasis.opendocument.text',
filename=filename,
)
self.assertTrue(pod_template.has_been_modified()) # template is manually changed
notify(ObjectModifiedEvent(pod_template, Attributes(Interface, 'odt_file')))
cleaned_content_xml = self.get_odt_content_xml(pod_template.odt_file.data)
self.assertFalse(dirty_note in cleaned_content_xml)
Expand Down
4 changes: 3 additions & 1 deletion src/collective/documentgenerator/tests/test_utils.py
Expand Up @@ -3,7 +3,9 @@
import os
from os import getenv, rmdir

from zope.interface import Interface
from zope.interface import Invalid
from zope.lifecycleevent import Attributes
from zope.lifecycleevent import modified

import collective.documentgenerator as cdg
Expand Down Expand Up @@ -69,7 +71,7 @@ def path(tmpl):
# don't replace file when not same os file but template is modified
# We change template file
test_template.odt_file.data = mgodt
modified(test_template)
modified(test_template, Attributes(Interface, 'odt_file'))
self.assertEqual(test_template.initial_md5, mcodt_md5)
self.assertNotEqual(test_template.style_modification_md5, test_template.current_md5)
self.assertTrue(test_template.has_been_modified())
Expand Down
7 changes: 6 additions & 1 deletion src/collective/documentgenerator/utils.py
Expand Up @@ -9,7 +9,9 @@
from Products.CMFPlone.utils import safe_unicode
from zope import i18n
from zope.component import getMultiAdapter
from zope.interface import Interface
from zope.interface import Invalid
from zope.lifecycleevent import Attributes
from zope.lifecycleevent import modified

import hashlib
Expand Down Expand Up @@ -75,7 +77,7 @@ def update_templates(templates, profile='', force=False):
obj.initial_md5 = new_md5
obj.style_modification_md5 = new_md5
obj.odt_file.data = data
modified(obj)
modified(obj, Attributes(Interface, 'odt_file'))
ret.append((ppath, ospath, 'replaced'))
return ret

Expand Down Expand Up @@ -200,13 +202,16 @@ def clean_notes(pod_template):
cleaned = _appy_clean_notes(path=tmp_file.name)
if cleaned:
was_cleaned = True
manually_modified = pod_template.has_been_modified()
with open(tmp_file.name, 'rb') as res_file:
# update template
result = NamedBlobFile(
data=res_file.read(),
contentType=odt_file.contentType,
filename=pod_template.odt_file.filename)
pod_template.odt_file = result
if not manually_modified:
pod_template.style_modification_md5 = pod_template.current_md5
extras = 'pod_template={0} cleaned_parts={1}'.format(
repr(pod_template), cleaned)
fplog('clean_notes', extras=extras)
Expand Down

0 comments on commit 4bf8a54

Please sign in to comment.