Move another function into a dedicated module

This commit is contained in:
Edward Betts 2019-10-10 09:42:16 +01:00
parent d030f745de
commit bd6d13bc36
2 changed files with 31 additions and 26 deletions

29
app.py
View file

@ -3,7 +3,8 @@
from flask import Flask, render_template, url_for, redirect, request, g, jsonify, session from flask import Flask, render_template, url_for, redirect, request, g, jsonify, session
from depicts import (utils, wdqs, commons, mediawiki, painting, saam, database, from depicts import (utils, wdqs, commons, mediawiki, painting, saam, database,
dia, rijksmuseum, npg, museodelprado, barnesfoundation, dia, rijksmuseum, npg, museodelprado, barnesfoundation,
wd_catalog, human, wikibase, wikidata_oauth, parse_catalog) wd_catalog, human, wikibase, wikidata_oauth, parse_catalog,
wikidata_edit)
from depicts.pager import Pagination, init_pager from depicts.pager import Pagination, init_pager
from depicts.model import (DepictsItem, DepictsItemAltLabel, Edit, PaintingItem, from depicts.model import (DepictsItem, DepictsItemAltLabel, Edit, PaintingItem,
Language) Language)
@ -81,30 +82,6 @@ def current_url():
args.update(request.args) args.update(request.args)
return url_for(request.endpoint, **args) return url_for(request.endpoint, **args)
def create_depicts_item(item_id):
qid = f'Q{item_id}'
entity = mediawiki.get_entity(qid)
if 'en' in entity['labels']:
label = entity['labels']['en']['value']
else:
label = None
if 'en' in entity['descriptions']:
description = entity['descriptions']['en']['value']
else:
description = None
if 'en' in entity['aliases']:
alt_labels = {alt['value'] for alt in entity['aliases']['en']}
else:
alt_labels = set()
return DepictsItem(item_id=item_id,
label=label,
description=description,
alt_labels=alt_labels,
count=0)
@app.before_request @app.before_request
def init_profile(): def init_profile():
g.profiling = [] g.profiling = []
@ -141,7 +118,7 @@ def save(item_id):
depicts_item = DepictsItem.query.get(depicts_id) depicts_item = DepictsItem.query.get(depicts_id)
if depicts_item is None: if depicts_item is None:
depicts_item = create_depicts_item(depicts_id) depicts_item = wikidata_edit.create_depicts_item(depicts_id)
database.session.add(depicts_item) database.session.add(depicts_item)
database.session.commit() database.session.commit()

28
depicts/wikidata_edit.py Normal file
View file

@ -0,0 +1,28 @@
from depicts import mediawiki
from depicts.model import DepictsItem
def create_depicts_item(item_id):
qid = f'Q{item_id}'
entity = mediawiki.get_entity(qid)
if 'en' in entity['labels']:
label = entity['labels']['en']['value']
else:
label = None
if 'en' in entity['descriptions']:
description = entity['descriptions']['en']['value']
else:
description = None
if 'en' in entity['aliases']:
alt_labels = {alt['value'] for alt in entity['aliases']['en']}
else:
alt_labels = set()
return DepictsItem(item_id=item_id,
label=label,
description=description,
alt_labels=alt_labels,
count=0)