Esempio n. 1
0
def item_page(item_id):
    qid = f'Q{item_id}'
    item = artwork.Artwork(qid)
    from_redirect = qid in session and session.pop(qid) == 'from redirect'
    entity = mediawiki.get_entity_with_cache(qid, refresh=not from_redirect)

    existing_depicts = existing_depicts_from_entity(entity)

    width = 800
    image_filename = item.image_filename
    if image_filename:
        image = image_with_cache(qid, image_filename, width)
    else:
        image = None

    # hits = item.run_query()
    label_and_language = get_entity_label_and_language(entity)
    if label_and_language:
        label = label_and_language['label']
    else:
        label = None
    other = get_other(item.entity)

    people = human.from_name(label) if label else None

    artwork_item = ArtworkItem.query.get(item_id)
    if artwork_item is None:
        artwork_item = ArtworkItem(item_id=item_id, label=label, entity=entity)
        database.session.add(artwork_item)

    catalog = wd_catalog.get_catalog_from_artwork(entity)
    if not catalog.get('institution'):
        catalog['institution'] = get_institution(entity, other)

    label_languages = label_and_language[
        'languages'] if label_and_language else []
    show_translation_links = all(lang.code != 'en' for lang in label_languages)
    return render_template(
        'item.html',
        qid=qid,
        item_id=item_id,
        item=item,
        catalog=catalog,
        labels=find_more_props,
        entity=item.entity,
        username=g.user,
        label=label,
        label_languages=label_languages,
        show_translation_links=show_translation_links,
        existing_depicts=existing_depicts,
        image=image,
        people=people,
        other=other,
        # hits=hits,
        title=item.display_title)
Esempio n. 2
0
def save(item_id):
    depicts = request.form.getlist('depicts')
    username = wikidata_oauth.get_username()
    assert username

    token = wikidata_oauth.get_token()

    artwork_item = Item.query.get(item_id)
    if artwork_item is None:
        artwork_entity = mediawiki.get_entity_with_cache(f'Q{item_id}')
        artwork_item = Item(item_id=item_id, entity=artwork_entity)
        database.session.add(artwork_item)
        database.session.commit()

    for depicts_qid in depicts:
        depicts_id = int(depicts_qid[1:])

        depicts_item = DepictsItem.query.get(depicts_id)
        if depicts_item is None:
            depicts_item = wikidata_edit.create_depicts_item(depicts_id)
            database.session.add(depicts_item)
            database.session.commit()

    for depicts_qid in depicts:
        depicts_id = int(depicts_qid[1:])
        if existing_edit(item_id, depicts_id):
            continue

        r = create_claim(item_id, depicts_id, token)
        try:
            reply = r.json()
        except simplejson.errors.JSONDecodeError:
            mail.send_mail('depicts save error', r.text)
            raise

        save_error = reply.get('error')
        if save_error:
            mail.send_mail('depicts save error', r.text)
            return render_template('save_error.html', error=save_error)

        saved = r.json()
        lastrevid = saved['pageinfo']['lastrevid']
        assert saved['success'] == 1
        edit = Edit(username=username,
                    artwork_id=item_id,
                    depicts_id=depicts_id,
                    lastrevid=lastrevid)
        database.session.add(edit)
        database.session.commit()

    return redirect(url_for('next_page', item_id=item_id))
Esempio n. 3
0
def save(item_id):
    depicts = request.form.getlist('depicts')
    username = wikidata_oauth.get_username()
    assert username

    token = wikidata_oauth.get_token()

    artwork_item = ArtworkItem.query.get(item_id)
    if artwork_item is None:
        artwork_entity = mediawiki.get_entity_with_cache(f'Q{item_id}')
        label = wikibase.get_entity_label(artwork_entity)
        artwork_item = ArtworkItem(item_id=item_id,
                                   label=label,
                                   entity=artwork_entity)
        database.session.add(artwork_item)
        database.session.commit()

    for depicts_qid in depicts:
        depicts_id = int(depicts_qid[1:])

        depicts_item = DepictsItem.query.get(depicts_id)
        if depicts_item is None:
            depicts_item = wikidata_edit.create_depicts_item(depicts_id)
            database.session.add(depicts_item)
            database.session.commit()

    for depicts_qid in depicts:
        depicts_id = int(depicts_qid[1:])
        if existing_edit(item_id, depicts_id):
            continue

        r = create_claim(item_id, depicts_id, token)
        reply = r.json()
        if 'error' in reply:
            return 'error:' + r.text
        print(r.text)
        saved = r.json()
        lastrevid = saved['pageinfo']['lastrevid']
        assert saved['success'] == 1
        edit = Edit(username=username,
                    artwork_id=item_id,
                    depicts_id=depicts_id,
                    lastrevid=lastrevid)
        database.session.add(edit)
        database.session.commit()

    return redirect(url_for('next_page', item_id=item_id))
Esempio n. 4
0
def random_artwork():
    rows = wdqs.run_from_template_with_cache('query/artwork_no_depicts.sparql')
    has_depicts = True
    while has_depicts:
        item_id = wdqs.row_id(random.choice(rows))
        if ArtworkItem.query.get(item_id):
            continue
        entity = mediawiki.get_entity_with_cache(f'Q{item_id}', refresh=True)
        en_label = wikibase.get_en_label(entity)
        if en_label and en_label.startswith('Page from '):
            # example: Q60467422
            # 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'
    return redirect(url_for('item_page', item_id=item_id))
Esempio n. 5
0
def next_page(item_id):
    qid = f'Q{item_id}'

    entity = mediawiki.get_entity_with_cache(qid)

    width = 800
    image_filename = wikibase.first_datavalue(entity, 'P18')
    image = image_with_cache(qid, image_filename, width)

    label = wikibase.get_entity_label(entity)
    other = get_other(entity)

    other_list = []
    for key, prop_label in find_more_props.items():
        if key == 'P186':  # skip material used
            continue  # too generic
        claims = entity['claims'].get(key)
        if not claims:
            continue

        values = []

        for claim in claims:
            if 'datavalue' not in claim['mainsnak']:
                continue
            value = claim['mainsnak']['datavalue']['value']
            claim_qid = value['id']
            if claim_qid == 'Q4233718':
                continue  # anonymous artist
            numeric_id = value['numeric-id']
            href = url_for('find_more_page',
                           property_id=key[1:],
                           item_id=numeric_id)
            values.append({
                'href': href,
                'qid': claim_qid,
                'label': other.get(claim_qid),
            })

        if not values:
            continue

        qid_list = [v['qid'] for v in values]

        other_list.append({
            'label':
            prop_label,
            'image_lookup':
            url_for('find_more_json', pid=key, qid=qid_list),
            'pid':
            key,
            'values':
            values,
            'images': [],
        })

    return render_template('next.html',
                           qid=qid,
                           label=label,
                           image=image,
                           labels=find_more_props,
                           other=other,
                           entity=entity,
                           other_props=other_list)
Esempio n. 6
0
def item_page(item_id):
    qid = f'Q{item_id}'
    g.qid = qid
    item = artwork.Artwork(qid)
    from_redirect = qid in session and session.pop(qid) == 'from redirect'
    entity = mediawiki.get_entity_with_cache(qid, refresh=not from_redirect)

    if 'redirects' in entity:
        redirect_to_item_id = int(entity['redirects']['to'][1:])
        return redirect(url_for(request.endpoint, item_id=redirect_to_item_id))

    existing_depicts = existing_depicts_from_entity(entity)

    width = 800
    image_filename = item.image_filename
    if image_filename:
        image = image_with_cache(qid, image_filename, width)
    else:
        image = None

    # hits = item.run_query()
    label_and_language = get_entity_label_and_language(entity)
    if label_and_language:
        label = label_and_language['label']
    else:
        label = None
    g.label = label
    other = get_other(item.entity)

    people = human.from_name(label) if label else None

    label_languages = label_and_language[
        'languages'] if label_and_language else []
    show_translation_links = all(lang.code != 'en' for lang in label_languages)

    artwork_item = Item.query.get(item_id)
    if artwork_item is None:

        if not wdqs.is_artificial_physical_object(qid):
            return render_template(
                'not_artwork.html',
                qid=qid,
                item_id=item_id,
                item=item,
                labels=find_more_props,
                entity=item.entity,
                username=g.user,
                label=label,
                label_languages=label_languages,
                show_translation_links=show_translation_links,
                image=image,
                other=other,
                title=item.display_title)

        modified = datetime.strptime(entity['modified'], "%Y-%m-%dT%H:%M:%SZ")

        artwork_item = Item(item_id=item_id,
                            entity=entity,
                            lastrevid=entity['lastrevid'],
                            modified=modified)
        database.session.add(artwork_item)

    catalog = wd_catalog.get_catalog_from_artwork(entity)
    if not catalog.get('institution'):
        catalog['institution'] = get_institution(entity, other)

    return render_template(
        'item.html',
        qid=qid,
        item_id=item_id,
        item=item,
        catalog=catalog,
        labels=find_more_props,
        entity=item.entity,
        username=g.user,
        label=label,
        label_languages=label_languages,
        show_translation_links=show_translation_links,
        existing_depicts=existing_depicts,
        image=image,
        people=people,
        other=other,
        # hits=hits,
        title=item.display_title)