Uncrazy the caching, more.
[wolnelektury.git] / src / sponsors / templatetags / sponsors.py
diff --git a/src/sponsors/templatetags/sponsors.py b/src/sponsors/templatetags/sponsors.py
new file mode 100644 (file)
index 0000000..fb586d0
--- /dev/null
@@ -0,0 +1,25 @@
+# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from django import template
+from django.core.cache import cache
+from django.utils.safestring import mark_safe
+from sponsors.models import SponsorPage
+
+
+register = template.Library()
+
+
+@register.simple_tag
+def sponsor_page(name):
+    key = 'sponsor_page:' + name
+    content = cache.get(key)
+    if content is None:
+        try:
+            page = SponsorPage.objects.get(name=name)
+        except SponsorPage.DoesNotExist:
+            content = ''
+        else:
+            content = page.html
+        cache.set(key, content) 
+    return mark_safe(content)