Saving to Wikidata is working.
This commit is contained in:
parent
e1ee9b047b
commit
7a233021fd
59
app.py
59
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<int:item_id>', 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<int:property_id>")
|
||||
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'
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -3,18 +3,24 @@
|
|||
{% block title %}{{ label }} ({{qid }}){% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="m-3">
|
||||
<div class="d-flex">
|
||||
<div class="p-2 flex-fill">
|
||||
<h1>{{ self.title() }}</h1>
|
||||
{# <pre>{{ other | pprint }}</pre> #}
|
||||
|
||||
{# <img src="{{ image.thumburl }}" height="{{ image.thumbheight }}" width="{{ image.thumbwidth }}"> #}
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<img src="{{ image.thumburl }}">
|
||||
<div class="alert alert-primary" role="alert">
|
||||
Thanks for contributing. Your edits have been saved to the painting on Wikidata. Use the links below to find other similar paintings to catalog.
|
||||
</div>
|
||||
|
||||
<div class="col">
|
||||
<p><a href="https://www.wikidata.org/wiki/{{ qid }}">view on Wikidata</a></p>
|
||||
<p>
|
||||
<a href="https://www.wikidata.org/wiki/{{ qid }}">view this painting on Wikidata</a>
|
||||
|
|
||||
<a href="{{ url_for('random_painting') }}">switch to another painting</a>
|
||||
|
|
||||
<a href="{{ url_for('browse_page') }}">browse paintings</a>
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
{% for key, prop_label in labels.items() %}
|
||||
{% set claims = entity['claims'][key] %}
|
||||
{% if claims %}
|
||||
|
@ -25,8 +31,11 @@
|
|||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="flex-shrink-1 vh-100">
|
||||
<img src="{{ image.thumburl }}">
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in a new issue