Skip to content

Commit

Permalink
ZO-4683: Add logging for more cases, unify log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
wosc committed Feb 19, 2024
1 parent 0dc6693 commit 0d3f915
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions core/src/zeit/speech/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _create(self, data: dict) -> IAudio:
speech.audio_type = 'tts'
folder = self._get_target_folder(data['uuid'])
folder[data['uuid']] = speech
log.info('Text-to-speech was created for article uuid %s', data['uuid'])
log.info('Created %s for article uuid %s', speech, data['uuid'])
self._update(data, folder[data['uuid']])
return folder[data['uuid']]

Expand All @@ -73,15 +73,15 @@ def _update(self, data: dict, speech: IAudio):
elif audio['type'] == 'PREVIEW_TTS':
ISpeechInfo(co).preview_url = audio_entry['url']
ISemanticChange(co).last_semantic_change = datetime.now(pytz.UTC)
log.info('Text-to-speech %s was updated for article uuid %s', speech.uniqueId, data['uuid'])
log.info('Updated %s for article uuid %s', speech, data['uuid'])

def _find(self, article_uuid: str) -> Optional[IAudio]:
connector = zope.component.getUtility(zeit.connector.interfaces.IConnector)
result = list(connector.search([AUDIO_ID], (AUDIO_ID == str(article_uuid))))
if not result:
return None
content = zeit.cms.interfaces.ICMSContent(result[0][0])
log.debug('Text-to-speech %s found for %s.', content.uniqueId, article_uuid)
log.debug('%s found for article uuid %s.', content, article_uuid)
return content

def update(self, data: dict):
Expand All @@ -98,10 +98,12 @@ def _add_audio_reference(self, speech: IAudio):

IPublish(speech).publish(background=False)
if speech in IAudioReferences(article).items:
log.debug('%s already references %s', article, speech)
return
with checked_out(article, raise_if_error=True) as co:
references = IAudioReferences(co)
references.add(speech)
log.info('Added reference from %s to %s', article, speech)
IPublish(article).publish(background=False)

def _article(self, speech: IAudio) -> IArticle:
Expand All @@ -115,17 +117,17 @@ def _assert_article_unchanged(self, speech: IAudio) -> IArticle:
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,
'%s was modified after publish. Skipped adding reference to %s.',
article,
speech,
)
return article

def _remove_reference_from_article(self, speech: IAudio):
article = self._article(speech)
if not article:
log.warning(
'No article found for Text-to-speech %s. ' 'Maybe it was already deleted?',
'No article found for %s. Maybe it was already deleted?',
speech,
)
return
Expand All @@ -137,12 +139,12 @@ def delete(self, data: dict):
speech = self._find(data['article_uuid'])
if not speech:
log.warning(
'No Text-to-speech found for article uuid %s. '
'No audio object found for article uuid %s. '
'Maybe it was already deleted?' % data['article_uuid'],
)
return
self._remove_reference_from_article(speech)
IPublish(speech).retract(background=False)
unique_id = speech.uniqueId
del speech.__parent__[speech.__name__]
log.info('Text-to-speech %s successfully deleted.', unique_id)
log.info('Deleted %s', unique_id)
4 changes: 2 additions & 2 deletions core/src/zeit/speech/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,5 @@ def test_unable_to_remove_anything_because_article_is_missing(self):
self.repository.connector.search_result = []
with mock.patch('zeit.speech.connection.Speech._find', return_value=audio):
Speech().delete(TTS_DELETED)
assert f'No article found for Text-to-speech {audio}' in self.caplog.text
assert not self.repository.has_key('audio')
assert f'No article found for {audio}' in self.caplog.text
assert 'audio' not in self.repository

0 comments on commit 0d3f915

Please sign in to comment.