Projet

Général

Profil

0001-data-add-support-for-Media-class-to-CellBase-fixes-8.patch

Benjamin Dauvergne, 18 septembre 2015 12:47

Télécharger (2,58 ko)

Voir les différences:

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(+)
combo/data/models.py
26 26
from django.core.exceptions import ObjectDoesNotExist
27 27
from django.core import serializers
28 28
from django.db import models
29
from django.db.models.base import ModelBase
29 30
from django.db.models import Max
30 31
from django.forms import models as model_forms
31 32
from django import template
32 33
from django.utils.safestring import mark_safe
33 34
from django.utils.translation import ugettext_lazy as _
35
from django.forms.widgets import MediaDefiningClass
34 36

  
35 37
from ckeditor.fields import RichTextField
36 38
import cmsplugin_blurp.utils
......
210 212
                page.save()
211 213

  
212 214

  
215
class CellMeta(MediaDefiningClass, ModelBase):
216
    pass
217

  
213 218

  
214 219
class CellBase(models.Model):
220
    __metaclass__ = CellMeta
221

  
215 222
    page = models.ForeignKey(Page)
216 223
    placeholder = models.CharField(max_length=20)
217 224
    order = models.PositiveIntegerField()
tests/test_cells.py
1 1
import pytest
2 2

  
3 3
from combo.data.models import Page, CellBase, TextCell, LinkCell
4
from django.forms.widgets import Media
4 5

  
5 6
pytestmark = pytest.mark.django_db
6 7

  
......
17 18

  
18 19
    assert CellBase.get_cell(cell.get_reference()) == cell
19 20

  
21

  
22
def test_media():
23
    class TextCelleWithMedia(TextCell):
24
        class Media:
25
            js = ['coincoin.js']
26

  
27
        class Meta:
28
            # to prevent error in Models metaclass as the current module is not
29
            # in a registered applicatoin
30
            app_label = 'data'
31
    cells = [TextCelleWithMedia() for i in range(3)]
32
    assert unicode(sum((cell.media for cell in cells), Media())) == u'<script type="text/javascript" src="/static/coincoin.js"></script>'
33

  
34

  
20 35
def test_additional_label():
21 36
    page = Page()
22 37
    page.save()
23
-