Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added blog category plugin

  • Loading branch information...
commit 78ec04446bee386203321b24520580224c53942d 1 parent c48346e
Tobias Lorenz Tyrdall authored
24 cmsplugin_blog_categories/cms_plugins.py
... ... @@ -0,0 +1,24 @@
  1 +"""CMS Plugins for the ``cmsplugin_blog_categories`` app."""
  2 +from django.utils.translation import ugettext as _
  3 +
  4 +from cms.plugin_base import CMSPluginBase
  5 +from cms.plugin_pool import plugin_pool
  6 +
  7 +from cmsplugin_blog_categories.models import CategoryPlugin, EntryCategory
  8 +
  9 +
  10 +class CMSCategoryPlugin(CMSPluginBase):
  11 + model = CategoryPlugin
  12 + name = _('Blog Category')
  13 + render_template = 'cmsplugin_blog_categories/category_plugin.html'
  14 +
  15 + def render(self, context, instance, placeholder):
  16 + entries = [category.entry for category in instance.category.entry_categories.all()]
  17 + context.update({
  18 + 'category': instance.category,
  19 + 'category_entries': entries,
  20 + 'placeholder': placeholder,
  21 + })
  22 + return context
  23 +
  24 +plugin_pool.register_plugin(CMSCategoryPlugin)
7 cmsplugin_blog_categories/models.py
@@ -3,6 +3,7 @@
3 3 from django.utils.translation import get_language
4 4 from django.utils.translation import ugettext_lazy as _
5 5
  6 +from cms.models import CMSPlugin
6 7 from simple_translation.utils import get_translation_queryset
7 8
8 9
@@ -50,7 +51,11 @@ class EntryCategory(models.Model):
50 51 'cmsplugin_blog.Entry',
51 52 related_name='categories',
52 53 )
53   - category = models.ForeignKey(Category)
  54 + category = models.ForeignKey(Category, related_name='entry_categories')
54 55
55 56 class Meta:
56 57 unique_together = ('entry', 'category', )
  58 +
  59 +
  60 +class CategoryPlugin(CMSPlugin):
  61 + category = models.ForeignKey(Category)
8 cmsplugin_blog_categories/templates/cmsplugin_blog_categories/category_plugin.html
... ... @@ -0,0 +1,8 @@
  1 +{% load i18n cmsplugin_blog_tags %}
  2 +{% with category_entries as object_list %}
  3 + {% if object_list %}
  4 + {% include "cmsplugin_blog/entry_list_include.html" %}
  5 + {% else %}
  6 + <p>{% trans "No entries" %}</p>
  7 + {% endif %}
  8 +{% endwith %}
2  cmsplugin_blog_categories/templatetags/cmsplugin_blog_categories_tags.py
@@ -18,7 +18,7 @@ def get_category(entry):
18 18 takes_context=True)
19 19 def render_category_links(context, exclude_empty=False):
20 20 """Renders a list of all categories in the database."""
21   - qs = Category.objects.annotate(num_posts=Count('entrycategory'))
  21 + qs = Category.objects.annotate(num_posts=Count('entry_categories'))
22 22 if exclude_empty:
23 23 qs = qs.exclude(num_posts=0)
24 24 context.update({'categories': qs, })
2  cmsplugin_blog_categories/tests/test_app/templates/cms/home.html
... ... @@ -1,5 +1,7 @@
1 1 {% extends "cms/base.html" %}
  2 +{% load cms_tags %}
2 3
3 4 {% block main %}
4 5 Home
  6 +{% placeholder 'home' %}
5 7 {% endblock %}
3  manage.py
@@ -3,7 +3,8 @@
3 3 import sys
4 4
5 5 if __name__ == "__main__":
6   - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cmsplugin_blog_categories.tests.south_settings")
  6 + os.environ.setdefault('DJANGO_SETTINGS_MODULE',
  7 + 'cmsplugin_blog_categories.tests.south_settings')
7 8
8 9 from django.core.management import execute_from_command_line
9 10

0 comments on commit 78ec044

Please sign in to comment.
Something went wrong with that request. Please try again.