general: extend |order_by to support sorting on fields (#53506)
This commit is contained in:
parent
9e6bd493a6
commit
bc90b08f5c
|
@ -1522,6 +1522,44 @@ def test_lazy_formdata_queryset_get_from_first(pub, variable_test_data):
|
|||
assert tmpl.render(context) == 'bar'
|
||||
|
||||
|
||||
def test_lazy_formdata_queryset_order_by(pub, variable_test_data):
|
||||
if not pub.is_using_postgresql():
|
||||
pytest.skip('this requires SQL')
|
||||
|
||||
lazy_formdata = variable_test_data
|
||||
data_class = lazy_formdata._formdef.data_class()
|
||||
for i in range(6):
|
||||
formdata = data_class()
|
||||
formdata.data = {
|
||||
'0': 'foo%s' % i,
|
||||
'1': True,
|
||||
'bo1': 'plop1',
|
||||
'10': '3',
|
||||
'3': datetime.date(2019, 7, 2 + i).timetuple(),
|
||||
}
|
||||
formdata.just_created()
|
||||
formdata.store()
|
||||
for i in range(4):
|
||||
formdata = data_class()
|
||||
formdata.data = {
|
||||
'0': 'bar%s' % i,
|
||||
'1': False,
|
||||
'3': datetime.date(2020, 7, 30 - i).timetuple(),
|
||||
'bo1': 'plop2',
|
||||
'10': '4',
|
||||
}
|
||||
formdata.just_created()
|
||||
formdata.jump_status('finished')
|
||||
formdata.store()
|
||||
|
||||
context = pub.substitutions.get_context_variables(mode='lazy')
|
||||
tmpl = Template('{% for v in form_objects|order_by:"foo_foo"|getlist:"foo_foo" %}{{ v }},{% endfor %}')
|
||||
assert tmpl.render(context) == 'bar,bar0,bar1,bar2,bar3,foo0,foo1,foo2,foo3,foo4,foo5,'
|
||||
|
||||
tmpl = Template('{% for v in form_objects|order_by:"datefield"|getlist:"foo_foo" %}{{ v }},{% endfor %}')
|
||||
assert tmpl.render(context) == 'bar,foo0,foo1,foo2,foo3,foo4,foo5,bar3,bar2,bar1,bar0,'
|
||||
|
||||
|
||||
def test_lazy_global_forms(pub):
|
||||
FormDef.wipe()
|
||||
formdef = FormDef()
|
||||
|
|
10
wcs/sql.py
10
wcs/sql.py
|
@ -2300,6 +2300,16 @@ class SqlDataMixin(SqlMixin):
|
|||
cur.close()
|
||||
return all_ids
|
||||
|
||||
@classmethod
|
||||
def get_order_by_clause(cls, order_by):
|
||||
if hasattr(order_by, 'id'):
|
||||
# form field, convert to its column name
|
||||
attribute = order_by
|
||||
order_by = get_field_id(attribute)
|
||||
if attribute.store_display_value:
|
||||
order_by = order_by + '_display'
|
||||
return super().get_order_by_clause(order_by)
|
||||
|
||||
@classmethod
|
||||
@guard_postgres
|
||||
def fix_sequences(cls):
|
||||
|
|
|
@ -68,7 +68,11 @@ class LazyFormDefObjectsManager:
|
|||
)
|
||||
|
||||
def order_by(self, attribute):
|
||||
return self._clone(self._criterias, order_by=attribute)
|
||||
if get_publisher().is_using_postgresql():
|
||||
field = self.get_field(attribute)
|
||||
else:
|
||||
field = None # no support for field search
|
||||
return self._clone(self._criterias, order_by=field or attribute)
|
||||
|
||||
def limit(self, limit):
|
||||
qs = self._clone(self._criterias)
|
||||
|
|
Loading…
Reference in New Issue