summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2015-05-28 11:51:57 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2015-05-28 11:54:31 (GMT)
commit94a946fa88ae66f3ca23d37d29af59b819df64a1 (patch)
treeb0da049b1bfd45eb8117a9ba3d41e514dd76166d
parentf21cefe39251d955ee83d545f6eb13dd301809e4 (diff)
downloadcombo-94a946fa88ae66f3ca23d37d29af59b819df64a1.zip
combo-94a946fa88ae66f3ca23d37d29af59b819df64a1.tar.gz
combo-94a946fa88ae66f3ca23d37d29af59b819df64a1.tar.bz2
misc: add a simple link cell type (#7322)
-rw-r--r--combo/data/models.py37
-rw-r--r--combo/public/templates/combo/link-cell.html1
-rw-r--r--combo/utils.py8
-rw-r--r--tests/test_cells.py40
4 files changed, 77 insertions, 9 deletions
diff --git a/combo/data/models.py b/combo/data/models.py
index b1bfc40..699a6ee 100644
--- a/combo/data/models.py
+++ b/combo/data/models.py
@@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
-from HTMLParser import HTMLParser
from django.conf import settings
from django.contrib.auth.models import Group
@@ -25,7 +24,7 @@ from django.core import serializers
from django.db import models
from django.db.models import Max
from django.forms import models as model_forms
-from django.utils.html import strip_tags
+from django import template
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
@@ -34,6 +33,8 @@ import cmsplugin_blurp.utils
from .library import register_cell_class, get_cell_classes, get_cell_class
+from combo import utils
+
def element_is_visible(element, user=None):
if element.public:
@@ -218,6 +219,7 @@ class CellBase(models.Model):
visible = True
user_dependant = False
manager_form_template = 'combo/cell_form.html'
+ template_name = None
class Meta:
abstract = True
@@ -332,6 +334,14 @@ class CellBase(models.Model):
'''Return whether the cell content varies from user to user.'''
return self.user_dependant
+ def get_cell_extra_context(self):
+ return {'cell': self}
+
+ def render(self, context):
+ context.update(self.get_cell_extra_context())
+ tmpl = template.loader.get_template(self.template_name)
+ return tmpl.render(context)
+
@register_cell_class
class TextCell(CellBase):
@@ -346,12 +356,7 @@ class TextCell(CellBase):
def get_additional_label(self):
if not self.text:
return None
- def ellipsize(text):
- if text < 50:
- return text
- return text[:40] + '...'
- h = HTMLParser()
- return ellipsize(h.unescape(strip_tags(self.text)))
+ return utils.ellipsize(self.text)
@register_cell_class
@@ -431,3 +436,19 @@ class MenuCell(CellBase):
def render(self, context):
from combo.public.menu import render_menu
return render_menu(context, level=-1, depth=self.depth)
+
+
+@register_cell_class
+class LinkCell(CellBase):
+ title = models.CharField(_('Title'), max_length=150)
+ url = models.URLField(_('URL'))
+
+ template_name = 'combo/link-cell.html'
+
+ class Meta:
+ verbose_name = _('Link')
+
+ def get_additional_label(self):
+ if not self.title:
+ return None
+ return utils.ellipsize(self.title)
diff --git a/combo/public/templates/combo/link-cell.html b/combo/public/templates/combo/link-cell.html
new file mode 100644
index 0000000..c96b7d8
--- /dev/null
+++ b/combo/public/templates/combo/link-cell.html
@@ -0,0 +1 @@
+<a href="{{cell.url}}">{{cell.title}}</a>
diff --git a/combo/utils.py b/combo/utils.py
index 51f92a7..7193096 100644
--- a/combo/utils.py
+++ b/combo/utils.py
@@ -18,10 +18,12 @@ import datetime
import base64
import hmac
import hashlib
+from HTMLParser import HTMLParser
import urllib
import random
import urlparse
+from django.utils.html import strip_tags
class NothingInCacheException(Exception):
pass
@@ -55,3 +57,9 @@ def sign_string(s, key, algo='sha256', timedelta=30):
digestmod = getattr(hashlib, algo)
hash = hmac.HMAC(key, digestmod=digestmod, msg=s)
return hash.digest()
+
+def ellipsize(text, length=50):
+ text = HTMLParser().unescape(strip_tags(text))
+ if len(text) < length:
+ return text
+ return text[:(length-10)] + '...'
diff --git a/tests/test_cells.py b/tests/test_cells.py
index 6954046..fc52f18 100644
--- a/tests/test_cells.py
+++ b/tests/test_cells.py
@@ -1,6 +1,6 @@
import pytest
-from combo.data.models import Page, CellBase, TextCell
+from combo.data.models import Page, CellBase, TextCell, LinkCell
pytestmark = pytest.mark.django_db
@@ -16,3 +16,41 @@ def test_cell_reference():
cell.save()
assert CellBase.get_cell(cell.get_reference()) == cell
+
+def test_additional_label():
+ page = Page()
+ page.save()
+
+ cell = TextCell()
+ cell.page = page
+ cell.text = '<p>foobar</p>'
+ cell.order = 0
+ cell.save()
+
+ assert cell.get_additional_label() == 'foobar'
+
+ cell = TextCell()
+ cell.page = page
+ cell.text = '<p>%s</p>' % 'foo'*30
+ cell.order = 0
+ cell.save()
+
+ assert len(cell.get_additional_label()) < 100
+ assert '...' in cell.get_additional_label()
+
+def test_link_cell():
+ page = Page()
+ page.save()
+
+ cell = LinkCell()
+ cell.page = page
+ cell.title = 'Example Site'
+ cell.url = 'http://example.net'
+ cell.order = 0
+ cell.save()
+
+ from django.template import Context
+ ctx = Context()
+ assert cell.render(ctx).strip() == '<a href="http://example.net">Example Site</a>'
+
+ assert cell.get_additional_label() == 'Example Site'