From 9de7bda04a59d4836931c7769034e79a02a6c62c Mon Sep 17 00:00:00 2001 From: Laura Beaufort <31420082+lbeaufort@users.noreply.github.com> Date: Mon, 10 Sep 2018 14:10:13 -0400 Subject: [PATCH] Add generic case filters and specific admin fine filters --- webservices/args.py | 23 ++++ webservices/legal_docs/index_management.py | 3 +- webservices/resources/legal.py | 125 ++++++++++----------- 3 files changed, 84 insertions(+), 67 deletions(-) diff --git a/webservices/args.py b/webservices/args.py index 9e8ba93e87..36d53441b2 100644 --- a/webservices/args.py +++ b/webservices/args.py @@ -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 = { diff --git a/webservices/legal_docs/index_management.py b/webservices/legal_docs/index_management.py index a627666030..d08fbc16e8 100644 --- a/webservices/legal_docs/index_management.py +++ b/webservices/legal_docs/index_management.py @@ -188,8 +188,7 @@ "index": "not_analyzed" }, "report_year": { - "type": "string", - "index": "no" + "type": "string" }, "report_type": { "type": "string", diff --git a/webservices/resources/legal.py b/webservices/resources/legal.py index 44f3e0c53c..8f65c084c7 100644 --- a/webservices/resources/legal.py +++ b/webservices/resources/legal.py @@ -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), @@ -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',