Use database to pick random artwork

Now we store details of artworks in the local database, we don't need to
use the Wikidata query service to pick a random artwork.
This commit is contained in:
Edward Betts 2019-12-30 09:16:18 +00:00
parent cacafb1d52
commit 28a03212f5

31
app.py
View file

@ -213,31 +213,24 @@ def property_query_page(property_id):
@app.route('/') @app.route('/')
def start(): def start():
return redirect(url_for('browse_page'))
return random_artwork() return random_artwork()
username = wikidata_oauth.get_username()
username = None
return render_template('start.html', username=username)
@app.route('/next') @app.route('/next')
def random_artwork(): def random_artwork():
rows = wdqs.run_from_template_with_cache('query/artwork_no_depicts.sparql') found = None
has_depicts = True while True:
while has_depicts: q = Item.query.filter_by(is_artwork=True).order_by(func.random()).limit(30)
item_id = wdqs.row_id(random.choice(rows)) for item in q:
if Item.query.get(item_id): has_depicts = 'P180' in item.entity['claims']
if has_depicts:
continue continue
entity = mediawiki.get_entity_with_cache(f'Q{item_id}', refresh=True) found = item
en_label = wikibase.get_en_label(entity) break
if en_label and en_label.startswith('Page from '): if found:
# example: Q60467422 break
# title: Page from Tales of a Parrot (Tuti-nama): text page
# this is not a painting
continue
has_depicts = 'P180' in entity['claims']
session[f'Q{item_id}'] = 'from redirect' session[found.qid] = 'from redirect'
return redirect(url_for('item_page', item_id=item_id)) return redirect(url_for('item_page', item_id=found.item_id))
@app.route('/oauth/start') @app.route('/oauth/start')
def start_oauth(): def start_oauth():