Ejemplo n.º 1
0
def browse_page():
    page_size = 45
    params = get_artwork_params()

    if not params:
        return browse_index()

    flat = '_'.join(f'{pid}={qid}' for pid, qid in params)
    item_labels = get_labels_db(qid for pid, qid in params)
    g.title = ' / '.join(find_more_props[pid] + ': ' +
                         (item_labels.get(qid) or qid) for pid, qid in params)

    q_items = get_db_items(params)
    facets = get_db_facets(params)

    all_items = q_items.all()

    page = utils.get_int_arg('page') or 1
    total = q_items.count()
    pager = Pagination(page, page_size, total)

    items = [item for item in pager.slice(all_items) if item.image_filename()]

    cache_name = f'{flat}_{page}_{page_size}'
    detail = get_image_detail_with_cache(items, cache_name)
    cache_refreshed = False

    linked_qids = {qid for pid, qid in params}
    for item in items:
        artist_qid = item.artist
        if artist_qid:
            linked_qids.add(artist_qid)
        for prop in 'P31', 'P180':
            linked_qids.update(item.linked_qids(prop))

    linked_labels = get_labels_db(linked_qids)

    for item in items:
        image_filename = item.image_filename()
        if not cache_refreshed and image_filename not in detail:
            detail = get_image_detail_with_cache(items,
                                                 cache_name,
                                                 refresh=True)
            cache_refreshed = True
        item.image = detail[image_filename]

    return render_template('find_more.html',
                           page=page,
                           label=g.title,
                           pager=pager,
                           prop_labels=find_more_props,
                           labels=find_more_props,
                           linked_labels=linked_labels,
                           items=items,
                           total=total,
                           params=params,
                           facets=facets)

    return jsonify(params=params, items=items.count(), facets=facets)
Ejemplo n.º 2
0
def catalog_page():
    params = get_artwork_params()
    bindings = filter_artwork(params)
    page = utils.get_int_arg('page') or 1
    page_size = 45

    item_ids = set()
    for row in bindings:
        item_id = wdqs.row_id(row)
        item_ids.add(item_id)

    qids = [f'Q{item_id}' for item_id in sorted(item_ids)]

    items = [Item.query.get(item_id) for item_id in item_ids]

    entities = mediawiki.get_entities_with_cache(qids)

    items = []
    other_items = set()
    for entity in entities:
        other_items.update(build_other_set(entity))
        continue

        item = {
            'label': wikibase.get_entity_label(entity),
            'qid': entity['id'],
            'item_id': int(entity['id'][1:]),
            'image_filename': wikibase.first_datavalue(entity, 'P18'),
            'entity': entity,
        }
        items.append(item)

    other = get_labels(other_items)

    flat = '_'.join(f'{pid}={qid}' for pid, qid in params)
    thumbwidth = 400
    # FIXME cache_name can be too long for filesystem
    cache_name = f'{flat}_{page}_{page_size}_{thumbwidth}'
    detail = get_image_detail_with_cache(items,
                                         cache_name,
                                         thumbwidth=thumbwidth)

    for item in items:
        item['url'] = url_for('item_page', item_id=item['item_id'])
        item['image'] = detail[item['image_filename']]

    item_labels = get_labels(qid for pid, qid in params)
    title = ' / '.join(find_more_props[pid] + ': ' + item_labels[qid]
                       for pid, qid in params)

    return render_template('catalog.html',
                           labels=find_more_props,
                           items=items,
                           other=other,
                           title=title)
Ejemplo n.º 3
0
def list_edits():
    q = Edit.query.order_by(Edit.timestamp.desc())
    page = utils.get_int_arg('page') or 1
    pager = Pagination(page, 100, q.count())

    item_count = (database.session.query(func.count(distinct(
        Edit.artwork_id))).scalar())

    user_count = (database.session.query(func.count(distinct(
        Edit.username))).scalar())

    return render_template('list_edits.html',
                           pager=pager,
                           edit_list=pager.slice(q),
                           item_count=item_count,
                           user_count=user_count)
Ejemplo n.º 4
0
def missing_image_report():
    limit = utils.get_int_arg('limit') or 1000
    q = DepictsItem.query.order_by(DepictsItem.count.desc()).limit(limit)

    qids = [item.qid for item in q]
    entities = mediawiki.get_entities_dict_with_cache(qids)

    item_list = []

    for depicts in q:
        entity = entities[depicts.qid]
        if any(
                wikibase.first_datavalue(entity, prop)
                for prop in ('P18', 'P2716')):
            continue
        item_list.append(depicts)

        # TODO: call wikidata search to find images that depict item

    return render_template('missing_image.html', item_list=item_list)
Ejemplo n.º 5
0
def property_query_page(property_id):
    pid = f'P{property_id}'
    g.title = find_more_props[pid]
    sort = request.args.get('sort')
    sort_by_name = sort and sort.lower().strip() == 'name'

    q = (database.session.query(
        Triple.object_id,
        func.count(func.distinct(Triple.subject_id)).label('c')).filter_by(
            predicate_id=property_id).join(
                Item, Item.item_id == Triple.subject_id).filter_by(
                    is_artwork=True).group_by(Triple.object_id).order_by(
                        desc('c')))

    page = utils.get_int_arg('page') or 1
    total = q.count()
    page_size = 100
    pager = Pagination(page, page_size, total)

    page_hits = pager.slice(q)

    labels = get_labels_db({f'Q{object_id}' for object_id, c in page_hits})

    hits = []
    for object_id, count in page_hits:
        qid = f'Q{object_id}'
        hits.append({
            'qid': qid,
            'label': labels.get(qid) or '[item missing]',
            'count': count
        })

    return render_template('property.html',
                           label=g.title,
                           order=('name' if sort_by_name else 'count'),
                           pid=pid,
                           page=page,
                           pager=pager,
                           hits=hits)
Ejemplo n.º 6
0
def browse_page():
    params = get_artwork_params()

    if not params:
        return browse_index()

    flat = '_'.join(f'{pid}={qid}' for pid, qid in params)

    item_labels = get_labels(qid for pid, qid in params)

    g.title = ' / '.join(find_more_props[pid] + ': ' + item_labels[qid]
                         for pid, qid in params)

    bindings = filter_artwork(params)

    try:
        facets = get_facets(params)
    except wdqs.QueryError:
        facets = {}

    page_size = 45

    item_map = wdqs.build_browse_item_map(bindings)

    all_items = []
    for item in item_map.values():
        if len(item['image_filename']) != 1:
            continue
        item['image_filename'] = item['image_filename'][0]
        all_items.append(item)

    page = utils.get_int_arg('page') or 1
    pager = Pagination(page, page_size, len(all_items))

    items = pager.slice(all_items)

    cache_name = f'{flat}_{page}_{page_size}'
    detail = get_image_detail_with_cache(items, cache_name)
    cache_refreshed = False

    for item in items:
        item['url'] = url_for('item_page', item_id=item['item_id'])
        image_filename = item['image_filename']
        if not cache_refreshed and image_filename not in detail:
            detail = get_image_detail_with_cache(items,
                                                 cache_name,
                                                 refresh=True)
            cache_refreshed = True
        item['image'] = detail[image_filename]

    catalog_url = url_for('catalog_page', **dict(params))

    return render_template('find_more.html',
                           facets=facets,
                           prop_labels=find_more_props,
                           label=g.title,
                           pager=pager,
                           params=params,
                           item_map=item_map,
                           catalog_url=catalog_url,
                           page=page,
                           labels=find_more_props,
                           bindings=bindings,
                           total=len(item_map),
                           items=items)