-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
179 lines (161 loc) · 7.27 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
168
169
170
171
172
173
174
175
176
177
178
179
import logging
import os
from typing import List, Optional
from urllib.parse import quote, unquote
from flask import Flask, render_template, request, redirect, jsonify
from models.articles import Articles
from models.cirrussearch import Cirrussearch
from models.parameters import Parameters
from models.topic import TopicItem
app = Flask(__name__)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
invalid_format = f"Not a valid QID, format must be 'Q[0-9]+'"
def build_term_row(term: str, source: str):
return f"""
<tr>
<td>
<input type="checkbox" name="terms" value="{term}" checked=true>
</td>
<td>
{term}
</td>
<td>
<span class="source">{source}</span>
</td>
</tr>
"""
def build_terms_html(user_terms: List[str], topic: Optional[TopicItem] = None) -> str:
"""Build the table row html"""
html_lines = list()
if topic is not None:
html_lines.append(build_term_row(term=topic.label, source="label"))
for term in topic.aliases:
html_lines.append(build_term_row(term=term, source="alias"))
for term in user_terms:
html_lines.append(build_term_row(term=term, source="user"))
return "\n".join(html_lines)
@app.route("/", methods=["GET", "POST"])
def index():
"""We either get a get request or a post request
If we get arguments, prefill the template"""
if request.method == "POST":
qid = request.form.get("qid", "")
limit = request.form.get("limit", "10")
cs = request.form.get("cs", "")
csa = request.form.get("csa", "")
terms = request.form.getlist("terms")
else:
qid = request.args.get("qid", "")
limit = request.args.get("limit", "10")
cs = request.args.get("cs", "")
csa = request.args.get("csa", "")
terms = request.args.getlist("terms")
if qid:
topic = TopicItem(qid=qid)
if not topic.is_valid:
return jsonify(error=invalid_format), 400
return render_template(
"index.html",
qid=qid,
limit=limit,
cs=cs,
csa=csa,
terms_html=build_terms_html(user_terms=terms, topic=topic),
)
else:
return render_template(
"index.html",
qid=qid,
limit=limit,
cs=cs,
csa=csa,
terms_html=build_terms_html(user_terms=terms),
)
@app.route("/articles", methods=["GET"])
def articles():
qid = request.args.get("qid", "")
if not qid:
return jsonify(f"Got no QID")
limit_param = request.args.get("limit", "10")
terms = request.args.getlist("terms")
if terms:
logger.debug(f"got terms: '{terms}'")
else:
logger.debug("Got no terms")
terms = list()
# exit()
cs_prefix = unquote(request.args.get("prefix", ""))
if cs_prefix:
logger.debug(f"got cirrussearch string: '{cs_prefix}'")
cs_affix = unquote(request.args.get("affix", ""))
if cs_affix:
logger.debug(f"got cirrussearch affix: '{cs_affix}'")
try:
if limit_param:
limit = int(limit_param)
else:
# Default to 10
limit = 10
except ValueError:
return jsonify(error="Limit must be an integer."), 400
topic = TopicItem(qid=qid)
if not topic.is_valid:
logger.debug(f"Invalid qid {topic.model_dump()}")
return jsonify(invalid_format)
if topic.label is None or not topic.label:
return jsonify(f"topic label was empty, please go add an english label in Wikidata. See {topic.url}")
# Call the GetArticles function with the provided variables
articles = Articles(
parameters=Parameters(
topic=topic,
limit=limit,
cirrussearch=Cirrussearch(
prefix=cs_prefix, affix=cs_affix, topic=topic, search_terms=terms
),
search_terms=terms,
)
)
# Run the queries
articles.get_items()
return render_template(
["results.html"],
queries=articles.get_query_html_rows(),
item_count=articles.item_count,
article_rows=articles.get_item_html_rows(),
qid=qid,
label=articles.parameters.topic.label,
link=topic.url,
)
def generate_qs_commands(main_subject: str, selected_qids: List[str]):
commands = list()
for qid in selected_qids:
# based on heuristic -> inferred from title
commands.append(f"{qid}|P921|{main_subject}|S887|Q69652283")
return "||".join(commands)
@app.route("/add-main-subject", methods=["POST"])
def add_main_subject():
if request.method == "POST":
selected_qids = request.form.getlist("selected_qids[]")
topic = request.form.get("main_subject")
if selected_qids and topic:
if len(selected_qids) > 30 and "topic-curator" in os.environ.get('USER'):
return jsonify("Error: Toolforge does not support long URLs "
"so we cannot process this many QIDs at once. "
"An OAUTH rewrite is planned, see "
"https://github.com/dpriskorn/WikidataTopicCurator/issues/5")
# Handle the selected_qids as needed
print(f"Got topic: {topic}")
print("Selected QIDs:", selected_qids)
commands = generate_qs_commands(
main_subject=topic, selected_qids=selected_qids
)
# https://quickstatements.toolforge.org/#/v1=%7CCREATE%7C%7C%7CLAST%7CP31%7CQ13442814%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CLen%7C%22Empirical%20research%20on%20parental%20alienation%3A%20A%20descriptive%20literature%20review%22%7C%7C%7CLAST%7CP304%7C%22105572%22%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP356%7C%2210.1016%2FJ.CHILDYOUTH.2020.105572%22%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP407%7CQ1860%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP478%7C%22119%22%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP577%7C%2B2020-12-01T00%3A00%3A00Z%2F10%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP1433%7CQ15753235%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP1476%7Cen%3A%22Empirical%20research%20on%20parental%20alienation%3A%20A%20descriptive%20literature%20review%22%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP2093%7C%22T.M.%20Marques%22%7CP1545%7C%221%22%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP2093%7C%22I.%20Narciso%22%7CP1545%7C%222%22%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C%7CLAST%7CP2093%7C%22L.C.%20Ferreira%22%7CP1545%7C%223%22%7CS248%7CQ5188229%7CS813%7C%2B2024-01-23T00%3A00%3A00Z%2F11%7C%7C
base_url = "https://quickstatements.toolforge.org"
endpoint = f"{base_url}/#/v1="
url = f"{endpoint}{quote(commands)}"
print(f"url to qs: {url}")
return redirect(location=url, code=302)
return jsonify(f"Error: No QIDs selected.")
if __name__ == "__main__":
app.run(debug=True)