Skip to content

Commit

Permalink
Add ADRs and Admin Fines to legal search
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeaufort committed Sep 6, 2018
1 parent 05c68b0 commit 9964dc7
Showing 1 changed file with 94 additions and 4 deletions.
98 changes: 94 additions & 4 deletions webservices/resources/legal.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
}
}

ALL_DOCUMENT_TYPES = ['statutes', 'regulations', 'advisory_opinions', 'murs']
ALL_DOCUMENT_TYPES = [
'statutes',
'regulations',
'advisory_opinions',
'murs',
'adrs',
'admin_fines',
]

class GetLegalCitation(utils.Resource):
@property
Expand Down Expand Up @@ -78,7 +85,9 @@ def get(self, q='', from_hit=0, hits_returned=20, **kwargs):
"statutes": generic_query_builder,
"regulations": generic_query_builder,
"advisory_opinions": ao_query_builder,
"murs": mur_query_builder
"murs": case_query_builder,
"adrs": case_query_builder,
"admin_fines": case_query_builder,
}

if kwargs.get('type', 'all') == 'all':
Expand Down Expand Up @@ -130,7 +139,7 @@ def generic_query_builder(q, type_, from_hit, hits_returned, **kwargs):

return query

def mur_query_builder(q, type_, from_hit, hits_returned, **kwargs):
def case_query_builder(q, type_, from_hit, hits_returned, **kwargs):
must_query = [Q('term', _type=type_)]

if q:
Expand All @@ -145,7 +154,12 @@ def mur_query_builder(q, type_, from_hit, hits_returned, **kwargs):
.index('docs_search') \
.sort("sort1", "sort2")

return apply_mur_specific_query_params(query, **kwargs)
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':
return apply_af_specific_query_params(query, **kwargs)

def ao_query_builder(q, type_, from_hit, hits_returned, **kwargs):
must_query = [Q('term', _type=type_)]
Expand Down Expand Up @@ -201,6 +215,82 @@ 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

#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 date_range:
must_clauses.append(Q("range", open_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 date_range:
must_clauses.append(Q("range", close_date=date_range))

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 9964dc7

Please sign in to comment.