Browse Source

Support project filter with wildcard

Translate glob-style wildcards to ILIKE comparisons for the database.
changes/11/163611/1
Doug Hellmann 4 years ago
parent
commit
80d5fc4ec2
1 changed files with 22 additions and 6 deletions
  1. 22
    6
      aeromancer/project_filter.py

+ 22
- 6
aeromancer/project_filter.py View File

@@ -1,8 +1,10 @@
1 1
 import argparse
2
-
2
+import logging
3 3
 
4 4
 from aeromancer.db.models import Project
5 5
 
6
+LOG = logging.getLogger(__name__)
7
+
6 8
 
7 9
 class ProjectFilter(object):
8 10
     """Manage the arguments for filtering queries by project.
@@ -18,7 +20,8 @@ class ProjectFilter(object):
18 20
             action='append',
19 21
             default=[],
20 22
             dest='projects',
21
-            help='projects to limit search, by exact name',
23
+            help=('projects to limit search, '
24
+                  'by exact name or glob-style patterns'),
22 25
         )
23 26
 
24 27
     @classmethod
@@ -26,11 +29,24 @@ class ProjectFilter(object):
26 29
         return cls(projects=parsed_args.projects)
27 30
 
28 31
     def __init__(self, projects):
32
+        self.exact = []
33
+        self.patterns = []
34
+        for p in projects:
35
+            if '*' in p:
36
+                self.patterns.append(p.replace('*', '%'))
37
+            else:
38
+                self.exact.append(p)
29 39
         self.projects = projects
30 40
 
31 41
     def update_query(self, query):
32
-        if self.projects:
33
-            query = query.filter(
34
-                Project.name.in_(self.projects)
35
-            )
42
+        the_filter = ()
43
+        if self.exact:
44
+            LOG.debug('filtering on projects in %s', self.exact)
45
+            the_filter += (Project.name.in_(self.exact),)
46
+        if self.patterns:
47
+            LOG.debug('filtering on projects matching %s', self.patterns)
48
+            the_filter += tuple(Project.name.ilike(p)
49
+                                for p in self.patterns)
50
+        if the_filter:
51
+            query = query.filter(*the_filter)
36 52
         return query

Loading…
Cancel
Save