misc: allow picking a custom view on card cell (#58878)

This commit is contained in:
Frédéric Péters 2022-02-10 20:19:53 +01:00
parent 9d7cf579ce
commit 4214a6c324
3 changed files with 59 additions and 2 deletions

View File

@ -96,7 +96,7 @@ class WcsCardInfoCellForm(forms.ModelForm):
initial = kwargs.pop('initial', {})
initial['with_user'] = not instance.without_user
super().__init__(initial=initial, *args, **kwargs)
card_models = get_wcs_options('/api/cards/@list')
card_models = get_wcs_options('/api/cards/@list', include_custom_views=True)
self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
if self.instance.custom_schema:
self.initial['customize_display'] = True

View File

@ -785,6 +785,13 @@ class CardMixin:
def card_slug(self):
return self.carddef_reference.split(':')[1]
@property
def card_custom_view(self):
try:
return self.carddef_reference.split(':')[2]
except IndexError:
return None
def get_additional_label(self):
return escape(self.custom_title) or self.cached_title or None
@ -949,7 +956,8 @@ class WcsCardInfosCell(CardMixin, CellBase):
def populate_cache():
if self.carddef_reference:
wcs_key, card_slug = self.carddef_reference.split(':')
parts = self.carddef_reference.split(':')
wcs_key, card_slug = parts[:2]
wcs_site = get_wcs_services().get(wcs_key)
card_schema = get_wcs_json(wcs_site, 'api/cards/%s/@schema' % card_slug, log_errors='warn')
@ -997,6 +1005,17 @@ class WcsCardInfosCell(CardMixin, CellBase):
def get_card_data(self, card_slug, card_id, context, synchronous=False):
api_url = '/api/cards/%s/%s/?include-files-content=off' % (card_slug, card_id)
if self.card_custom_view:
api_url = '/api/cards/%s/%s/%s/?include-files-content=off' % (
card_slug,
self.card_custom_view,
card_id,
)
user = self.get_concerned_user(context)
if user:
user_name_id = user.get_name_id()
if user_name_id:
api_url += '&filter-user-uuid=%s' % user_name_id
if not synchronous:
synchronous = bool(context.get('synchronous'))
wcs_site = get_wcs_services().get(self.wcs_site)
@ -1012,6 +1031,10 @@ class WcsCardInfosCell(CardMixin, CellBase):
)
response.raise_for_status()
except RequestException:
if self.card_custom_view:
# if there's a custom view consider the error is a 404 because
# the card was not found in that view, and mark it so.
context['card_not_found'] = True
return {}
if response.status_code == 200:

View File

@ -419,6 +419,12 @@ def get_data_from_url(url):
return [d for d in WCS_CARDS_DATA[m_card.group(1)] if d['id'] == int(m_card.group(2))][0]
except IndexError:
return {}
m_card = re.match(r'/api/cards/([a-z0-9_]+)/([a-z0-9_]+)/(\d+)/', url) # model/custom-view/id
if m_card:
try:
return [d for d in WCS_CARDS_DATA[m_card.group(1)] if d['id'] == int(m_card.group(3))][0]
except IndexError:
return {}
if 'api/cards/card_model_1/list/foo' in url:
return WCS_CARDS_CUSTOM_VIEW_DATA
m_list = re.match(r'/api/cards/([a-z0-9_]+)/list', url)
@ -1881,6 +1887,7 @@ def test_card_cell_setup(mock_send, app, admin_user):
form = form_class(instance=cell)
assert form.fields['carddef_reference'].widget.choices == [
('default:card_model_1', 'test : Card Model 1'),
('default:card_model_1:foo', 'test : Card Model 1 - bar'),
('default:card_model_2', 'test : Card Model 2'),
('default:card_model_3', 'test : Card Model 3'),
('default:card_a', 'test : Card A'),
@ -1889,6 +1896,7 @@ def test_card_cell_setup(mock_send, app, admin_user):
('default:card_d', 'test : Card D'),
('default:card_e', 'test : Card E'),
('other:card_model_1', 'test2 : Card Model 1'),
('other:card_model_1:foo', 'test2 : Card Model 1 - bar'),
('other:card_model_2', 'test2 : Card Model 2'),
('other:card_model_3', 'test2 : Card Model 3'),
('other:card_a', 'test2 : Card A'),
@ -1912,6 +1920,17 @@ def test_card_cell_setup(mock_send, app, admin_user):
assert 'customize_display' not in form.initial
assert form.initial['custom_schema'] == {}
cell.carddef_reference = 'default:card_model_1:foo'
cell.save()
form = form_class(instance=cell)
assert 'customize_display' in form.fields
assert 'custom_schema' in form.fields
assert 'customize_display' not in form.initial
assert form.initial['custom_schema'] == {}
cell.carddef_reference = 'default:card_model_1'
cell.save()
cell.custom_schema = {'cells': [{'varname': 'foo', 'display_mode': 'value'}]}
cell.save()
form = form_class(instance=cell)
@ -2496,6 +2515,21 @@ def test_card_cell_render(mock_send, context, app):
for i in range(0, 3):
assert '<h2>Foo bar X%sY</h2>' % i in resp
# using custom view
cell.carddef_reference = 'default:card_model_1:foo'
cell.save()
result = cell.render(context)
assert 'Foo bar X0Y' in result
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
mock_resp = Response()
mock_resp.status_code = 404
requests_get.return_value = mock_resp
result = cell.render(context)
# nothing, hide cell
assert not result.strip()
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
def test_card_cell_render_text_field(mock_send, context):