mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Closes #11090: Add regular expression support to global search engine
This commit is contained in:
@ -5,6 +5,7 @@
|
|||||||
### Enhancements
|
### Enhancements
|
||||||
|
|
||||||
* [#815](https://github.com/netbox-community/netbox/issues/815) - Enable specifying terminations when bulk importing circuits
|
* [#815](https://github.com/netbox-community/netbox/issues/815) - Enable specifying terminations when bulk importing circuits
|
||||||
|
* [#11090](https://github.com/netbox-community/netbox/issues/11090) - Add regular expression support to global search engine
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
@ -12,6 +14,7 @@ LOOKUP_CHOICES = (
|
|||||||
(LookupTypes.EXACT, _('Exact match')),
|
(LookupTypes.EXACT, _('Exact match')),
|
||||||
(LookupTypes.STARTSWITH, _('Starts with')),
|
(LookupTypes.STARTSWITH, _('Starts with')),
|
||||||
(LookupTypes.ENDSWITH, _('Ends with')),
|
(LookupTypes.ENDSWITH, _('Ends with')),
|
||||||
|
(LookupTypes.REGEX, _('Regex')),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -43,3 +46,14 @@ class SearchForm(BootstrapMixin, forms.Form):
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.fields['obj_types'].choices = search_backend.get_object_types()
|
self.fields['obj_types'].choices = search_backend.get_object_types()
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
|
||||||
|
# Validate regular expressions
|
||||||
|
if self.cleaned_data['lookup'] == LookupTypes.REGEX:
|
||||||
|
try:
|
||||||
|
re.compile(self.cleaned_data['q'])
|
||||||
|
except re.error as e:
|
||||||
|
raise forms.ValidationError({
|
||||||
|
'q': f'Invalid regular expression: {e}'
|
||||||
|
})
|
||||||
|
@ -18,6 +18,7 @@ class LookupTypes:
|
|||||||
EXACT = 'iexact'
|
EXACT = 'iexact'
|
||||||
STARTSWITH = 'istartswith'
|
STARTSWITH = 'istartswith'
|
||||||
ENDSWITH = 'iendswith'
|
ENDSWITH = 'iendswith'
|
||||||
|
REGEX = 'iregex'
|
||||||
|
|
||||||
|
|
||||||
class SearchIndex:
|
class SearchIndex:
|
||||||
|
Reference in New Issue
Block a user