From 52aa9794d0f09c511dcb8cf87779fad5e3090d92 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 18 Sep 2015 12:25:31 +0200 Subject: [PATCH 1/2] 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. --- combo/data/models.py | 7 +++++++ tests/test_cells.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/combo/data/models.py b/combo/data/models.py index cb05cda..5637b97 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -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() diff --git a/tests/test_cells.py b/tests/test_cells.py index 8a58592..7695b84 100644 --- a/tests/test_cells.py +++ b/tests/test_cells.py @@ -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'' + + def test_additional_label(): page = Page() page.save() -- 2.1.4