Projet

Général

Profil

0001-assets-add-dynamic-asset-slots-to-category-and-form-.patch

Frédéric Péters, 29 juin 2018 15:55

Télécharger (6,28 ko)

Voir les différences:

Subject: [PATCH] assets: add dynamic asset slots to category and form cells
 (#24770)

 combo/apps/assets/views.py | 16 +++++++++++-----
 combo/apps/wcs/models.py   | 21 +++++++++++++++++++++
 combo/data/models.py       |  3 +++
 combo/settings.py          |  5 +++++
 tests/test_wcs.py          | 27 +++++++++++++++++++++++++++
 5 files changed, 67 insertions(+), 5 deletions(-)
combo/apps/assets/views.py
27 27
import ckeditor
28 28
from sorl.thumbnail.shortcuts import get_thumbnail
29 29

  
30
from combo.data.models import CellBase
31

  
30 32
from .forms import AssetUploadForm
31 33
from .models import Asset
32 34

  
......
63 65

  
64 66

  
65 67
class SlotAsset(object):
66
    def __init__(self, key=None, asset=None):
68
    def __init__(self, key=None, name=None, asset=None):
67 69
        self.key = key
68
        self.name = settings.COMBO_ASSET_SLOTS[key]['label']
70
        self.name = name
69 71
        self.asset = asset
70 72

  
71 73
    def is_image(self):
......
84 86

  
85 87
    @classmethod
86 88
    def get_assets(cls):
87
        assets = dict([(x.key, x) for x in Asset.objects.all() if x.key in settings.COMBO_ASSET_SLOTS])
88
        for key, value in settings.COMBO_ASSET_SLOTS.items():
89
            yield cls(key, asset=assets.get(key))
89
        assets = dict([(x.key, x) for x in Asset.objects.all()])
90
        uniq_slots = {}
91
        uniq_slots.update(settings.COMBO_ASSET_SLOTS)
92
        for cell in CellBase.get_cells(cell_filter=lambda x: bool(x.get_asset_slots)):
93
            uniq_slots.update(cell.get_asset_slots())
94
        for key, value in uniq_slots.items():
95
            yield cls(key, name=value.get('label'), asset=assets.get(key))
90 96

  
91 97

  
92 98
class Assets(ListView):
combo/apps/wcs/models.py
1
# -*- coding: utf-8 -*-
2
#
1 3
# combo - content management system
2 4
# Copyright (C) 2014-2015  Entr'ouvert
3 5
#
......
18 20
import logging
19 21

  
20 22
from django import template
23
from django.conf import settings
21 24
from django.db import models
22 25
from django.forms import models as model_forms
23 26
from django.forms import Select
......
98 101
            'text': text,
99 102
            }]
100 103

  
104
    def get_asset_slots(self):
105
        slots = {}
106
        for slot_template_key, slot_template_data in settings.WCS_FORM_ASSET_SLOTS.items():
107
            slots['wcs:form:%s:%s' % (slot_template_key, self.formdef_reference)] = {
108
                    'label': u'%(prefix)s — %(label)s' % {
109
                        'prefix': slot_template_data['prefix'],
110
                        'label': self.cached_title}}
111
        return slots
112

  
101 113

  
102 114
class WcsCommonCategoryCell(CellBase):
103 115
    is_enabled = classmethod(is_wcs_enabled)
......
128 140
            return
129 141
        return self.cached_title
130 142

  
143
    def get_asset_slots(self):
144
        slots = {}
145
        for slot_template_key, slot_template_data in settings.WCS_CATEGORY_ASSET_SLOTS.items():
146
            slots['wcs:category:%s:%s' % (slot_template_key, self.category_reference)] = {
147
                    'label': u'%(prefix)s — %(label)s' % {
148
                        'prefix': slot_template_data['prefix'],
149
                        'label': self.cached_title}}
150
        return slots
151

  
131 152

  
132 153
@register_cell_class
133 154
class WcsCategoryCell(WcsCommonCategoryCell):
combo/data/models.py
437 437
    # get_badge(self, context); set to None so cell types can be skipped easily
438 438
    get_badge = None
439 439

  
440
    # get_asset_slots(self); set to None so cell types can be skipped easily
441
    get_asset_slots = None
442

  
440 443
    # message displayed when the cell is loaded asynchronously
441 444
    loading_message = _('Loading...')
442 445

  
combo/settings.py
307 307
# example: {'banner': {'label': 'Banner image'}}
308 308
COMBO_ASSET_SLOTS = {}
309 309

  
310
# dynamic slots created for wcs category/form cells
311
# example: {'picture': {'prefix': 'Picture'}}
312
WCS_CATEGORY_ASSET_SLOTS = {}
313
WCS_FORM_ASSET_SLOTS = {}
314

  
310 315
# hide work-in-progress/experimental/whatever cells for now
311 316
BOOKING_CALENDAR_CELL_ENABLED = False
312 317
NEWSLETTERS_CELL_ENABLED = False
tests/test_wcs.py
1
# -*- coding: utf-8 -*-
2

  
1 3
import pytest
2 4

  
3 5
import json
......
12 14

  
13 15
from django.conf import settings
14 16
from django.core.cache import cache
17
from django.test import override_settings
15 18
from django.test.client import RequestFactory
16 19

  
17 20
from combo.data.models import Page
......
615 618
    resp.form['cell'] = 'xxxx'
616 619
    resp.form['code'] = 'CNPHNTFB'
617 620
    resp = resp.form.submit(status=400)
621

  
622
@wcsctl_present
623
def test_cell_assets(app, admin_user):
624
    page = Page(title='xxx', slug='test_cell_assets', template_name='standard')
625
    page.save()
626
    cell = WcsFormCell(page=page, placeholder='content', order=0)
627
    cell.formdef_reference = u'default:form-title'
628
    cell.save()
629

  
630
    cell = WcsFormsOfCategoryCell(page=page, placeholder='content', order=0)
631
    cell.category_reference = 'default:test-9'
632
    cell.ordering = 'alpha'
633
    cell.save()
634

  
635
    app = login(app)
636
    resp = app.get('/manage/assets/')
637
    assert 'have any asset yet.' in resp.content
638

  
639
    with override_settings(
640
            WCS_CATEGORY_ASSET_SLOTS={'logo': {'prefix': 'Logo'}},
641
            WCS_FORM_ASSET_SLOTS={'picture': {'prefix': 'Picture'}}):
642
        resp = app.get('/manage/assets/')
643
        assert '>Logo — Test 9<' in resp.content
644
        assert '>Picture — form title<' in resp.content
618
-