Skip to content

Commit

Permalink
ENHANCEMENT: Additional parameters on the CaseList API endpoint to al…
Browse files Browse the repository at this point in the history
…low more granular searching - #257
  • Loading branch information
n3tsurge committed Sep 20, 2022
1 parent 5368c3b commit 1c6bd5f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/api_v2/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,10 @@ def delete(self, uuid, current_user):

case_parser = pager_parser.copy()
case_parser.add_argument('title', location='args', required=False, type=str)
case_parser.add_argument('description__like', location='args', required=False, type=str)
case_parser.add_argument('observables', location='args', required=False, type=str, action='split')
case_parser.add_argument('organization', location='args', required=False, type=str)
case_parser.add_argument('comments__like', location='args', required=False, type=str)
case_parser.add_argument('status', location='args', required=False, type=str)
case_parser.add_argument('close_reason', location='args', required=False, action="split", type=str)
case_parser.add_argument('severity', location='args', required=False, action="split", type=str)
Expand Down Expand Up @@ -931,9 +934,42 @@ def get(self, current_user):
cases = cases.sort('-created_at')

# Apply filters


# If the user is attempting to filter by observables, find all the events that have
# the related observables so we can map them to the cases they belong to
# and use that as a filter on our overall case search
case_uuids = []
if 'observables' in args and args.observables:
events = Event.search()
events = events.filter('exists', field='case')
events = events.filter('nested', path='event_observables', query=Q('terms', event_observables__value__keyword=args.observables))
events = events.source(['case'])
events = [e for e in events.scan()]
if events:
case_uuids = [e.case for e in events]

# If the user is attempting to filter by comments, find all the comments
# with the text they are searching for and use that as a filter on our
# overall case search
if 'comments__like' in args and args.comments__like:
comments = CaseComment.search()
comments = comments.filter('exists', field='case_uuid')
comments = comments.filter('wildcard', message__keyword="*"+args.comments__like+"*")
comments = comments.source(['case_uuid'])
comments = [c for c in comments.scan()]
if comments:
[case_uuids.append(c.case_uuid) for c in comments]

if case_uuids:
cases = cases.filter('terms', uuid=case_uuids)

if 'title' in args and args['title']:
cases = cases.filter('wildcard', title=args['title']+"*")

if 'description__like' in args and args['description__like']:
cases = cases.filter('wildcard', description="*"+args['description__like']+"*")

if 'status' in args and args['status']:
cases = cases.filter('match', status__name=args['status'])

Expand Down

0 comments on commit 1c6bd5f

Please sign in to comment.