This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
167 lines (146 loc) · 5.46 KB
/
app.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
""" SeeCollections """
import json
from flask import Flask, render_template, request
from wtforms import Form, SelectField, StringField, validators
import requests
from key import apikey
app = Flask(__name__)
class SearchForm(Form):
""" set up wtforms class """
facet_choices = [
("lcc", "Library of Congress Classification"),
("creationdate", "Creation Date"),
("topic", "Topic"),
]
college_choices = [
("BB", "Baruch"),
("BM", "BMCC"),
("BX", "Bronx CC"),
("BC", "Brooklyn College"),
("CC", "City College"),
("NY", "City Tech"),
("SI", "CSI"),
("GC", "Graduate Center"),
("NC", "Guttman"),
("HO", "Hostos"),
("HC", "Hunter"),
("JJ", "John Jay"),
("KB", "Kingsborough"),
("LG", "LaGuardia"),
("LE", "Lehman"),
("ME", "Medgar Evers"),
("QC", "Queens College"),
("QB", "Queensborough"),
("GJ", "School of Journalism"),
("CL", "School of Law"),
("YC", "York"),
]
college = SelectField(
"college",
choices=college_choices,
validators=[validators.DataRequired(message="You must select a campus.")],
)
facet = SelectField(
"facet",
choices=facet_choices,
validators=[validators.DataRequired(message="You must select a facet.")],
)
keyword = StringField(
"keyword",
[
validators.Length(
max=200, message="You cannot enter more than 200 characters."
),
validators.Regexp(
r"^[\-a-zA-Z ]*$",
message="Invalid characters in your search string. \
Use only A-Z, -, and space.",
),
validators.DataRequired(message="You must type in something."),
],
)
@app.after_request
def add_security_headers(resp):
resp.headers["X-Content-Type-Options"] = "nosniff"
resp.headers["X-Frame-Options"] = "SAMEORIGIN"
resp.headers["X-XSS-Protection"] = "1; mode=block"
resp.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
resp.headers["Content-Security-Policy"] = "script-src 'self'; style-src 'self'; default-src 'none'"
return resp
@app.route("/", methods=["GET", "POST"])
def index():
""" display the index page """
form = SearchForm(request.form)
faux_data = {"name": "content", "children": None}
if request.method == "GET":
return render_template(
"index.html", error_message="", final_data=faux_data, form=form
)
else:
if form.validate():
get_facet = request.form["facet"]
get_college = request.form["college"]
get_keyword = request.form["keyword"]
print("----> Facet: " + get_facet)
print("----> College: " + get_college)
print("----> Terms: " + get_keyword)
resp = requests.get(
"https://api-na.hosted.exlibrisgroup.com/primo/v1/"
"search?vid=CUNY&scope=everything"
"&q=any,contains,{}&qInclude=facet_local3,exact,{}"
"&qInclude=facet_rtype,exact,books"
"&apikey={}".format(get_keyword, get_college, apikey)
)
api_call = json.loads(resp.text)
# what if there are no results?
if api_call["facets"] == []:
error_message = "<div class='alert alert-danger' role='alert'>No results found!</div>"
return render_template(
"index.html",
error_message=error_message,
final_data=faux_data,
form=form,
)
# parse the api data
facet_data = api_call["facets"]
for facet in facet_data:
if facet["name"] == get_facet:
chosen_facet = facet["values"]
# make chosen_facet a list if it is not a list
# this will happen if there is only one result
if isinstance(chosen_facet, dict):
chosen_facet = [chosen_facet]
# transform the data into a format d3 will like
transformed_facet = []
for line in chosen_facet:
temp = {"value": "name", "count": "size"}
transformed_facet.append({temp[k]: v for k, v in line.items()})
final_data = {"name": "content", "children": transformed_facet}
return render_template(
"index.html", error_message="", final_data=final_data, form=form
)
else:
try:
error_text = form.errors["keyword"][0]
except:
try:
error_text = form.errors["facet"][0]
except:
try:
error_text = form.errors["college"][0]
except:
error_text = "Unexpected error"
finally:
error_message = (
"<div class='alert alert-danger' role='alert'>"
+ error_text
+ "</div>"
)
return render_template(
"index.html",
error_message=error_message,
final_data=faux_data,
form=form,
)
if __name__ == "__main__":
app.run(port=8000, host="127.0.0.1", debug=True)