Skip to content

Commit

Permalink
ZO-4683: Check date_last_modified instead of checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
louika committed Feb 16, 2024
1 parent d8052d1 commit 0dc6693
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions core/docs/changelog/ZO-4683.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ZO-4683: Only publish article after referencing tts audio if it is unchanged
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<attribute ns="http://namespaces.zeit.de/CMS/workflow" name="corrected">yes</attribute>
<attribute ns="http://namespaces.zeit.de/CMS/document" name="date_first_released">2022-08-09T06:11:30+00:00</attribute>
<attribute ns="http://namespaces.zeit.de/CMS/document" name="date_last_checkout">2022-08-10T21:35:24.546250+00:00</attribute>
<attribute ns="http://namespaces.zeit.de/CMS/document" name="date_last_modified">2022-08-10T21:35:24.546250+00:00</attribute>
<attribute ns="http://namespaces.zeit.de/CMS/workflow" name="date_last_published">2022-08-10T21:35:23.950553+00:00</attribute>
<attribute ns="http://namespaces.zeit.de/CMS/workflow" name="date_last_published_semantic">2022-08-09T06:11:30+00:00</attribute>
<attribute ns="http://namespaces.zeit.de/CMS/workflow" name="edited">yes</attribute>
Expand Down
18 changes: 10 additions & 8 deletions core/src/zeit/speech/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from zeit.cms.repository.interfaces import IFolder
from zeit.cms.workflow.interfaces import IPublish, IPublishInfo
from zeit.connector.search import SearchVar
from zeit.content.article.interfaces import IArticle, ISpeechbertChecksum
from zeit.content.article.interfaces import IArticle
from zeit.content.audio.audio import AUDIO_SCHEMA_NS, Audio
from zeit.content.audio.interfaces import IAudio, IAudioReferences, ISpeechInfo
from zeit.speech.errors import ChecksumMismatchError
from zeit.speech.errors import AudioReferenceError
import zeit.cms.interfaces
import zeit.cms.repository.folder
import zeit.speech.interfaces
Expand Down Expand Up @@ -94,7 +94,8 @@ def update(self, data: dict):
self._add_audio_reference(speech)

def _add_audio_reference(self, speech: IAudio):
article = self._assert_checksum_matches(speech)
article = self._assert_article_unchanged(speech)

IPublish(speech).publish(background=False)
if speech in IAudioReferences(article).items:
return
Expand All @@ -108,12 +109,13 @@ def _article(self, speech: IAudio) -> IArticle:
zeit.cms.content.interfaces.IUUID(ISpeechInfo(speech).article_uuid), None
)

def _assert_checksum_matches(self, speech: IAudio) -> IArticle:
def _assert_article_unchanged(self, speech: IAudio) -> IArticle:
article = self._article(speech)
article_checksum = ISpeechbertChecksum(article).calculate()
if article_checksum != ISpeechInfo(speech).checksum:
raise ChecksumMismatchError(
'Speechbert checksum mismatch for article %s and speech %s',
last_modified = zeit.cms.workflow.interfaces.IModified(article).date_last_modified
last_published = zeit.cms.workflow.interfaces.IPublishInfo(article).date_last_published
if last_modified > last_published:
raise AudioReferenceError(
'Article %s was modified after publish. Speech %s is not referenced.',
article.uniqueId,
speech.uniqueId,
)
Expand Down
7 changes: 5 additions & 2 deletions core/src/zeit/speech/errors.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class ChecksumMismatchError(Exception):
"""An exception raised when the checksum of the article and the speech do not match."""
class AudioReferenceError(Exception):
"""
An exception raised if the article was updated after publish, to avoid publishing it with
unreviewed changes
"""
28 changes: 17 additions & 11 deletions core/src/zeit/speech/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from zeit.content.audio.interfaces import IAudioReferences, ISpeechInfo
from zeit.content.audio.testing import AudioBuilder
from zeit.speech.connection import Speech
from zeit.speech.errors import ChecksumMismatchError
from zeit.speech.errors import AudioReferenceError
from zeit.speech.testing import TTS_CREATED, TTS_DELETED, FunctionalTestCase
import zeit.cms.checkout.interfaces
import zeit.cms.workflow.mock
Expand Down Expand Up @@ -87,23 +87,29 @@ def test_update_audio_without_touching_the_article(self):
assert zeit.cms.workflow.mock._publish_count[article.uniqueId] == 2
assert zeit.cms.workflow.mock._publish_count[audio.uniqueId] == 2

def test_if_checksum_does_not_match_do_not_add_reference(self):
tts_msg = self.setup_speech_message('checksum', 'cake')
with pytest.raises(ChecksumMismatchError):
self.create_audio(tts_msg)
def test_if_article_changed_do_not_add_reference(self):
IPublish(self.article).publish(background=False)
with checked_out(self.article) as co:
paragraph = co.body.create_item('p')
paragraph.text = 'the article has changed'
with pytest.raises(AudioReferenceError):
self.create_audio(TTS_CREATED)
article = ICMSContent(self.article_uid)
reference = IAudioReferences(article)
assert not reference.items

def test_update_audio_fails_because_checksum_is_not_matching(self):
def test_update_audio_fails_if_article_changed(self):
audio = self.create_audio(TTS_CREATED)
assert zeit.cms.workflow.mock._publish_count[audio.uniqueId] == 1

tts_msg = self.setup_speech_message('checksum', 'cake')
article = ICMSContent(self.article_uid)
reference = IAudioReferences(article)
assert audio in reference.items
with checked_out(self.article) as co:
paragraph = co.body.create_item('p')
paragraph.text = 'the article has changed'
self.repository.connector.search_result = [(self.article.uniqueId)]
with mock.patch('zeit.speech.connection.Speech._find', return_value=audio):
with pytest.raises(ChecksumMismatchError):
Speech().update(tts_msg)
with pytest.raises(AudioReferenceError):
Speech().update(TTS_CREATED)

def test_handle_delete_event(self):
audio = self.create_audio(TTS_CREATED)
Expand Down

0 comments on commit 0dc6693

Please sign in to comment.