From f54401ef0553e9d83addcd4facf44d5d624e99b9 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 25 Nov 2023 20:59:10 +0000 Subject: [PATCH 1/5] Show list of hits on article page --- templates/article2.html | 15 +++++++++++---- web_view.py | 35 ++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/templates/article2.html b/templates/article2.html index e6ef74c..55d05b0 100644 --- a/templates/article2.html +++ b/templates/article2.html @@ -1,6 +1,6 @@ {% extends "base.html" %} -{% block title %}Link '{{ title }}' in '{{ hit.title }}'{% endblock %} +{% block title %}Link '{{ title }}' in '{{ hit_title }}'{% endblock %} {% block style %} @@ -8,7 +8,7 @@ {% block content %}
-

Link '{{ title }}' in '{{ hit.title }}'

+

Link '{{ title }}' in '{{ hit_title }}'

@@ -30,12 +30,19 @@ {{ diff | safe }} - +
- skip + skip
+ +
    + {% for hit in hits %} + {% set url = url_for("article_page", url_title=url_title, title=hit.title) %} +
  1. {{ hit.title }} – {{ hit.snippet | safe }}
  2. + {% endfor %} +
{% endblock %} diff --git a/web_view.py b/web_view.py index 5e1e507..a8804cc 100755 --- a/web_view.py +++ b/web_view.py @@ -236,7 +236,7 @@ def match_type(q: str, snippet: str) -> str | None: class NoGoodHit(Exception): - pass + """No good hit.""" def get_best_hit(title: str, hits: list[Hit]) -> tuple[Hit, dict[str, typing.Any]]: @@ -271,30 +271,39 @@ def article_page(url_title: str) -> str | Response: flask.url_for("article_page", url_title=url_title, after=hit_title) ) + article_title = flask.request.args.get("title") + total = search_count(from_title) with_link = search_count_with_link(from_title) no_link_count, hits = search_no_link(from_title) - after = flask.request.args.get("after") - if after: - print(after) - hits_iter = itertools.dropwhile(lambda hit: hit["title"] != after, hits) - skip = next(hits_iter, None) - if skip: - hits = list(hits_iter) + by_title = {hit["title"]: hit for hit in hits} - try: - hit, found = get_best_hit(from_title, hits) - except NoGoodHit: - return flask.render_template("all_done.html") + if article_title in by_title: + hit = by_title[article_title] + found = get_diff(from_title, hit["title"], None) + else: + after = flask.request.args.get("after") + if after: + print(after) + hits_iter = itertools.dropwhile(lambda hit: hit["title"] != after, hits) + skip = next(hits_iter, None) + if skip: + hits = list(hits_iter) + + try: + hit, found = get_best_hit(from_title, hits) + except NoGoodHit: + return flask.render_template("all_done.html") return flask.render_template( "article2.html", title=from_title, total=total, with_link=with_link, - hit=hit, + hit_title=hit["title"], + hits=hits, replacement=found["replacement"], diff=found["diff"], found=found, From 6f4ae2537fc193af93b7dc912cd882793fa6ddf3 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 26 Nov 2023 14:10:00 +0000 Subject: [PATCH 2/5] Add static/bootstrap to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0dcd64f..613f75e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ .mypy_cache/ node_modules package-lock.json +static/bootstrap From 32a7e939a52d07bdb0912c4d4dfed19cec118a39 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 26 Nov 2023 14:13:51 +0000 Subject: [PATCH 3/5] Rename article2.html to article.html --- templates/article.html | 76 +++++++++++++++++--------------------- templates/article2.html | 48 ------------------------ templates/article_old.html | 56 ++++++++++++++++++++++++++++ web_view.py | 2 +- 4 files changed, 91 insertions(+), 91 deletions(-) delete mode 100644 templates/article2.html create mode 100644 templates/article_old.html diff --git a/templates/article.html b/templates/article.html index ec3d3e4..55d05b0 100644 --- a/templates/article.html +++ b/templates/article.html @@ -1,56 +1,48 @@ {% extends "base.html" %} -{% block title %}{{ title }}{% endblock %} +{% block title %}Link '{{ title }}' in '{{ hit_title }}'{% endblock %} {% block style %} - + {% endblock %} {% block content %}
-

{{ self.title() }}

-
+

Link '{{ title }}' in '{{ hit_title }}'

+
-
+ +
Username: {{ g.user }}
+ +
view article
+ +
back to index
+ +
total: {{ total }}
+
with link: {{ with_link }}
+
ratio: {{ "{:.1%}".format(with_link / total) }}
+ {#
hit: {{ hit }}
#} +
replacement: {{ found.replacement }}
+
section: {{ found.section }}
+ + {{ diff | safe }} +
+
+ +
+ + skip +
+
+ +
    + {% for hit in hits %} + {% set url = url_for("article_page", url_title=url_title, title=hit.title) %} +
  1. {{ hit.title }} – {{ hit.snippet | safe }}
  2. + {% endfor %} +
- - - - - {% endblock %} diff --git a/templates/article2.html b/templates/article2.html deleted file mode 100644 index 55d05b0..0000000 --- a/templates/article2.html +++ /dev/null @@ -1,48 +0,0 @@ -{% extends "base.html" %} - -{% block title %}Link '{{ title }}' in '{{ hit_title }}'{% endblock %} - -{% block style %} - -{% endblock %} - -{% block content %} -
-

Link '{{ title }}' in '{{ hit_title }}'

-
- - -
- -
Username: {{ g.user }}
- -
view article
- -
back to index
- -
total: {{ total }}
-
with link: {{ with_link }}
-
ratio: {{ "{:.1%}".format(with_link / total) }}
- {#
hit: {{ hit }}
#} -
replacement: {{ found.replacement }}
-
section: {{ found.section }}
- - {{ diff | safe }} -
-
- -
- - skip -
-
- -
    - {% for hit in hits %} - {% set url = url_for("article_page", url_title=url_title, title=hit.title) %} -
  1. {{ hit.title }} – {{ hit.snippet | safe }}
  2. - {% endfor %} -
-
-{% endblock %} - diff --git a/templates/article_old.html b/templates/article_old.html new file mode 100644 index 0000000..ec3d3e4 --- /dev/null +++ b/templates/article_old.html @@ -0,0 +1,56 @@ +{% extends "base.html" %} + +{% block title %}{{ title }}{% endblock %} + +{% block style %} + +{% endblock %} + +{% block content %} +
+

{{ self.title() }}

+
+ + +
+
+
+ + + + + +{% endblock %} + diff --git a/web_view.py b/web_view.py index a8804cc..75926cd 100755 --- a/web_view.py +++ b/web_view.py @@ -298,7 +298,7 @@ def article_page(url_title: str) -> str | Response: return flask.render_template("all_done.html") return flask.render_template( - "article2.html", + "article.html", title=from_title, total=total, with_link=with_link, From 39f9ba31ede7dbd83212019daed5eedff27cefe8 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 6 Dec 2023 09:53:06 +0000 Subject: [PATCH 4/5] Raise LoginNeeded if not logged in --- add_links/wikidata_oauth.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/add_links/wikidata_oauth.py b/add_links/wikidata_oauth.py index d91b66e..9b4de72 100644 --- a/add_links/wikidata_oauth.py +++ b/add_links/wikidata_oauth.py @@ -1,7 +1,10 @@ +"""Wikipedia OAuth.""" + import typing import urllib from typing import cast +import requests from flask import current_app, session from requests_oauthlib import OAuth1Session @@ -9,6 +12,12 @@ wiki_hostname = "en.wikipedia.org" api_url = f"https://{wiki_hostname}/w/api.php" +class LoginNeeded(Exception): + """Not logged in.""" + + pass + + def get_edit_proxy() -> dict[str, str]: """Retrieve proxy information from config.""" edit_proxy = current_app.config.get("EDIT_PROXY") @@ -18,7 +27,7 @@ def get_edit_proxy() -> dict[str, str]: return {} -def api_post_request(params: dict[str, str | int]): +def api_post_request(params: dict[str, str | int]) -> requests.Response: """HTTP Post using Oauth.""" app = current_app # url = "https://www.wikidata.org/w/api.php" @@ -34,12 +43,14 @@ def api_post_request(params: dict[str, str | int]): return oauth.post(api_url, data=params, timeout=4, proxies=proxies) -def raw_request(params: typing.Mapping[str, str | int]): +def raw_request(params: typing.Mapping[str, str | int]) -> requests.Response: """Low-level API request.""" app = current_app # url = "https://www.wikidata.org/w/api.php?" + urlencode(params) client_key = app.config["CLIENT_KEY"] client_secret = app.config["CLIENT_SECRET"] + if "owner_key" not in session or "owner_secret" not in session: + raise LoginNeeded oauth = OAuth1Session( client_key, client_secret=client_secret, From 128d84fce9816b3172425ec643c03f3ed74e70c2 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 6 Dec 2023 09:53:35 +0000 Subject: [PATCH 5/5] Redirect to oauth_start if not logged in. --- web_view.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web_view.py b/web_view.py index 75926cd..436307a 100755 --- a/web_view.py +++ b/web_view.py @@ -266,7 +266,10 @@ def article_page(url_title: str) -> str | Response: if flask.request.method == "POST": hit_title = flask.request.form["hit"] - do_save(from_title, hit_title) + try: + do_save(from_title, hit_title) + except wikidata_oauth.LoginNeeded: + return flask.redirect(flask.url_for("start_oauth")) return flask.redirect( flask.url_for("article_page", url_title=url_title, after=hit_title) )