Permalink
Browse files

added blog category plugin

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

0 comments on commit 78ec044

Please sign in to comment.