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

Commit

Permalink
Merge pull request #3289 from mozilla/bug1178252
Browse files Browse the repository at this point in the history
Fix bug 1178252 - Stop updated the json blob when a wiki document is logically deleted like when it's marked as spam.
  • Loading branch information
groovecoder committed Jun 29, 2015
2 parents 3cc38b4 + 3c63918 commit 6e851d1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
12 changes: 4 additions & 8 deletions kuma/wiki/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,17 +555,13 @@ def render(self, cache_control=None, base_url=None, timeout=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):
"""Attempt to get the document summary from rendered content, with
fallback to raw HTML"""
"""
Attempt to get the document summary from rendered content, with
fallback to raw HTML
"""
if use_rendered and self.rendered_html:
src = self.rendered_html
else:
Expand Down
12 changes: 10 additions & 2 deletions kuma/wiki/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import with_statement

import json
import logging
import os
from datetime import datetime
Expand Down Expand Up @@ -126,15 +127,22 @@ def render_stale_documents(log=None):


@task
def build_json_data_for_document_task(pk, stale):
def build_json_data_for_document(pk, stale):
"""Force-refresh cached JSON data after rendering."""
document = Document.objects.get(pk=pk)
document.get_json_data(stale=stale)

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


@receiver(render_done)
def build_json_data_handler(sender, instance, **kwargs):
build_json_data_for_document_task.delay(instance.pk, stale=False)
if not instance.deleted:
build_json_data_for_document.delay(instance.pk, stale=False)


@task
Expand Down
38 changes: 28 additions & 10 deletions kuma/wiki/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import mock
from nose.tools import eq_, ok_
from nose.plugins.attrib import attr
from nose import SkipTest

from django.conf import settings
from django.core.exceptions import ValidationError
from django.test.utils import override_settings

from constance import config
from waffle.models import Switch
Expand Down Expand Up @@ -891,43 +891,61 @@ def test_get_rendered(self, mock_kumascript_get):

@attr('bug875349')
@override_constance_settings(KUMASCRIPT_TIMEOUT=1.0)
@override_settings(CELERY_ALWAYS_EAGER=True)
@mock.patch('kuma.wiki.kumascript.get')
def test_build_json_on_render(self, mock_kumascript_get):
"""
A document's json field is refreshed on render(), but not on save()
"""
# FIXME
# this was broken when render_done signal was introduced
raise SkipTest("Skip for now")
mock_kumascript_get.return_value = (self.rendered_content, None)

# Initially empty json field should be filled in after render()
eq_(None, self.d1.json)
eq_(self.d1.json, None)
self.d1.render()
# reloading from db to get the updates done in the celery task
self.d1 = Document.objects.get(pk=self.d1.pk)
ok_(self.d1.json is not None)

time.sleep(0.1) # Small clock-tick to age the results.
time.sleep(1.0) # Small clock-tick to age the results.

# Change the doc title, saving does not actually change the json field.
self.d1.title = "New title"
self.d1.save()
ok_(self.d1.title != self.d1.get_json_data()['title'])
self.d1 = Document.objects.get(pk=self.d1.pk)

# However, rendering refreshes the json field.
self.d1.render()
self.d1 = Document.objects.get(pk=self.d1.pk)
eq_(self.d1.title, self.d1.get_json_data()['title'])

# In case we logically delete a document with a changed title
# we don't update the json blob
deleted_title = 'Deleted title'
self.d1.title = deleted_title
self.d1.save()
self.d1.delete()
self.d1.render()
self.d1 = Document.objects.get(pk=self.d1.pk)
ok_(deleted_title != self.d1.get_json_data()['title'])

@mock.patch('kuma.wiki.kumascript.get')
@override_settings(CELERY_ALWAYS_EAGER=True)
def test_get_summary(self, mock_kumascript_get):
"""get_summary() should attempt to use rendered"""
raise SkipTest("Transient failures here, skip for now")

"""
get_summary() should attempt to use rendered
"""
config.KUMASCRIPT_TIMEOUT = 1.0
mock_kumascript_get.return_value = ('<p>summary!</p>', None)

ok_(not self.d1.rendered_html)
result_summary = self.d1.get_summary()
ok_(not mock_kumascript_get.called)
ok_(not self.d1.rendered_html)

self.d1.render()
ok_(self.d1.rendered_html)
ok_(mock_kumascript_get.called)
result_summary = self.d1.get_summary()
eq_("summary!", result_summary)

config.KUMASCRIPT_TIMEOUT = 0.0
Expand Down

0 comments on commit 6e851d1

Please sign in to comment.