Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django cms integration #1

Merged
merged 13 commits into from
Dec 13, 2012
25 changes: 24 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ Usage

* `Middleware`_ (to give django-terms a try or for development)
* `Template filter`_ (for production)
* `cms_plugin_processor`_ (if you are using django-CMS)

The added terms should now be automatically linked to their definitions.

For django-CMS users : display all terms and their definitions with the `TermsIndexPlugin`_.


Middleware
----------
Expand Down Expand Up @@ -140,6 +143,26 @@ Example:
TERMS_ADDITIONAL_IGNORED_CLASSES = ['code-snippet']


cms_plugin_processor
--------------------

A cms_plugin_processor is available if you are using django-CMS.
It will parse all plugin output.

Add this in `settings.py`::

CMS_PLUGIN_PROCESSORS = (
[...]
'terms.cms_plugin_processors.TermsProcessor',
[...]
)


TermsIndexPlugin
----------------

If you are using django-CMS, you can display all terms and their definitions with the "Terms Index Plugin".


Settings
========
Expand Down Expand Up @@ -308,7 +331,7 @@ Resolver404
HTMLValidationWarning
.....................

:Raised by: `Middleware`_ and `Template filter`_.
:Raised by: `Middleware`_, `Template filter`_. and `cms_plugin_processor`_
:Raised in: `TERMS_DEBUG`_ mode. Otherwise we try to make terms replacements
work anyway.
:Reason: This happens when django-terms finds a problem in the architecture
Expand Down
22 changes: 22 additions & 0 deletions terms/cms_plugin_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from .html import TermsHTMLReconstructor
from django.template import Context, Template


def TermsProcessor(instance, placeholder, rendered_content, original_context):
"""
This processor mark all terms in all placeholders plugins except termsplugins
"""
if 'terms' in original_context:
return rendered_content

parser = TermsHTMLReconstructor()
parser.feed(rendered_content)

t = Template('{{ content|safe }}')
c = Context({
'content': parser.out,
})
return t.render(c)
19 changes: 19 additions & 0 deletions terms/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cms.models.pluginmodel import CMSPlugin
from django.utils.translation import ugettext_lazy as _
from .models import Term


class TermsIndexPlugin(CMSPluginBase):
model = CMSPlugin
name = _("Terms Index Plugin")
render_template = "term_plugin.html"

def render(self, context, instance, placeholder):
terms = Term.objects.all()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized something: if you select every Term, you will also display those who have no definition but a link instead. So you either need to apply a .exclude(description='') to this QuerySet or handle this (not so) special case in the template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO we should add link in the template, maybe a short version like

<dd>{{ term.definition|safe }} {{ term.url|urlizetrunc:15 }}</dd>

If there is a description, it will be add after. Else, it will be the only content.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. Maybe a newline this time? Only if there is a url, of course. And maybe more letters shown in the urlizetrunc. Something like:

<dd>
  {{ term.definition|safe }}
  {% if term.url %}
    <br/>
    {{ term.url|urlizetrunc:40 }}
  {% endif %}
</dd>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, if definition is empty and url not, the new line is still there.
Perhaps, but more complex :

{{ term.definition|safe }}
{% if term.url and term.definition %}<br/>{%endif %}
{{ term.url|urlizetrunc:40 }}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes. Sorry. Go on, I think this will be the last one before I merge all your changes.

context['terms'] = terms
return context


plugin_pool.register_plugin(TermsIndexPlugin)
8 changes: 8 additions & 0 deletions terms/templates/term_plugin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<dl class="terms-index">
{% for term in terms %}
<dt>{{ term|capfirst }}</dt>
<dd>{{ term.definition|safe }}
{% if term.url and term.definition %}<br/>{%endif %}
{{ term.url|urlizetrunc:40 }}</dd>
{% endfor %}
</dl>