-
Notifications
You must be signed in to change notification settings - Fork 40
/
views.py
188 lines (152 loc) · 5.89 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import requests
import math
from urllib import parse
from django.shortcuts import render
from django.conf import settings
def search_candidates(query):
"""Searches the data API for candidates matching the query"""
path = os.path.join(settings.FEC_API_VERSION, 'candidates', 'search')
url = parse.urljoin(settings.FEC_API_URL, path)
r = requests.get(url, params={
'q': query, 'sort': '-receipts', 'per_page': 3, 'api_key': settings.FEC_API_KEY_PRIVATE
})
return r.json()
def search_committees(query):
"""Searches the data API for committees matching the query"""
path = os.path.join(settings.FEC_API_VERSION, 'committees')
url = parse.urljoin(settings.FEC_API_URL, path)
r = requests.get(url, params={
'q': query, 'per_page': 3, 'sort': '-receipts', 'api_key': settings.FEC_API_KEY_PRIVATE
})
return r.json()
def prev_offset(limit, next_offset):
"""
Helper function for determining the previous offset, which is used
for paging to previous pages
"""
if (next_offset - limit) >= 0:
return next_offset - limit
else:
return 0
def parse_icon(path):
"""
Parse which icon to show by checking if the URL is at /data/
But because some /data/ pages aren't data tables, screen those out with
the pages list
"""
pages = ['/legal/advisory-opinions/', '/legal/statutes/', '/browse-data/']
if '/data/' in path and not any(p for p in pages if p in path):
return 'data'
else:
return 'page'
def replace_url(url):
"""
Replace the base part of the URL with the canonical URL from settings
Useful for the initial switch over from beta.fec.gov to fec.gov
"""
parsed = parse.urlparse(url)
if parsed.netloc == 'beta.fec.gov':
# Only return a new url if the base is beta.fec.gov
return parse.urljoin(settings.CANONICAL_BASE, parsed.path)
else:
return url
def process_site_results(results, limit=0, offset=0):
"""Organizes the results from Search.gov website into a better format"""
web_results = results['web']
grouped = {
'results': web_results['results'],
'best_bets': {
'results': results['text_best_bets'],
'count': len(results['text_best_bets'])
},
'meta': {
'count': web_results['total'],
'next_offset': web_results['next_offset'],
'prev_offset': prev_offset(limit, int(offset))
}
}
# Parse the icons and replace the URLs
for result in grouped['results']:
result['icon'] = parse_icon(result['url'])
result['url'] = replace_url(result['url'])
return grouped
def search_site(query, limit=0, offset=0):
"""Calls the Search.gov website and then processes the results if successful"""
params = {
'affiliate': 'betafec_api',
'access_key': settings.SEARCHGOV_API_ACCESS_KEY,
'query': query,
'limit': limit,
'offset': offset
}
r = requests.get('https://api.gsa.gov/technology/searchgov/v2/results/i14y', params=params)
if r.status_code == 200:
return process_site_results(r.json(), limit=limit, offset=offset)
def search(request):
"""
Takes a page request and calls the appropriate searches
depending on the type requested
"""
limit = 10
search_query = request.GET.get('query', None)
offset = request.GET.get('offset', 0)
search_type = request.GET.getlist('type', ['site'])
results = {}
results['count'] = 0
if 'candidates' in search_type and search_query:
results['candidates'] = search_candidates(search_query)
if results['candidates']:
results['count'] += len(results['candidates']['results'])
if 'committees' in search_type and search_query:
results['committees'] = search_committees(search_query)
if results['committees']:
results['count'] += len(results['committees']['results'])
if 'site' in search_type and search_query:
results['site'] = search_site(search_query, limit=limit, offset=offset)
if results['site']:
results['count'] += len(results['site']['results'])
return render(request, 'search/search.html', {
'search_query': search_query,
'results': results,
'type': search_type,
'self': {'title': 'Search results'}
})
# Policy and guidance search
# Search.gov API call
def policy_guidance_search_site(query, limit=0, offset=0):
"""Calls the Search.gov policy and guidance search and then processes the results if successful"""
params = {
'affiliate': 'fec_content_s3',
'access_key': settings.SEARCHGOV_POLICY_GUIDANCE_KEY,
'query': query,
'limit': limit,
'offset': offset
}
r = requests.get('https://api.gsa.gov/technology/searchgov/v2/results/i14y', params=params)
if r.status_code == 200:
return process_site_results(r.json(), limit=limit, offset=offset)
def policy_guidance_search(request):
"""
Takes a page request and calls the appropriate searches
depending on the type requested
"""
limit = 10
search_query = request.GET.get('query', None)
offset = request.GET.get('offset', 0)
results = policy_guidance_search_site(search_query, limit=limit, offset=offset)
current_page = int(int(offset) / limit) + 1
num_pages = 1
total_count = 0
if results:
num_pages = math.ceil(int(results['meta']['count']) / limit)
total_count = results['meta']['count'] + results['best_bets']['count']
resultset = {}
resultset['search_query'] = search_query
resultset['results'] = results
resultset['self'] = {'title': 'Guidance documents'}
resultset['offset'] = offset
resultset['num_pages'] = num_pages
resultset['current_page'] = current_page
resultset['total_count'] = total_count
return render(request, 'search/policy_guidance_search_page.html', resultset)