Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Fix bug 998346: Rebuild source document JSON on translate.
Browse files Browse the repository at this point in the history
This is slightly hacky and ends up duplicating code, but the way the
rendering and JSON building happens currently there seems not to be
another option to force-refresh the parent doc's JSON.
  • Loading branch information
ubernostrum committed Jun 3, 2014
1 parent 0314deb commit 6a78c05
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
7 changes: 7 additions & 0 deletions apps/wiki/models.py
Expand Up @@ -996,6 +996,13 @@ def render(self, cache_control=None, base_url=None, timeout=None):
self.render_expires = None

self.save()

# If we're a translation, rebuild our source doc's JSON so its
# translation list includes our last edit date.
if self.parent is not None:
parent_json = json.dumps(self.parent.build_json_data())
Document.objects.filter(pk=self.parent.pk).update(json=parent_json)

render_done.send(sender=self.__class__, instance=self)

def get_summary(self, strip_markup=True, use_rendered=True):
Expand Down
53 changes: 47 additions & 6 deletions apps/wiki/tests/test_views.py
Expand Up @@ -1641,12 +1641,53 @@ def test_translate_keeps_toc_depth(self):
es_d = Document.objects.get(locale=foreign_locale, slug=foreign_slug)
eq_(r.toc_depth, es_d.current_revision.toc_depth)

# Go to edit the translation, ensure the the slug is correct
response = self.client.get(reverse('wiki.edit_document',
args=[foreign_slug],
locale=foreign_locale))
page = pq(response.content)
eq_(page.find('input[name=slug]')[0].value, foreign_slug)
@override_constance_settings(KUMASCRIPT_TIMEOUT=1.0)
def test_translate_rebuilds_source_json(self):
self.client.login(username='admin', password='testpass')
# Create an English original and a Spanish translation.
en_slug = 'en-doc'
es_locale = 'es'
es_slug = 'es-doc'
en_doc = document(title='EN Doc',
slug=en_slug,
is_localizable=True,
locale=settings.WIKI_DEFAULT_LANGUAGE)
en_doc.save()
en_doc.render()

en_doc = Document.objects.get(locale=settings.WIKI_DEFAULT_LANGUAGE,
slug=en_slug)
old_en_json = json.loads(en_doc.json)

r = revision(document=en_doc)
r.save()
translation_data = new_document_data()
translation_data['title'] = 'ES Doc'
translation_data['slug'] = es_slug
translation_data['content'] = 'This is the content'
translation_data['is_localizable'] = False
translation_data['form'] = 'both'
translate_url = reverse('wiki.document', args=[en_slug],
locale=settings.WIKI_DEFAULT_LANGUAGE)
translate_url += '$translate?tolocale=' + es_locale
response = self.client.post(translate_url, translation_data)
# Sanity to make sure the translate succeeded.
self.assertRedirects(response, reverse('wiki.document',
args=[es_slug],
locale=es_locale))
es_doc = Document.objects.get(locale=es_locale,
slug=es_slug)
es_doc.render()

new_en_json = json.loads(Document.objects.get(pk=en_doc.pk).json)

ok_('translations' in new_en_json)
ok_(translation_data['title'] in [t['title'] for t in \
new_en_json['translations']])
es_translation_json = [t for t in new_en_json['translations'] if \
t['title'] == translation_data['title']][0]
eq_(es_translation_json['last_edit'],
es_doc.current_revision.created.isoformat())

def test_slug_translate(self):
"""Editing a translated doc keeps the correct slug"""
Expand Down

0 comments on commit 6a78c05

Please sign in to comment.