misc: add |filter_by_user and |current_user for querysets (#46855)
This commit is contained in:
parent
fb1908b2ab
commit
d8cd485379
|
@ -944,6 +944,8 @@ def test_lazy_formdata_queryset_distance(pub, variable_test_data):
|
|||
|
||||
|
||||
def test_lazy_formdata_queryset_filter(pub, variable_test_data):
|
||||
local_user_id = variable_test_data.user.id
|
||||
|
||||
wf = Workflow.get_default_workflow()
|
||||
wf.id = None
|
||||
wf.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(wf)
|
||||
|
@ -1055,6 +1057,23 @@ def test_lazy_formdata_queryset_filter(pub, variable_test_data):
|
|||
tmpl = Template('{{form_objects|same_user|filter_by:"foo_foo"|filter_value:"foo"|count}}')
|
||||
assert tmpl.render(context) == '4'
|
||||
|
||||
# attach a user to context form to get form_user variable
|
||||
formdata.user_id = local_user_id
|
||||
formdata.store()
|
||||
|
||||
# test |filter_by_user
|
||||
context = pub.substitutions.get_context_variables(mode='lazy')
|
||||
tmpl = Template('{{form_objects|filter_by_user:form_user|filter_by:"foo_foo"|filter_value:"foo"|count}}')
|
||||
assert tmpl.render(context) == '1'
|
||||
assert LazyFormData(formdata).objects.filter_by_user(lazy_formdata.user).filter_by('foo_foo').apply_filter_value('foo').count == 1
|
||||
|
||||
# test |current_user
|
||||
pub.get_request()._user = () # reset cache
|
||||
pub.get_request().session = sessions.BasicSession(id=1)
|
||||
pub.get_request().session.set_user(local_user_id)
|
||||
tmpl = Template('{{form_objects|current_user|filter_by:"foo_foo"|filter_value:"foo"|count}}')
|
||||
assert tmpl.render(context) == '1'
|
||||
|
||||
|
||||
def test_lazy_global_forms(pub, variable_test_data):
|
||||
lazy_formdata = variable_test_data
|
||||
|
|
|
@ -581,6 +581,16 @@ def exclude_self(queryset):
|
|||
return queryset.exclude_self()
|
||||
|
||||
|
||||
@register.filter
|
||||
def current_user(queryset):
|
||||
return queryset.current_user()
|
||||
|
||||
|
||||
@register.filter
|
||||
def filter_by_user(queryset, user):
|
||||
return queryset.filter_by_user(user)
|
||||
|
||||
|
||||
@register.filter
|
||||
def objects(forms_source, slug):
|
||||
if hasattr(slug, 'get_value'):
|
||||
|
|
|
@ -79,6 +79,9 @@ class LazyFormDefObjectsManager(object):
|
|||
user = get_request().user
|
||||
return self._clone(self._criterias + [Equal('user_id', str(user.id) if user else '-1')])
|
||||
|
||||
def filter_by_user(self, user):
|
||||
return self._clone(self._criterias + [Equal('user_id', str(user.id) if user else '-1')])
|
||||
|
||||
def exclude_self(self):
|
||||
assert self._formdata
|
||||
return self._clone(self._criterias + [NotEqual('id', str(self._formdata.id))])
|
||||
|
|
Loading…
Reference in New Issue