40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from django.forms import Form, CharField, DecimalField, TextInput, NumberInput
|
|
|
|
from zoo.models import Entity
|
|
|
|
|
|
class EntitySearchForm(Form):
|
|
def __init__(self, *args, **kwargs):
|
|
self.schema = kwargs.pop('schema')
|
|
super(EntitySearchForm, self).__init__(*args, **kwargs)
|
|
self.fields['limit'] = DecimalField(
|
|
widget=NumberInput(attrs={
|
|
'type': 'range',
|
|
'min': '0',
|
|
'max': '1',
|
|
'step': '0.02',
|
|
}),
|
|
required=False)
|
|
for path, _type in self.schema.paths():
|
|
if _type != 'string':
|
|
continue
|
|
key = '__'.join(path)
|
|
self.fields[key] = CharField(
|
|
max_length=32,
|
|
required=False,
|
|
widget=TextInput(attrs={'autocomplete': 'off'}),
|
|
)
|
|
|
|
def search(self):
|
|
kwargs = {}
|
|
for key in self.fields:
|
|
if key == 'limit':
|
|
continue
|
|
if self.cleaned_data.get(key):
|
|
kwargs[key] = self.cleaned_data[key]
|
|
limit = self.cleaned_data.get('limit') or None
|
|
if kwargs:
|
|
return Entity.objects.content_search(self.schema, limit=limit, **kwargs)
|
|
else:
|
|
return Entity.objects.none()
|