misc: give access to attributes of users taken from data source (#55940)

This commit is contained in:
Frédéric Péters 2021-08-24 10:10:13 +02:00
parent 70fc9d0c21
commit 4156847120
3 changed files with 58 additions and 6 deletions

View File

@ -12,6 +12,7 @@ from wcs.blocks import BlockDef
from wcs.carddef import CardDef
from wcs.categories import Category
from wcs.conditions import Condition
from wcs.data_sources import NamedDataSource
from wcs.formdata import Evolution
from wcs.formdef import FormDef
from wcs.qommon import force_str
@ -1007,6 +1008,41 @@ def test_lazy_formdata_live_item(pub):
assert 'form_var_foo_live_var_attr' not in context.get_flat_keys()
def test_lazy_formdata_live_user_item(pub, local_user):
NamedDataSource.wipe()
datasource = NamedDataSource(name='foo')
datasource.data_source = {'type': 'wcs:users'}
datasource.store()
formdef = FormDef()
formdef.name = 'foobar'
formdef.fields = [
fields.ItemField(
id='0',
label='string',
type='item',
varname='foo',
data_source={'type': 'foo'},
)
]
formdef.store()
formdata = formdef.data_class()()
formdata.data = {
'0': str(local_user.id),
}
formdata.data['0_display'] = formdef.fields[0].store_display_value(formdata.data, '0')
formdata.data['0_structured'] = formdef.fields[0].store_structured_value(formdata.data, '0')
pub.substitutions.feed(pub)
pub.substitutions.feed(formdef)
pub.substitutions.feed(formdata)
context = pub.substitutions.get_context_variables(mode='lazy')
assert context['form_var_foo_live_name'] == local_user.name
assert context['form_var_foo_live_email'] == local_user.email
assert 'form_var_foo_live_email' in context.get_flat_keys()
def test_lazy_formdata_queryset(pub, variable_test_data):
lazy_formdata = variable_test_data
data_class = lazy_formdata._formdef.data_class()

View File

@ -1763,8 +1763,11 @@ class MapOptionsMixin:
class ItemFieldMixin:
def get_real_data_source(self):
return data_sources.get_real(self.data_source)
def add_items_fields_admin_form(self, form):
real_data_source = data_sources.get_real(self.data_source)
real_data_source = self.get_real_data_source()
form.add(
RadiobuttonsWidget,
'data_mode',
@ -2135,7 +2138,7 @@ class ItemField(WidgetField, MapOptionsMixin, ItemFieldMixin):
return
def feed_session(self, value, display_value):
real_data_source = data_sources.get_real(self.data_source)
real_data_source = self.get_real_data_source()
if real_data_source and real_data_source.get('type') == 'jsonp':
if not get_session().jsonp_display_values:
get_session().jsonp_display_values = {}

View File

@ -841,14 +841,18 @@ class LazyFieldVarComputed(LazyFieldVarComplex):
class LazyFieldVarStructured(LazyFieldVarComplex):
def inspect_keys(self):
if not self._data.get(self._field.id):
return []
real_data_source = self._field.get_real_data_source()
if real_data_source and real_data_source.get('type', '') == 'wcs:users':
return ['raw', 'live']
structured_value = self._field.get_structured_value(self._data)
if not structured_value:
if not self._data.get(self._field.id):
return []
return ['raw']
keys = ['raw', 'structured']
if self._field.data_source and self._field.data_source.get('type', '').startswith('carddef:'):
if real_data_source and real_data_source.get('type', '').startswith('carddef:'):
try:
self.live
except AttributeError:
@ -875,8 +879,17 @@ class LazyFieldVarStructured(LazyFieldVarComplex):
@property
def live(self):
if not (self._field.data_source and self._field.data_source.get('type', '').startswith('carddef:')):
real_data_source = self._field.get_real_data_source()
if not (
real_data_source
and (
real_data_source.get('type', '').startswith('carddef:')
or real_data_source.get('type', '') == 'wcs:users'
)
):
raise AttributeError('live')
if real_data_source.get('type', '') == 'wcs:users':
return LazyUser(get_publisher().user_class.get(self._data.get(self._field.id)))
request = get_request()
card_id = self._data.get(self._field.id)
if request: