assets: add dynamic asset slots to category and form cells (#24770)
This commit is contained in:
parent
c9b7df83bb
commit
70c5da0d23
|
@ -27,6 +27,8 @@ from django.views.generic import TemplateView, ListView, FormView
|
||||||
import ckeditor
|
import ckeditor
|
||||||
from sorl.thumbnail.shortcuts import get_thumbnail
|
from sorl.thumbnail.shortcuts import get_thumbnail
|
||||||
|
|
||||||
|
from combo.data.models import CellBase
|
||||||
|
|
||||||
from .forms import AssetUploadForm
|
from .forms import AssetUploadForm
|
||||||
from .models import Asset
|
from .models import Asset
|
||||||
|
|
||||||
|
@ -63,9 +65,9 @@ class CkEditorAsset(object):
|
||||||
|
|
||||||
|
|
||||||
class SlotAsset(object):
|
class SlotAsset(object):
|
||||||
def __init__(self, key=None, asset=None):
|
def __init__(self, key=None, name=None, asset=None):
|
||||||
self.key = key
|
self.key = key
|
||||||
self.name = settings.COMBO_ASSET_SLOTS[key]['label']
|
self.name = name
|
||||||
self.asset = asset
|
self.asset = asset
|
||||||
|
|
||||||
def is_image(self):
|
def is_image(self):
|
||||||
|
@ -84,9 +86,13 @@ class SlotAsset(object):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_assets(cls):
|
def get_assets(cls):
|
||||||
assets = dict([(x.key, x) for x in Asset.objects.all() if x.key in settings.COMBO_ASSET_SLOTS])
|
assets = dict([(x.key, x) for x in Asset.objects.all()])
|
||||||
for key, value in settings.COMBO_ASSET_SLOTS.items():
|
uniq_slots = {}
|
||||||
yield cls(key, asset=assets.get(key))
|
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):
|
class Assets(ListView):
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
# combo - content management system
|
# combo - content management system
|
||||||
# Copyright (C) 2014-2015 Entr'ouvert
|
# Copyright (C) 2014-2015 Entr'ouvert
|
||||||
#
|
#
|
||||||
|
@ -18,6 +20,7 @@ import copy
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django import template
|
from django import template
|
||||||
|
from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.forms import models as model_forms
|
from django.forms import models as model_forms
|
||||||
from django.forms import Select
|
from django.forms import Select
|
||||||
|
@ -98,6 +101,15 @@ class WcsFormCell(CellBase):
|
||||||
'text': text,
|
'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):
|
class WcsCommonCategoryCell(CellBase):
|
||||||
is_enabled = classmethod(is_wcs_enabled)
|
is_enabled = classmethod(is_wcs_enabled)
|
||||||
|
@ -128,6 +140,15 @@ class WcsCommonCategoryCell(CellBase):
|
||||||
return
|
return
|
||||||
return self.cached_title
|
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
|
@register_cell_class
|
||||||
class WcsCategoryCell(WcsCommonCategoryCell):
|
class WcsCategoryCell(WcsCommonCategoryCell):
|
||||||
|
|
|
@ -437,6 +437,9 @@ class CellBase(models.Model):
|
||||||
# get_badge(self, context); set to None so cell types can be skipped easily
|
# get_badge(self, context); set to None so cell types can be skipped easily
|
||||||
get_badge = None
|
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
|
# message displayed when the cell is loaded asynchronously
|
||||||
loading_message = _('Loading...')
|
loading_message = _('Loading...')
|
||||||
|
|
||||||
|
|
|
@ -307,6 +307,11 @@ COMBO_DEFAULT_NOTIFICATION_DURATION = 3
|
||||||
# example: {'banner': {'label': 'Banner image'}}
|
# example: {'banner': {'label': 'Banner image'}}
|
||||||
COMBO_ASSET_SLOTS = {}
|
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
|
# hide work-in-progress/experimental/whatever cells for now
|
||||||
BOOKING_CALENDAR_CELL_ENABLED = False
|
BOOKING_CALENDAR_CELL_ENABLED = False
|
||||||
NEWSLETTERS_CELL_ENABLED = False
|
NEWSLETTERS_CELL_ENABLED = False
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
@ -12,6 +14,7 @@ import os
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
|
from django.test import override_settings
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
|
|
||||||
from combo.data.models import Page
|
from combo.data.models import Page
|
||||||
|
@ -615,3 +618,27 @@ def test_tracking_code_cell(app):
|
||||||
resp.form['cell'] = 'xxxx'
|
resp.form['cell'] = 'xxxx'
|
||||||
resp.form['code'] = 'CNPHNTFB'
|
resp.form['code'] = 'CNPHNTFB'
|
||||||
resp = resp.form.submit(status=400)
|
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
|
||||||
|
|
Loading…
Reference in New Issue