From 3d7b18b590fe9336c06439007581a2b34ebc3d3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 25 Jun 2018 20:23:59 +0200 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(-) diff --git a/combo/apps/assets/views.py b/combo/apps/assets/views.py index 4e81f45..f84d929 100644 --- a/combo/apps/assets/views.py +++ b/combo/apps/assets/views.py @@ -27,6 +27,8 @@ from django.views.generic import TemplateView, ListView, FormView import ckeditor from sorl.thumbnail.shortcuts import get_thumbnail +from combo.data.models import CellBase + from .forms import AssetUploadForm from .models import Asset @@ -63,9 +65,9 @@ class CkEditorAsset(object): class SlotAsset(object): - def __init__(self, key=None, asset=None): + def __init__(self, key=None, name=None, asset=None): self.key = key - self.name = settings.COMBO_ASSET_SLOTS[key]['label'] + self.name = name self.asset = asset def is_image(self): @@ -84,9 +86,13 @@ class SlotAsset(object): @classmethod def get_assets(cls): - assets = dict([(x.key, x) for x in Asset.objects.all() if x.key in settings.COMBO_ASSET_SLOTS]) - for key, value in settings.COMBO_ASSET_SLOTS.items(): - yield cls(key, asset=assets.get(key)) + assets = dict([(x.key, x) for x in Asset.objects.all()]) + uniq_slots = {} + uniq_slots.update(settings.COMBO_ASSET_SLOTS) + for cell in CellBase.get_cells(cell_filter=lambda x: bool(x.get_asset_slots)): + uniq_slots.update(cell.get_asset_slots()) + for key, value in uniq_slots.items(): + yield cls(key, name=value.get('label'), asset=assets.get(key)) class Assets(ListView): diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py index 11e93d4..06ce1e4 100644 --- a/combo/apps/wcs/models.py +++ b/combo/apps/wcs/models.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- +# # combo - content management system # Copyright (C) 2014-2015 Entr'ouvert # @@ -18,6 +20,7 @@ import copy import logging from django import template +from django.conf import settings from django.db import models from django.forms import models as model_forms from django.forms import Select @@ -98,6 +101,15 @@ class WcsFormCell(CellBase): 'text': text, }] + def get_asset_slots(self): + slots = {} + for slot_template_key, slot_template_data in settings.WCS_FORM_ASSET_SLOTS.items(): + slots['wcs:form:%s:%s' % (slot_template_key, self.formdef_reference)] = { + 'label': u'%(prefix)s — %(label)s' % { + 'prefix': slot_template_data['prefix'], + 'label': self.cached_title}} + return slots + class WcsCommonCategoryCell(CellBase): is_enabled = classmethod(is_wcs_enabled) @@ -128,6 +140,15 @@ class WcsCommonCategoryCell(CellBase): return return self.cached_title + def get_asset_slots(self): + slots = {} + for slot_template_key, slot_template_data in settings.WCS_CATEGORY_ASSET_SLOTS.items(): + slots['wcs:category:%s:%s' % (slot_template_key, self.category_reference)] = { + 'label': u'%(prefix)s — %(label)s' % { + 'prefix': slot_template_data['prefix'], + 'label': self.cached_title}} + return slots + @register_cell_class class WcsCategoryCell(WcsCommonCategoryCell): diff --git a/combo/data/models.py b/combo/data/models.py index 0f4d3fb..3885ab3 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -437,6 +437,9 @@ class CellBase(models.Model): # get_badge(self, context); set to None so cell types can be skipped easily get_badge = None + # get_asset_slots(self); set to None so cell types can be skipped easily + get_asset_slots = None + # message displayed when the cell is loaded asynchronously loading_message = _('Loading...') diff --git a/combo/settings.py b/combo/settings.py index 0687ec9..ce02b47 100644 --- a/combo/settings.py +++ b/combo/settings.py @@ -307,6 +307,11 @@ COMBO_DEFAULT_NOTIFICATION_DURATION = 3 # example: {'banner': {'label': 'Banner image'}} COMBO_ASSET_SLOTS = {} +# dynamic slots created for wcs category/form cells +# example: {'picture': {'prefix': 'Picture'}} +WCS_CATEGORY_ASSET_SLOTS = {} +WCS_FORM_ASSET_SLOTS = {} + # hide work-in-progress/experimental/whatever cells for now BOOKING_CALENDAR_CELL_ENABLED = False NEWSLETTERS_CELL_ENABLED = False diff --git a/tests/test_wcs.py b/tests/test_wcs.py index a1ed6fd..129e587 100644 --- a/tests/test_wcs.py +++ b/tests/test_wcs.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import pytest import json @@ -12,6 +14,7 @@ import os from django.conf import settings from django.core.cache import cache +from django.test import override_settings from django.test.client import RequestFactory from combo.data.models import Page @@ -615,3 +618,27 @@ def test_tracking_code_cell(app): resp.form['cell'] = 'xxxx' resp.form['code'] = 'CNPHNTFB' resp = resp.form.submit(status=400) + +@wcsctl_present +def test_cell_assets(app, admin_user): + page = Page(title='xxx', slug='test_cell_assets', template_name='standard') + page.save() + cell = WcsFormCell(page=page, placeholder='content', order=0) + cell.formdef_reference = u'default:form-title' + cell.save() + + cell = WcsFormsOfCategoryCell(page=page, placeholder='content', order=0) + cell.category_reference = 'default:test-9' + cell.ordering = 'alpha' + cell.save() + + app = login(app) + resp = app.get('/manage/assets/') + assert 'have any asset yet.' in resp.content + + with override_settings( + WCS_CATEGORY_ASSET_SLOTS={'logo': {'prefix': 'Logo'}}, + WCS_FORM_ASSET_SLOTS={'picture': {'prefix': 'Picture'}}): + resp = app.get('/manage/assets/') + assert '>Logo — Test 9<' in resp.content + assert '>Picture — form title<' in resp.content -- 2.18.0