misc: add a simple link cell type (#7322)

This commit is contained in:
Frédéric Péters 2015-05-28 13:51:57 +02:00
parent f21cefe392
commit 94a946fa88
4 changed files with 77 additions and 9 deletions

View File

@ -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)

View File

@ -0,0 +1 @@
<a href="{{cell.url}}">{{cell.title}}</a>

View File

@ -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)] + '...'

View File

@ -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'