wcs: add cards in context and objects filter (#49406)

This commit is contained in:
Lauréline Guérin 2020-12-15 14:46:08 +01:00
parent 45d7824966
commit 0e0042fa22
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
7 changed files with 266 additions and 5 deletions

View File

@ -0,0 +1,116 @@
# combo - content management system
# Copyright (C) 2020 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from requests.exceptions import RequestException
from combo.apps.wcs.utils import get_wcs_services
from combo.utils import requests
def get_default_wcs_service_key():
services = get_wcs_services()
for key, service in services.items():
if not service.get('secondary', False):
# if secondary is not set or not set to True, return this one
return key
return None
class LazyCardDefObjectsManager(object):
def __init__(self, service_key, card_id):
self._service_key = service_key
self._card_id = card_id
self._cached_resultset = None
@property
def count(self):
return len(self)
def _get_results_from_wcs(self):
service = get_wcs_services().get(self._service_key)
if not service:
return []
api_url = 'api/cards/%s/list' % self._card_id
try:
response = requests.get(
api_url,
remote_service=service,
user=None,
without_user=True,
log_errors=False)
response.raise_for_status()
except RequestException:
return []
if response.json().get('err') == 1:
return []
return response.json().get('data') or []
def _populate_cache(self):
if self._cached_resultset is not None:
return
self._cached_resultset = self._get_results_from_wcs()
def __len__(self):
self._populate_cache()
return len(self._cached_resultset)
def __getitem__(self, key):
try:
if not isinstance(key, slice):
int(key)
except ValueError:
raise TypeError
self._populate_cache()
return self._cached_resultset[key]
def __iter__(self):
self._populate_cache()
for data in self._cached_resultset:
yield data
def __nonzero__(self):
return any(self)
class LazyCardDef(object):
def __init__(self, slug):
if ':' in slug:
self.service_key, self.card_id = slug.split(':')[:2]
else:
self.card_id = slug
self.service_key = get_default_wcs_service_key()
@property
def objects(self):
return LazyCardDefObjectsManager(self.service_key, self.card_id)
class Cards(object):
def __getattr__(self, attr):
try:
return LazyCardDef(attr)
except KeyError:
raise AttributeError(attr)
def cards(request):
return {'cards': Cards()}

View File

View File

@ -0,0 +1,25 @@
# combo - content management system
# Copyright (C) 2020 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import template
register = template.Library()
@register.filter
def objects(cards, slug):
return getattr(cards, slug).objects

View File

@ -117,6 +117,7 @@ TEMPLATES = [
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'combo.context_processors.template_vars',
'combo.apps.wcs.context_processors.cards',
],
'builtins': [
'combo.public.templatetags.combo',

View File

@ -10,12 +10,16 @@ LANGUAGE_CODE = 'en-us'
KNOWN_SERVICES = {
'wcs': {
'default': {'title': 'test', 'url': 'http://127.0.0.1:8999/',
'default': {
'title': 'test', 'url': 'http://127.0.0.1:8999/',
'secret': 'combo', 'orig': 'combo',
'backoffice-menu-url': 'http://127.0.0.1:8999/backoffice/',},
'other': {'title': 'test2', 'url': 'http://127.0.0.2:8999/',
'backoffice-menu-url': 'http://127.0.0.1:8999/backoffice/',
'secondary': False},
'other': {
'title': 'test2', 'url': 'http://127.0.0.2:8999/',
'secret': 'combo', 'orig': 'combo',
'backoffice-menu-url': 'http://127.0.0.2:8999/backoffice/',},
'backoffice-menu-url': 'http://127.0.0.2:8999/backoffice/',
'secondary': True},
},
'passerelle': {
'default': {'title': 'test', 'url': 'http://example.org',

View File

@ -1,3 +1,4 @@
import copy
import json
import re
@ -47,7 +48,11 @@ def test_services_js_no_services(app, normal_user):
app.get('/__services.js', status=404)
def test_services_js_multiple_wcs(app, normal_user):
def test_services_js_multiple_wcs(settings, app, normal_user):
KNOWN_SERVICES = copy.deepcopy(settings.KNOWN_SERVICES)
del KNOWN_SERVICES['wcs']['default']['secondary']
del KNOWN_SERVICES['wcs']['other']['secondary']
settings.KNOWN_SERVICES = KNOWN_SERVICES
login(app)
resp = app.get('/__services.js', status=200)
assert resp.content_type == 'text/javascript'

View File

@ -0,0 +1,110 @@
# -*- coding: utf-8 -*-
import copy
import json
import mock
import pytest
from django.template import Context, Template
from requests.exceptions import ConnectionError
from requests.models import Response
from combo.apps.wcs.context_processors import Cards
@pytest.fixture
def context():
ctx = Context({
'cards': Cards()
})
return ctx
class MockedRequestResponse(mock.Mock):
status_code = 200
def json(self):
return json.loads(self.content)
def mocked_requests_send(request, **kwargs):
data = [{'id': 1}, {'id': 2}] # fake result
return MockedRequestResponse(content=json.dumps({'data': data}))
def test_context(context):
assert 'cards' in context
@mock.patch('combo.apps.wcs.models.requests.send', side_effect=mocked_requests_send)
def test_objects(mock_send, settings, context, nocache):
# lazy filters
t = Template('{% load wcs %}{{ cards|objects:"foo" }}')
assert t.render(context).startswith('&lt;combo.apps.wcs.context_processors.LazyCardDefObjectsManager object at')
assert mock_send.call_args_list == [] # lazy
t = Template('{% load wcs %}{{ cards|objects:"default:foo" }}')
assert t.render(context).startswith('&lt;combo.apps.wcs.context_processors.LazyCardDefObjectsManager object at')
assert mock_send.call_args_list == [] # lazy
# test filters evaluation
t = Template('{% load wcs %}{% for card in cards|objects:"foo" %}{{ card.id }} {% endfor %}')
assert t.render(context) == "1 2 "
assert mock_send.call_args_list[0][0][0].url.startswith('http://127.0.0.1:8999/api/cards/foo/list?') # primary service
t = Template('{% load wcs %}{{ cards|objects:"default:foo"|list }}')
t.render(context)
assert mock_send.call_args_list[0][0][0].url.startswith('http://127.0.0.1:8999/api/cards/foo/list?')
mock_send.reset_mock()
t = Template('{% load wcs %}{{ cards|objects:"other:foo"|list }}')
t.render(context)
assert mock_send.call_args_list[0][0][0].url.startswith('http://127.0.0.2:8999/api/cards/foo/list?')
mock_send.reset_mock()
t = Template('{% load wcs %}{{ cards|objects:"unknown:foo"|list }}')
t.render(context)
assert mock_send.call_args_list == [] # unknown, not evaluated
# test card_id with variable
context['foobar'] = 'some-slug'
mock_send.reset_mock()
t = Template('{% load wcs %}{{ cards|objects:foobar|list }}')
t.render(context)
assert mock_send.call_args_list[0][0][0].url.startswith('http://127.0.0.1:8999/api/cards/some-slug/list?')
# test with no secondary param
KNOWN_SERVICES = copy.deepcopy(settings.KNOWN_SERVICES)
KNOWN_SERVICES['wcs'] = {'default': {'url': 'http://127.0.0.3:8999/'}}
settings.KNOWN_SERVICES = KNOWN_SERVICES
mock_send.reset_mock()
t = Template('{% load wcs %}{{ cards|objects:"bar"|list }}')
t.render(context)
assert mock_send.call_args_list[0][0][0].url.startswith('http://127.0.0.3:8999/api/cards/bar/list?')
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
def test_errors(mock_send, context, nocache):
t = Template('{% load wcs %}{{ cards|objects:"foo"|list }}')
with mock.patch('combo.apps.wcs.utils.requests.get') as requests_get:
mock_resp = Response()
mock_resp.status_code = 500
requests_get.return_value = mock_resp
assert t.render(context) == "[]"
with mock.patch('combo.apps.wcs.utils.requests.get') as requests_get:
requests_get.side_effect = ConnectionError()
requests_get.return_value = mock_resp
assert t.render(context) == "[]"
with mock.patch('combo.apps.wcs.utils.requests.get') as requests_get:
mock_resp = Response()
mock_resp.status_code = 404
requests_get.return_value = mock_resp
assert t.render(context) == "[]"
mock_send.side_effect = lambda *a, **k: MockedRequestResponse(content=json.dumps({'err': 1}))
assert t.render(context) == "[]"
mock_send.side_effect = lambda *a, **k: MockedRequestResponse(content=json.dumps({}))
assert t.render(context) == "[]"
mock_send.side_effect = lambda *a, **k: MockedRequestResponse(content=json.dumps({'data': None}))
assert t.render(context) == "[]"