Skip to content

Commit

Permalink
Merge pull request #1371 from chris34/wiki-update-related-metadata
Browse files Browse the repository at this point in the history
Wiki, pages: Update metadata and content of related pages
  • Loading branch information
chris34 authored Dec 21, 2024
2 parents efb6937 + 75065a8 commit c749790
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 53 deletions.
1 change: 1 addition & 0 deletions ChangeLog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Deployment notes

✨ New features
---------------
* Wiki: Update metadata and content of related pages after a edit

🏗 Changes
----------
Expand Down
38 changes: 0 additions & 38 deletions inyoka/wiki/management/commands/regenerate_metadata.py

This file was deleted.

27 changes: 26 additions & 1 deletion inyoka/wiki/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,31 @@ def update_meta(self):
continue
MetaData(page=self, key=key, value=value[:MAX_METADATA]).save()

def update_related_pages(self, update_meta: bool=True) -> None:
"""
Removes the content of page from the cache and its related pages.
It also updates the metadata of all pages. This is f.e. relevant for page
templates that emit tags.
Intended to be run in a celery task.
"""

related_pages = Page.objects.select_related('last_rev__text') \
.filter(metadata__key__in=('X-Link', 'X-Attach'),
metadata__value=self.name)

for p in related_pages:
cache.delete(f'wiki/page/{p.name.lower()}')
p.last_rev.text.remove_value_from_cache()
if update_meta:
p.update_meta()

cache.delete(f'wiki/page/{self.name.lower()}')
self.last_rev.text.remove_value_from_cache()
if update_meta:
self.update_meta()

def save(self, update_meta=True, *args, **kwargs):
"""
This not only saves the page but also a revision that is
Expand Down Expand Up @@ -1275,7 +1300,7 @@ class Revision(models.Model):
`rendered_text`.
user
The user that created this revision. If an anoymous user created
The user that created this revision. If an anonymous user created
the revision this will be `None`.
change_date
Expand Down
18 changes: 4 additions & 14 deletions inyoka/wiki/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,10 @@ def update_page_by_slug():


@shared_task
def update_related_pages(page, update_meta=True):
from inyoka.wiki.models import MetaData, Page
page = Page.objects.get(id=page)
related_pages = set()
values = ('value', 'page__last_rev__text_id')
linked = MetaData.objects.values_list(*values) \
.filter(key__in=('X-Link', 'X-Attach'), value=page.name)
for value, text_id in linked.all():
cache.delete(f'wiki/page/{value.lower()}')
related_pages.add(text_id)
cache.delete(f'wiki/page/{page.name.lower()}')

if update_meta:
page.update_meta()
def update_related_pages(page_id: int, update_meta: bool=True) -> None:
from inyoka.wiki.models import Page
page = Page.objects.get(id=page_id)
page.update_related_pages(update_meta=update_meta)


@shared_task
Expand Down
58 changes: 58 additions & 0 deletions tests/apps/wiki/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,64 @@
BASE_PATH = path.dirname(__file__)


class TestPage(TestCase):

def test_update_related_pages__content_updated(self):
"""
Test, that content of a embedder page will be updated.
"""
template = Page.objects.create('Wiki/Templates/template', 'Foo')
page = Page.objects.create('test1', '[:test1:] content [[Vorlage(template, "Hello World")]]')

self.assertEqual(page.last_rev.text.value_rendered,
'<p><a href="http://wiki.ubuntuusers.local:8080/test1/" class="internal">test1</a> content Foo</p>')

# included `template` was changed
# `page` should have a different content, but it's yet not updated
template.edit('Bar', note='changed content')
self.assertEqual(page.last_rev.text.value_rendered,
'<p><a href="http://wiki.ubuntuusers.local:8080/test1/" class="internal">test1</a> content Foo</p>')

template.update_related_pages()

self.assertEqual(page.last_rev.text.value_rendered,
'<p><a href="http://wiki.ubuntuusers.local:8080/test1/" class="internal">test1</a> content Bar</p>')

def test_update_related_pages__metadata_updated(self):
"""
Test, that metadata will be updated.
"""
template = Page.objects.create('Wiki/Templates/template', '#tag: untested')
page = Page.objects.create('test1', '[:test1:] content [[Vorlage(template, "Hello World")]]')

self.assertEqual(len(page.metadata), 3)
self.assertEqual(page.metadata['X-Link'], ['test1'])
self.assertEqual(page.metadata['X-Attach'], ['Wiki/Templates/template'])
self.assertEqual(page.metadata['tag'], ['untested'])

# included `template` was changed
# `page` should have a different metadata, but it's yet not updated
template.edit('# tag: foo', note='changed tag')
page = Page.objects.get(id=page.id) # defer of page.meta caches the metadata once we accessed it
self.assertEqual(len(page.metadata), 3)
self.assertEqual(page.metadata['tag'], ['untested'])

template.update_related_pages()
page = Page.objects.get(id=page.id) # defer of page.meta caches the metadata once we accessed it
self.assertEqual(len(page.metadata), 3)
self.assertEqual(page.metadata['tag'], ['foo'])

def test_update_related_pages__queries_used(self):
"""
Test, how many database queries are needed.
"""
template = Page.objects.create('Wiki/Templates/template', 'Foo')
Page.objects.create('test1', '[:test1:] content [[Vorlage(template, "Hello World")]]')

with self.assertNumQueries(7):
template.update_related_pages()


class TestPageManager(TestCase):
def test_get_by_name_case_sensitive(self):
"""
Expand Down

0 comments on commit c749790

Please sign in to comment.