-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathviews.py
More file actions
131 lines (101 loc) · 3.85 KB
/
Copy pathviews.py
File metadata and controls
131 lines (101 loc) · 3.85 KB
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
import math
import re
from urllib.parse import urlencode
from django.conf import settings
from django.http import JsonResponse
import requests
from core.views import TranslatedTemplateView
from .forms import SearchForm
# Search.gov API docs: https://open.gsa.gov/api/searchgov-results/
API_ENDPOINT = "https://api.gsa.gov/technology/searchgov/v2/results/i14y?{}"
RESULTS_PER_PAGE = 20
def get_affiliate(context):
"""Given a request, return the appropriate affiliate for Search.gov.
Our default affiliate code is "cfpb". We have a separate Spanish-language
index named "cfpb_es".
"""
if context.get("current_language") == "es":
return "cfpb_es"
return "cfpb"
def get_api_key(context):
if context.get("current_language") == "es":
return settings.SEARCHGOV_ES_API_KEY
return settings.SEARCHGOV_API_KEY
def encode_url(params):
return API_ENDPOINT.format(urlencode(params))
def strip_title_suffix(title):
"""Remove the ' | Consumer Financial Protection Bureau' from the end
of search result titles. Becuase one or more words may be enclosed by
\ue000 and \ue001 characters if they match a search term, we have to be
a little flexible with the regex.
"""
suffix_regex = r" \| .*"
cleaned_title = re.sub(suffix_regex, "", title)
return cleaned_title
class SearchView(TranslatedTemplateView):
template_name = "searchgov/index.html"
def get(self, request, **kwargs):
context = self.get_context_data(**kwargs)
form = SearchForm(request.GET)
results = []
recommended = []
total_pages = 1
current_page = 1
count = 0
offset = 0
start_index = 1
end_index = RESULTS_PER_PAGE
query = ""
affiliate = get_affiliate(context)
api_key = get_api_key(context)
if form.is_valid():
query = form.cleaned_data["q"]
offset = (form.cleaned_data["page"] - 1) * RESULTS_PER_PAGE
response = requests.get(
encode_url(
{
"affiliate": affiliate,
"access_key": api_key,
"limit": RESULTS_PER_PAGE,
"offset": offset,
"query": query,
}
)
)
if response.status_code == 200:
json_data = response.json()
data = json_data["web"]
results = data["results"]
recommended = json_data["text_best_bets"]
count = data["total"]
if count > 999:
count = 999
# Protect against page shenanigans
if offset < count:
total_pages = math.ceil(count / RESULTS_PER_PAGE)
current_page = form.cleaned_data["page"]
start_index = offset + 1
end_index = offset + RESULTS_PER_PAGE
if end_index > count:
end_index = count
# Post proprocess results
for res in results:
# Strip " | CFPB" suffix
res["title"] = strip_title_suffix(res["title"])
else:
count = 0
context_data = {
"query": query,
"count": count,
"start_index": start_index,
"end_index": end_index,
"total_pages": total_pages,
"current_page": current_page,
"recommended": recommended,
"results": results,
"pagination_params": {"q": query} if query else {},
}
if form.cleaned_data.get("format") == "json":
return JsonResponse(context_data)
context.update(context_data)
return self.render_to_response(context)