diff --git a/app.py b/app.py index 9faf186..b42d4b9 100755 --- a/app.py +++ b/app.py @@ -4,7 +4,7 @@ from flask import Flask, render_template, url_for, redirect, request, g, jsonify from depicts import (utils, wdqs, commons, mediawiki, painting, saam, database, dia, rijksmuseum, npg, museodelprado, barnesfoundation, wd_catalog) -from depicts.model import DepictsItem, DepictsItemAltLabel +from depicts.model import DepictsItem, DepictsItemAltLabel, Edit from requests_oauthlib import OAuth1Session from urllib.parse import urlencode import requests.exceptions @@ -122,7 +122,25 @@ def init_profile(): @app.route('/save/Q', methods=['POST']) def save(item_id): depicts = request.form.getlist('depicts') - return repr(depicts) + username = get_username() + assert username + + token = get_token() + + for depicts_qid in depicts: + depicts_id = int(depicts_qid[1:]) + r = create_claim(item_id, depicts_id, token) + reply = r.json() + if 'error' in reply: + return 'error:' + r.text + print(r.text) + edit = Edit(username=username, + painting_id=item_id, + depicts_id=depicts_id) + database.session.add(edit) + database.session.commit() + + return redirect(url_for('next_page', item_id=item_id)) @app.route("/property/P") def property_query_page(property_id): @@ -252,6 +270,43 @@ def oauth_api_request(params): return reply +def create_claim(painting_id, depicts_id, token): + painting_qid = f'Q{painting_id}' + value = json.dumps({'entity-type': 'item', + 'numeric-id': depicts_id}) + params = { + 'action': 'wbcreateclaim', + 'entity': painting_qid, + 'property': 'P180', + 'snaktype': 'value', + 'value': value, + 'token': token, + 'format': 'json', + 'formatversion': 2, + } + return oauth_api_post_request(params) + +def get_token(): + params = { + 'action': 'query', + 'meta': 'tokens', + 'format': 'json', + 'formatversion': 2, + } + reply = oauth_api_request(params) + token = reply['query']['tokens']['csrftoken'] + + return token + +def oauth_api_post_request(params): + url = 'https://www.wikidata.org/w/api.php' + client_key = app.config['CLIENT_KEY'] + client_secret = app.config['CLIENT_SECRET'] + oauth = OAuth1Session(client_key, + client_secret=client_secret, + resource_owner_key=session['owner_key'], + resource_owner_secret=session['owner_secret']) + return oauth.post(url, data=params) def image_with_cache(qid, image_filename, width): filename = f'cache/{qid}_{width}_image.json' diff --git a/depicts/model.py b/depicts/model.py index 940a075..5007504 100644 --- a/depicts/model.py +++ b/depicts/model.py @@ -1,7 +1,7 @@ from sqlalchemy.ext.declarative import declarative_base -from .database import session +from .database import session, now_utc from sqlalchemy.schema import Column, ForeignKey -from sqlalchemy.types import Integer, String +from sqlalchemy.types import Integer, String, DateTime from sqlalchemy.orm import column_property, relationship from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.sql.expression import cast @@ -33,3 +33,10 @@ class DepictsItemAltLabel(Base): def __init__(self, alt_label): self.alt_label = alt_label + +class Edit(Base): + __tablename__ = 'edit' + username = Column(String, primary_key=True) + painting_id = Column(Integer, primary_key=True) + depicts_id = Column(Integer, primary_key=True) + timestamp = Column(DateTime, default=now_utc()) diff --git a/templates/next.html b/templates/next.html index aa214b7..5b69dd8 100644 --- a/templates/next.html +++ b/templates/next.html @@ -3,30 +3,39 @@ {% block title %}{{ label }} ({{qid }}){% endblock %} {% block content %} -
-

{{ self.title() }}

- {#
{{ other | pprint }}
#} +
+
+

{{ self.title() }}

- {# #} -
-
- -
+ + +

+ view this painting on Wikidata + | + switch to another painting + | + browse paintings +

+ + + + {% for key, prop_label in labels.items() %} + {% set claims = entity['claims'][key] %} + {% if claims %} +

{{ prop_label }} ({{ key }})

+ {% for claim in claims %} + {% set claim_qid = claim.mainsnak.datavalue.value.id %} + {{ other[claim_qid] or '[ label missing ]' }} ({{ claim_qid }}) + {% endfor %} + {% endif %} + {% endfor %} -
-

view on Wikidata

- {% for key, prop_label in labels.items() %} - {% set claims = entity['claims'][key] %} - {% if claims %} -

{{ prop_label }} ({{ key }})

- {% for claim in claims %} - {% set claim_qid = claim.mainsnak.datavalue.value.id %} - {{ other[claim_qid] or '[ label missing ]' }} ({{ claim_qid }}) - {% endfor %} - {% endif %} - {% endfor %} -
+
+ +
{% endblock %}