Exemplo n.º 1
0
def get_collection_with_items_for_client(cid, myrefsets, myredis, mydao):
    startkey = [cid, 0]
    endkey = [cid, "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"]
    view_response = mydao.db.view(
        "collections_with_items/collections_with_items", include_docs=True, startkey=startkey, endkey=endkey
    )
    # the first row is the collection document
    first_row = view_response.rows[0]
    collection = first_row.doc
    try:
        del collection["ip_address"]
    except KeyError:
        pass
    del collection["alias_tiids"]

    # start with the 2nd row, since 1st row is the collection document
    collection["items"] = []
    if len(view_response.rows) > 1:
        for row in view_response.rows[1:]:
            item_doc = row.doc
            try:
                item_for_client = ItemFactory.build_item_for_client(item_doc, myrefsets)
            except (KeyError, TypeError):
                logging.info(
                    "Couldn't build item {item_doc}, excluding it from the returned collection {cid}".format(
                        item_doc=item_doc, cid=cid
                    )
                )
                item_for_client = None
                raise
            if item_for_client:
                collection["items"] += [item_for_client]

    something_currently_updating = False
    for item in collection["items"]:
        item["currently_updating"] = ItemFactory.is_currently_updating(item["_id"], myredis)
        something_currently_updating = something_currently_updating or item["currently_updating"]

    logging.info("Got items for collection %s" % cid)
    # print json.dumps(collection, sort_keys=True, indent=4)
    return (collection, something_currently_updating)
Exemplo n.º 2
0
def get_item_from_tiid(tiid, format=None):
    # TODO check request headers for format as well.

    try:
        item = ItemFactory.get_item(tiid, myrefsets, mydao)
    except (LookupError, AttributeError):
        abort(404)

    if not item:
        abort(404)

    if ItemFactory.is_currently_updating(tiid, myredis):
        response_code = 210  # not complete yet
        item["currently_updating"] = True
    else:
        response_code = 200
        item["currently_updating"] = False

    clean_item = ItemFactory.clean_for_export(item)
    resp = make_response(json.dumps(clean_item, sort_keys=True, indent=4), response_code)
    resp.mimetype = "application/json"

    return resp