Skip to content

Commit

Permalink
add user argument to article.render
Browse files Browse the repository at this point in the history
  • Loading branch information
jenda1 committed Jan 15, 2018
1 parent dc4354b commit 8cc19e5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/wiki/core/markdown/__init__.py
Expand Up @@ -12,12 +12,13 @@

class ArticleMarkdown(markdown.Markdown):

def __init__(self, article, preview=False, *args, **kwargs):
def __init__(self, article, preview=False, user=None, *args, **kwargs):
kwargs.update(settings.MARKDOWN_KWARGS)
kwargs['extensions'] = self.get_markdown_extensions()
markdown.Markdown.__init__(self, *args, **kwargs)
self.article = article
self.preview = preview
self.user = user

def core_extensions(self):
"""List of core extensions found in the mdx folder"""
Expand Down
34 changes: 17 additions & 17 deletions src/wiki/models/article.py
Expand Up @@ -189,36 +189,36 @@ class Meta:
("grant", _("Can assign permissions to other users")),
)

def render(self, preview_content=None):
def render(self, preview_content=None, user=None):
if not self.current_revision:
return ""
if preview_content:
content = preview_content
else:
content = self.current_revision.content
return mark_safe(article_markdown(content, self,
preview=preview_content is not None))
preview=preview_content is not None,
user=user))

def get_cache_key(self):
def get_cache_key(self, user=None):
lang = translation.get_language()
return "wiki:article:{id:d}:{lang:s}".format(
key = "wiki:article:{id:d}:{lang:s}".format(
id=self.current_revision.id if self.current_revision else self.id,
lang=lang
lang=lang,
)

def get_cached_content(self):
"""Returns cached version of rendered article"""
cache_key = self.get_cache_key()
cached_content = cache.get(cache_key)
if cached_content is None:
cached_content = self.render()
cache.set(cache_key, cached_content, settings.CACHE_TIMEOUT)
else:
cached_content = mark_safe(cached_content)
return cached_content
if user:
key = "{}:{user!s}".format(key, user=user)

return key


def clear_cache(self):
cache.delete(self.get_cache_key())
def clear_cache(self, user=None):
"""
Clear rendered content from cache.
If user is None, clear cache for all users.
"""
cache.delete(self.get_cache_key(user))

def get_absolute_url(self):
urlpaths = self.urlpath_set.all()
Expand Down
8 changes: 1 addition & 7 deletions src/wiki/templates/wiki/includes/render.html
Expand Up @@ -5,13 +5,7 @@
{% endaddtoblock %}

<div class="wiki-article">
{% if not preview %}
{% if article.current_revision %}
{{ article.get_cached_content }}
{% endif %}
{% else %}
{{ content|default:"" }}
{% endif %}
{{ content|default:"" }}
</div>

{% for plugin in plugins %}
Expand Down
18 changes: 16 additions & 2 deletions src/wiki/templatetags/wiki_tags.py
Expand Up @@ -6,6 +6,7 @@
from django import template
from django.conf import settings as django_settings
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.db.models import Model
from django.forms import BaseForm
from django.template.defaultfilters import striptags
Expand Down Expand Up @@ -46,14 +47,27 @@ def article_for_object(context, obj):
_cache[obj] = article
return _cache[obj]


@register.inclusion_tag('wiki/includes/render.html', takes_context=True)
def wiki_render(context, article, preview_content=None):
user = context['user'] if 'user' in context else None

if preview_content:
content = article.render(preview_content=preview_content)
else:
content = None
key = str(user) if user else ""
cache_key = article.get_cache_key()
cache_user_key = article.get_cache_key(key)

cache_items = cache.get(cache_key, list())
content = cache.get(cache_user_key) if key in cache_items else None

if content is None:
content = article.render(user=user)
cache_items.append(key)

cache.set(cache_key, cache_items, settings.CACHE_TIMEOUT)
cache.set(cache_user_key, content, settings.CACHE_TIMEOUT)

context.update({
'article': article,
'content': content,
Expand Down

0 comments on commit 8cc19e5

Please sign in to comment.