소스 검색

Closes #11090: Add regular expression support to global search engine

jeremystretch 3 년 전
부모
커밋
db61e57893
3개의 변경된 파일16개의 추가작업 그리고 0개의 파일을 삭제
  1. 1 0
      docs/release-notes/version-3.4.md
  2. 14 0
      netbox/netbox/forms/__init__.py
  3. 1 0
      netbox/netbox/search/__init__.py

+ 1 - 0
docs/release-notes/version-3.4.md

@@ -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
 

+ 14 - 0
netbox/netbox/forms/__init__.py

@@ -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}'
+                })

+ 1 - 0
netbox/netbox/search/__init__.py

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