Skip to content

Commit

Permalink
Add generic case filters and specific admin fine filters
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeaufort committed Sep 10, 2018
1 parent 4dc1846 commit 9de7bda
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 67 deletions.
23 changes: 23 additions & 0 deletions webservices/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@ def make_seek_args(field=fields.Int, description=None):
description='Filter MURs by earliest date closed'),
'mur_max_close_date': fields.Date(required=False,
description='Filter MURs by latest date closed'),
'case_no': fields.List(IStr, required=False, description='Enforcement matter case number'),
'case_document_category': fields.List(IStr, required=False,
description='Filter cases by category of associated documents'),
'case_respondents': fields.Str(IStr, required=False, description='Filter cases by respondents'),
'case_dispositions': fields.List(IStr, required=False, description='Filter cases by dispositions'),
'case_election_cycles': fields.Int(IStr, required=False, description='Filter cases by election cycles'),
'case_min_open_date': fields.Date(required=False, description='Filter cases by earliest date opened'),
'case_max_open_date': fields.Date(required=False, description='Filter cases by latest date opened'),
'case_min_close_date': fields.Date(required=False,
description='Filter cases by earliest date closed'),
'case_max_close_date': fields.Date(required=False,
description='Filter cases by latest date closed'),
'af_name': fields.List(IStr, required=False, description='Admin fine committee name'),
'af_committee_id': fields.Str(IStr, required=False, description='Admin fine committee ID'),
'af_report_year': fields.Str(IStr, required=False, description='Admin fine report year'),
'af_min_rtb_date': fields.Date(required=False, description='Filter cases by earliest Reason to Believe date'),
'af_max_rtb_date': fields.Date(required=False, description='Filter cases by latest Reason to Believe date'),
'af_rtb_fine_amount': fields.Int(IStr, required=False, description='Filter cases by Reason to Believe fine amount'),
'af_min_fd_date': fields.Date(required=False,
description='Filter cases by earliest Final Determination date'),
'af_max_fd_date': fields.Date(required=False,
description='Filter cases by latest Final Determination date'),
'af_fd_fine_amount': fields.Int(IStr, required=False, description='Filter cases by Final Determination fine amount')
}

candidate_detail = {
Expand Down
3 changes: 1 addition & 2 deletions webservices/legal_docs/index_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@
"index": "not_analyzed"
},
"report_year": {
"type": "string",
"index": "no"
"type": "string"
},
"report_type": {
"type": "string",
Expand Down
125 changes: 60 additions & 65 deletions webservices/resources/legal.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,48 @@ def case_query_builder(q, type_, from_hit, hits_returned, **kwargs):
.index('docs_search') \
.sort("sort1", "sort2")

must_clauses = []
if kwargs.get('case_no'):
must_clauses.append(Q('terms', no=kwargs.get('case_no')))
if kwargs.get('case_document_category'):
must_clauses = [Q('terms', documents__category=kwargs.get('case_document_category'))]

if kwargs.get('case_respondents'):
must_clauses.append(Q('match', respondents=kwargs.get('case_respondents')))
if kwargs.get('case_dispositions'):
must_clauses.append(Q('term', disposition__data__disposition=kwargs.get('case_dispositions')))

if kwargs.get('case_election_cycles'):
must_clauses.append(Q('term', election_cycles=kwargs.get('case_election_cycles')))

# gte/lte: greater than or equal to/less than or equal to
date_range = {}
if kwargs.get('case_min_open_date'):
date_range['gte'] = kwargs.get('case_min_open_date')
if kwargs.get('case_max_open_date'):
date_range['lte'] = kwargs.get('case_max_open_date')
if date_range:
must_clauses.append(Q("range", open_date=date_range))

date_range = {}
if kwargs.get('case_min_close_date'):
date_range['gte'] = kwargs.get('case_min_close_date')
if kwargs.get('case_max_close_date'):
date_range['lte'] = kwargs.get('case_max_close_date')
if date_range:
must_clauses.append(Q("range", close_date=date_range))

query = query.query('bool', must=must_clauses)

# Refactor MURS to use `case_` filters
# once we change the front end to use generic params (issue #)
if type_ == 'murs':
return apply_mur_specific_query_params(query, **kwargs)
elif type_ == 'adrs':
return apply_adr_specific_query_params(query, **kwargs)
elif type_ == 'admin_fines':
if type_ == 'admin_fines':
return apply_af_specific_query_params(query, **kwargs)

return query

def ao_query_builder(q, type_, from_hit, hits_returned, **kwargs):
must_query = [Q('term', _type=type_)]
should_query = [get_ao_document_query(q, **kwargs),
Expand Down Expand Up @@ -215,82 +250,42 @@ def apply_mur_specific_query_params(query, **kwargs):

return query

def apply_adr_specific_query_params(query, **kwargs):
must_clauses = []
if kwargs.get('adr_no'):
must_clauses.append(Q('terms', no=kwargs.get('adr_no')))
if kwargs.get('adr_respondents'):
must_clauses.append(Q('match', respondents=kwargs.get('adr_respondents')))
if kwargs.get('adr_dispositions'):
must_clauses.append(Q('term', disposition__data__disposition=kwargs.get('adr_dispositions')))
if kwargs.get('adr_election_cycles'):
must_clauses.append(Q('term', election_cycles=kwargs.get('adr_election_cycles')))

if kwargs.get('adr_document_category'):
must_clauses = [Q('terms', documents__category=kwargs.get('adr_document_category'))]

#if the query contains min or max open date, add as a range clause ("Q(range)")
#to the set of must_clauses

#gte = greater than or equal to and lte = less than or equal to (see elasticsearch docs)
date_range = {}
if kwargs.get('adr_min_open_date'):
date_range['gte'] = kwargs.get('adr_min_open_date')
if kwargs.get('adr_max_open_date'):
date_range['lte'] = kwargs.get('adr_max_open_date')
if date_range:
must_clauses.append(Q("range", open_date=date_range))

date_range = {}
if kwargs.get('adr_min_close_date'):
date_range['gte'] = kwargs.get('adr_min_close_date')
if kwargs.get('adr_max_close_date'):
date_range['lte'] = kwargs.get('adr_max_close_date')
if date_range:
must_clauses.append(Q("range", close_date=date_range))

query = query.query('bool', must=must_clauses)

return query

def apply_af_specific_query_params(query, **kwargs):
must_clauses = []
if kwargs.get('af_no'):
must_clauses.append(Q('terms', no=kwargs.get('af_no')))
if kwargs.get('af_respondents'):
must_clauses.append(Q('match', respondents=kwargs.get('af_respondents')))
if kwargs.get('af_dispositions'):
must_clauses.append(Q('term', disposition__data__disposition=kwargs.get('af_dispositions')))
if kwargs.get('af_election_cycles'):
must_clauses.append(Q('term', election_cycles=kwargs.get('af_election_cycles')))

if kwargs.get('af_document_category'):
must_clauses = [Q('terms', documents__category=kwargs.get('af_document_category'))]

#if the query contains min or max open date, add as a range clause ("Q(range)")
#to the set of must_clauses
if kwargs.get('af_name'):
must_clauses.append(Q('match', name=' '.join(kwargs.get('af_name'))))
if kwargs.get('af_committee_id'):
must_clauses.append(Q('match', committee_id=kwargs.get('af_committee_id')))
if kwargs.get('af_report_year'):
must_clauses.append(Q('match', report_year=kwargs.get('af_report_year')))

#gte = greater than or equal to and lte = less than or equal to (see elasticsearch docs)
date_range = {}
if kwargs.get('af_min_open_date'):
date_range['gte'] = kwargs.get('af_min_open_date')
if kwargs.get('af_max_open_date'):
date_range['lte'] = kwargs.get('af_max_open_date')
if kwargs.get('af_min_rtb_date'):
date_range['gte'] = kwargs.get('af_min_rtb_date')
if kwargs.get('af_max_rtb_date'):
date_range['lte'] = kwargs.get('af_max_rtb_date')
if date_range:
must_clauses.append(Q("range", open_date=date_range))
must_clauses.append(Q("range", reason_to_believe_action_date=date_range))

date_range = {}
if kwargs.get('af_min_close_date'):
date_range['gte'] = kwargs.get('af_min_close_date')
if kwargs.get('af_max_close_date'):
date_range['lte'] = kwargs.get('af_max_close_date')
if kwargs.get('af_min_fd_date'):
date_range['gte'] = kwargs.get('mur_min_fd_date')
if kwargs.get('af_max_fd_date'):
date_range['lte'] = kwargs.get('mur_max_fd_date')
if date_range:
must_clauses.append(Q("range", close_date=date_range))
must_clauses.append(Q("range", final_determination_date=date_range))

if kwargs.get('af_rtb_fine_amount'):
must_clauses.append(Q('term', reason_to_believe_fine_amount=kwargs.get('af_rtb_fine_amount')))
if kwargs.get('af_fd_fine_amount'):
must_clauses.append(Q('term', final_determination_amount=kwargs.get('af_fd_fine_amount')))

query = query.query('bool', must=must_clauses)

return query


def get_ao_document_query(q, **kwargs):
categories = {'F': 'Final Opinion',
'V': 'Votes',
Expand Down

0 comments on commit 9de7bda

Please sign in to comment.