From 94a946fa88ae66f3ca23d37d29af59b819df64a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 28 May 2015 13:51:57 +0200 Subject: [PATCH] misc: add a simple link cell type (#7322) --- combo/data/models.py | 37 ++++++++++++++----- combo/public/templates/combo/link-cell.html | 1 + combo/utils.py | 8 +++++ tests/test_cells.py | 40 ++++++++++++++++++++- 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 combo/public/templates/combo/link-cell.html diff --git a/combo/data/models.py b/combo/data/models.py index b1bfc40a..699a6eed 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -15,7 +15,6 @@ # along with this program. If not, see . 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 00000000..c96b7d8a --- /dev/null +++ b/combo/public/templates/combo/link-cell.html @@ -0,0 +1 @@ +{{cell.title}} diff --git a/combo/utils.py b/combo/utils.py index 51f92a7b..71930960 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 6954046f..fc52f180 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 = '

foobar

' + cell.order = 0 + cell.save() + + assert cell.get_additional_label() == 'foobar' + + cell = TextCell() + cell.page = page + cell.text = '

%s

' % '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() == 'Example Site' + + assert cell.get_additional_label() == 'Example Site'