1
0
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:
jeremystretch
2022-12-02 12:54:35 -05:00
parent 52cf9086a5
commit db61e57893
3 changed files with 16 additions and 0 deletions

View File

@ -5,6 +5,7 @@
### Enhancements
* [#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

View File

@ -1,3 +1,5 @@
import re
from django import forms
from django.utils.translation import gettext as _
@ -12,6 +14,7 @@ LOOKUP_CHOICES = (
(LookupTypes.EXACT, _('Exact match')),
(LookupTypes.STARTSWITH, _('Starts with')),
(LookupTypes.ENDSWITH, _('Ends with')),
(LookupTypes.REGEX, _('Regex')),
)
@ -43,3 +46,14 @@ class SearchForm(BootstrapMixin, forms.Form):
super().__init__(*args, **kwargs)
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}'
})

View File

@ -18,6 +18,7 @@ class LookupTypes:
EXACT = 'iexact'
STARTSWITH = 'istartswith'
ENDSWITH = 'iendswith'
REGEX = 'iregex'
class SearchIndex: