misc: also look for placeholders within cells (#19443)

This commit is contained in:
Frédéric Péters 2017-10-14 22:08:21 +02:00
parent 70acfb2a33
commit 96208f1f40
8 changed files with 68 additions and 19 deletions

View File

@ -39,6 +39,8 @@ class FamilyInfosCell(CellBase):
return is_family_enabled()
def get_cell_extra_context(self, context):
if context.get('placeholder_search_mode'):
return {}
user = self.get_concerned_user(context)
if not user or user.is_anonymous():
return {}

View File

@ -535,6 +535,9 @@ class Items(CellBase):
def get_cell_extra_context(self, context):
ctx = super(Items, self).get_cell_extra_context(context)
if context.get('placeholder_search_mode'):
# don't call webservices when we're just looking for placeholders
return ctx
ctx.update({'title': self.title, 'text': self.text})
items = self.get_invoices(user=context['user'])
items.sort(key=lambda i: i.creation_date, reverse=True)

View File

@ -140,6 +140,9 @@ class WcsBlurpMixin(object):
cache_duration = 5
def get_data(self, context):
if context.get('placeholder_search_mode'):
# don't call webservices when we're just looking for placeholders
return {}
if self.wcs_site:
try:
wcs_sites = {self.wcs_site: get_wcs_services()[self.wcs_site]}

View File

@ -78,11 +78,11 @@ def element_is_visible(element, user=None):
class Placeholder(object):
def __init__(self, key, name=None, internal=False, acquired=False):
def __init__(self, key, name=None, acquired=False, render=True):
self.key = key
self.name = name
self.internal = internal
self.acquired = acquired
self.render = render
class PageManager(models.Manager):
@ -184,7 +184,7 @@ class Page(models.Model):
def get_template_display_name(self):
return settings.COMBO_PUBLIC_TEMPLATES[self.template_name]['name']
def get_placeholders(self, template_name=None):
def get_placeholders(self, traverse_cells=False, template_name=None):
placeholders = []
page_template = settings.COMBO_PUBLIC_TEMPLATES.get(template_name or self.template_name)
@ -201,9 +201,10 @@ class Page(models.Model):
context = RequestContext(request, {
'page': self,
'request': request,
'synchronous': False,
'synchronous': True,
'placeholder_search_mode': True,
'placeholders': placeholders,
'traverse_cells': traverse_cells,
})
tmpl.render(context)
return placeholders
@ -713,7 +714,10 @@ class FeedCell(CellBase):
return bool(self.url) and super(FeedCell, self).is_visible(user=user)
def get_cell_extra_context(self, context):
context = super(FeedCell, self).get_cell_extra_context(context)
extra_context = super(FeedCell, self).get_cell_extra_context(context)
if context.get('placeholder_search_mode'):
# don't call webservices when we're just looking for placeholders
return extra_context
cache_key = hashlib.md5(self.url).hexdigest()
feed_content = cache.get(cache_key)
if not feed_content:
@ -722,15 +726,15 @@ class FeedCell(CellBase):
feed_content = feed_response.content
cache.set(cache_key, feed_content, 600)
if feed_content:
context['feed'] = feedparser.parse(feed_content)
extra_context['feed'] = feedparser.parse(feed_content)
if self.limit:
context['feed']['entries'] = context['feed']['entries'][:self.limit]
extra_context['feed']['entries'] = extra_context['feed']['entries'][:self.limit]
if not self.title:
try:
self.title = context['feed']['feed'].title
self.title = extra_context['feed']['feed'].title
except KeyError:
pass
return context
return extra_context
def render(self, context):
cache_key = hashlib.md5(self.url).hexdigest()
@ -885,6 +889,9 @@ class JsonCellBase(CellBase):
def get_cell_extra_context(self, context, invalidate_cache=False):
extra_context = super(JsonCellBase, self).get_cell_extra_context(context)
if context.get('placeholder_search_mode'):
# don't call webservices when we're just looking for placeholders
return extra_context
if self.varnames and context.get('request'):
for varname in self.varnames:
if varname in context['request'].GET:

View File

@ -210,9 +210,7 @@ class PageView(DetailView):
placeholders = []
combo_template = settings.COMBO_PUBLIC_TEMPLATES.get(template)
for placeholder in self.object.get_placeholders():
if placeholder.internal:
continue
for placeholder in self.object.get_placeholders(traverse_cells=True):
placeholder_dict = {
'key': placeholder.key,
'name': placeholder.name,

View File

@ -1,4 +1,5 @@
{% load combo i18n %}
{% if render %}
{% for cell in cells %}
<div class="cell {{ cell.css_class_names }} {% if cell.slug %}{{cell.slug}}{% endif %} {% if cell|shown_because_admin:request %}shown-because-admin{% endif %}"
data-ajax-cell-url="{{ site_base }}{% url 'combo-public-ajax-page-cell' page_pk=cell.page.id cell_reference=cell.get_reference %}"
@ -10,3 +11,4 @@
{% if render_skeleton %}
{{ skeleton }}
{% endif %}
{% endif %}

View File

@ -39,10 +39,25 @@ def skeleton_text(context, placeholder_name, content=''):
@register.inclusion_tag('combo/placeholder.html', takes_context=True)
def placeholder(context, placeholder_name, **options):
placeholder = Placeholder(key=placeholder_name, **options)
if context.get('placeholder_search_mode'):
context['placeholders'].append(Placeholder(key=placeholder_name, **options))
if placeholder.name:
# only include placeholders with a name
context['placeholders'].append(placeholder)
if not context['traverse_cells']:
return context
context['render'] = True
if not placeholder.render:
context['render'] = False
return context
context['cells'] = [x for x in context.get('page_cells', []) if
page_cells = []
if 'page_cells' in context:
# page cells are not precomputed when rendering a single cell in an
# ajax call
page_cells = context.get('page_cells')
elif not context.get('render_skeleton'):
page_cells = context['page'].get_cells()
context['cells'] = [x for x in page_cells if
x.placeholder == placeholder_name and
(context.get('render_skeleton') or x.is_relevant(context))]
if context.get('render_skeleton') and not context['cells']:

View File

@ -1,5 +1,6 @@
import base64
import os
import re
import StringIO
import urllib
@ -15,7 +16,7 @@ from webtest import TestApp
from webtest import Upload
from combo.wsgi import application
from combo.data.models import Page, CellBase, TextCell, LinkCell, ConfigJsonCell
from combo.data.models import Page, CellBase, TextCell, LinkCell, ConfigJsonCell, JsonCell
from combo.apps.search.models import SearchCell
pytestmark = pytest.mark.django_db
@ -486,7 +487,8 @@ def test_edit_config_json_cell(app, admin_user):
options = [x.text for x in resp.html.find_all('option')]
assert not 'Foobar' in options
with override_settings(JSON_CELL_TYPES={'foobar': {'name': 'Foobar', 'url': 'http://test/'}}):
with override_settings(JSON_CELL_TYPES={'test-config-json-cell': {'name': 'Foobar', 'url': 'http://test/'}},
TEMPLATE_DIRS=['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]):
resp = app.get('/manage/pages/%s/' % page.id)
options = [x.text for x in resp.html.find_all('option')]
assert 'Foobar' in options
@ -497,7 +499,7 @@ def test_edit_config_json_cell(app, admin_user):
cells = CellBase.get_cells(page_id=page.id)
assert len(cells) == 1
assert isinstance(cells[0], ConfigJsonCell)
assert cells[0].key == 'foobar'
assert cells[0].key == 'test-config-json-cell'
resp = app.get('/manage/pages/%s/' % page.id)
assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.body
@ -505,7 +507,7 @@ def test_edit_config_json_cell(app, admin_user):
# make it configurable
with override_settings(JSON_CELL_TYPES=
{'foobar': {
{'test-config-json-cell': {
'name': 'Foobar',
'url': 'http://test/',
'form': [
@ -524,7 +526,8 @@ def test_edit_config_json_cell(app, admin_user):
'label': 'Test no type is string',
'varname': 'test3',
},
]}}):
]}},
TEMPLATE_DIRS=['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]):
resp = app.get('/manage/pages/%s/' % page.id)
assert not 'There are no options for this cell.' in resp.form.text
@ -603,3 +606,19 @@ def test_page_multiple_link_cells(app, admin_user):
func.return_value = []
resp = app.get('/manage/pages/%s/' % page.id)
assert func.call_count == 1
def test_page_cell_placeholder(app, admin_user):
page = Page(title='One', slug='one', template_name='standard')
page.save()
cell = JsonCell(page=page, placeholder='content', order=0)
cell.save()
app = login(app)
resp = app.get('/manage/pages/%s/' % page.id)
assert re.findall('data-placeholder-key="(.*)">', resp.body) == ['content', 'footer']
# check a placeholder within a cell is included
cell.template_string = '{% load combo %}{% placeholder "foobar" name="Foobar" %}'
cell.save()
resp = app.get('/manage/pages/%s/' % page.id)
assert re.findall('data-placeholder-key="(.*)">', resp.body) == ['content', 'foobar', 'footer']