cards: make id lookup work with display id (#52647)
This commit is contained in:
parent
3566e1cddc
commit
2b8a9a6252
|
@ -330,6 +330,44 @@ def test_template_access(pub):
|
||||||
assert tmpl.render(context) == '0'
|
assert tmpl.render(context) == '0'
|
||||||
|
|
||||||
|
|
||||||
|
def test_data_source_access_by_id(pub):
|
||||||
|
CardDef.wipe()
|
||||||
|
carddef = CardDef()
|
||||||
|
carddef.name = 'foo'
|
||||||
|
carddef.fields = [
|
||||||
|
StringField(id='1', label='Test', type='string', varname='foo'),
|
||||||
|
]
|
||||||
|
carddef.digest_template = '{{ form_var_foo }}'
|
||||||
|
carddef.store()
|
||||||
|
carddef.data_class().wipe()
|
||||||
|
|
||||||
|
carddata = carddef.data_class()()
|
||||||
|
carddata.data = {'1': 'hello world'}
|
||||||
|
carddata.just_created()
|
||||||
|
carddata.store()
|
||||||
|
|
||||||
|
carddata2 = carddef.data_class()()
|
||||||
|
carddata2.data = {'1': 'bye'}
|
||||||
|
carddata2.just_created()
|
||||||
|
carddata2.store()
|
||||||
|
|
||||||
|
cards = CardDef.get_data_source_items('carddef:foo', get_by_id=carddata.id)
|
||||||
|
assert len(cards) == 1
|
||||||
|
assert cards[0]['text'] == 'hello world'
|
||||||
|
|
||||||
|
cards = CardDef.get_data_source_items('carddef:foo', get_by_id=carddata2.id)
|
||||||
|
assert len(cards) == 1
|
||||||
|
assert cards[0]['text'] == 'bye'
|
||||||
|
|
||||||
|
cards = CardDef.get_data_source_items('carddef:foo', get_by_id=carddata.get_display_id())
|
||||||
|
assert len(cards) == 1
|
||||||
|
assert cards[0]['text'] == 'hello world'
|
||||||
|
|
||||||
|
cards = CardDef.get_data_source_items('carddef:foo', get_by_id=carddata2.get_display_id())
|
||||||
|
assert len(cards) == 1
|
||||||
|
assert cards[0]['text'] == 'bye'
|
||||||
|
|
||||||
|
|
||||||
def test_data_source_access_invalid_id(pub):
|
def test_data_source_access_invalid_id(pub):
|
||||||
CardDef.wipe()
|
CardDef.wipe()
|
||||||
carddef = CardDef()
|
carddef = CardDef()
|
||||||
|
|
|
@ -4669,6 +4669,7 @@ def test_set_backoffice_field_card_item(two_pubs):
|
||||||
}
|
}
|
||||||
carddata.just_created()
|
carddata.just_created()
|
||||||
carddata.store()
|
carddata.store()
|
||||||
|
latest_carddata = carddata
|
||||||
latest_carddata_id = carddata.id
|
latest_carddata_id = carddata.id
|
||||||
ds = {'type': 'carddef:%s' % carddef.url_name}
|
ds = {'type': 'carddef:%s' % carddef.url_name}
|
||||||
|
|
||||||
|
@ -4713,6 +4714,16 @@ def test_set_backoffice_field_card_item(two_pubs):
|
||||||
assert formdata.data['bo1_display'] == 'baz'
|
assert formdata.data['bo1_display'] == 'baz'
|
||||||
assert formdata.data['bo1_structured']['attr'] == 'attr2'
|
assert formdata.data['bo1_structured']['attr'] == 'attr2'
|
||||||
|
|
||||||
|
# reset, and get by display id value
|
||||||
|
formdata.data = {}
|
||||||
|
formdata.store()
|
||||||
|
item.fields = [{'field_id': 'bo1', 'value': latest_carddata.get_display_id()}]
|
||||||
|
item.perform(formdata)
|
||||||
|
formdata = formdef.data_class().get(formdata.id)
|
||||||
|
assert formdata.data['bo1'] == str(latest_carddata_id)
|
||||||
|
assert formdata.data['bo1_display'] == 'baz'
|
||||||
|
assert formdata.data['bo1_structured']['attr'] == 'attr2'
|
||||||
|
|
||||||
# reset, and get by text value
|
# reset, and get by text value
|
||||||
formdata.data = {}
|
formdata.data = {}
|
||||||
formdata.store()
|
formdata.store()
|
||||||
|
|
|
@ -209,11 +209,16 @@ class CardDef(FormDef):
|
||||||
if query:
|
if query:
|
||||||
criterias.append(ILike('digest', query))
|
criterias.append(ILike('digest', query))
|
||||||
if get_by_id:
|
if get_by_id:
|
||||||
if int(get_by_id) >= 2 ** 31:
|
try:
|
||||||
# out of range for postgresql integer type; would raise
|
if int(get_by_id) >= 2 ** 31:
|
||||||
# DataError.
|
# out of range for postgresql integer type; would raise
|
||||||
return []
|
# DataError.
|
||||||
criterias.append(Equal('id', get_by_id))
|
return []
|
||||||
|
except ValueError:
|
||||||
|
# get_by_id not an integer, it could be id_display
|
||||||
|
criterias.append(Equal('id_display', get_by_id))
|
||||||
|
else:
|
||||||
|
criterias.append(Equal('id', get_by_id))
|
||||||
if get_by_text:
|
if get_by_text:
|
||||||
criterias.append(Equal('digest', get_by_text))
|
criterias.append(Equal('digest', get_by_text))
|
||||||
|
|
||||||
|
|
|
@ -690,13 +690,7 @@ class NamedDataSource(XmlStorableObject):
|
||||||
def get_card_structured_value_by_id(self, option_id):
|
def get_card_structured_value_by_id(self, option_id):
|
||||||
from wcs.carddef import CardDef
|
from wcs.carddef import CardDef
|
||||||
|
|
||||||
values = []
|
values = CardDef.get_data_source_items(self.type, get_by_id=option_id)
|
||||||
try:
|
|
||||||
int(option_id)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
values = CardDef.get_data_source_items(self.type, get_by_id=option_id)
|
|
||||||
if not values:
|
if not values:
|
||||||
values = CardDef.get_data_source_items(self.type, get_by_text=option_id)
|
values = CardDef.get_data_source_items(self.type, get_by_text=option_id)
|
||||||
if not values:
|
if not values:
|
||||||
|
|
Loading…
Reference in New Issue