data: add support for Media class to CellBase (fixes #8293)

You can produce media for a whole page with

  media = sum((cell.media for cell in cells), Media())

The `Media()` is ugly but sum is not able to find itself the base case
for the folding, it defaults to 0 which is not addable to Media.
This commit is contained in:
Benjamin Dauvergne 2015-09-18 12:25:31 +02:00
parent de36956369
commit 52aa9794d0
2 changed files with 22 additions and 0 deletions

View File

@ -26,11 +26,13 @@ from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from django.core import serializers
from django.db import models
from django.db.models.base import ModelBase
from django.db.models import Max
from django.forms import models as model_forms
from django import template
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.forms.widgets import MediaDefiningClass
from ckeditor.fields import RichTextField
import cmsplugin_blurp.utils
@ -210,8 +212,13 @@ class Page(models.Model):
page.save()
class CellMeta(MediaDefiningClass, ModelBase):
pass
class CellBase(models.Model):
__metaclass__ = CellMeta
page = models.ForeignKey(Page)
placeholder = models.CharField(max_length=20)
order = models.PositiveIntegerField()

View File

@ -1,6 +1,7 @@
import pytest
from combo.data.models import Page, CellBase, TextCell, LinkCell
from django.forms.widgets import Media
pytestmark = pytest.mark.django_db
@ -17,6 +18,20 @@ def test_cell_reference():
assert CellBase.get_cell(cell.get_reference()) == cell
def test_media():
class TextCelleWithMedia(TextCell):
class Media:
js = ['coincoin.js']
class Meta:
# to prevent error in Models metaclass as the current module is not
# in a registered applicatoin
app_label = 'data'
cells = [TextCelleWithMedia() for i in range(3)]
assert unicode(sum((cell.media for cell in cells), Media())) == u'<script type="text/javascript" src="/static/coincoin.js"></script>'
def test_additional_label():
page = Page()
page.save()