Example #1
0
def collection_get(cid='', format="json", include_history=False):
    logger.info(u"in collection_get".format(cid=cid))

    # if not include items, then just return the collection straight from couch
    if (request.args.get("include_items") in ["0", "false", "False"]):
        coll = collection.get_collection_doc(cid)
        if not coll:
            abort_custom(404, "collection not found")

        # except if format is csv.  can't do that.
        if format == "csv":
            abort_custom(405, "csv method not supported for not include_items")
        else:
            response_code = 200
            resp = make_response(json.dumps(coll, sort_keys=True, indent=4),
                                 response_code)
    else:
        include_history = (request.args.get("include_history", 0)
                           in ["1", "true", "True"])
        (coll_with_items, something_currently_updating
         ) = collection.get_collection_with_items_for_client(
             cid, myrefsets, myredis, mydao, include_history)

        # return success if all reporting is complete for all items
        if something_currently_updating:
            response_code = 210  # update is not complete yet
        else:
            response_code = 200

        if format == "csv":
            # remove scopus before exporting to csv, so don't add magic keep-scopus keys to clean method
            clean_items = [
                item_module.clean_for_export(item)
                for item in coll_with_items["items"]
            ]
            csv = collection.make_csv_stream(clean_items)
            resp = make_response(csv, response_code)
            resp.mimetype = "text/csv;charset=UTF-8"
            resp.headers.add(
                "Content-Disposition",
                "attachment; filename=impactstory-{cid}.csv".format(cid=cid))
            resp.headers.add("Content-Encoding", "UTF-8")
        else:

            secret_key = os.getenv("API_ADMIN_KEY")
            if request.args.get("api_admin_key"):
                supplied_key = request.args.get("api_admin_key", "")
            else:
                supplied_key = request.args.get("key", "")

            clean_if_necessary_items = [
                item_module.clean_for_export(item, supplied_key, secret_key)
                for item in coll_with_items["items"]
            ]

            coll_with_items["items"] = clean_if_necessary_items
            resp = make_response(
                json.dumps(coll_with_items, sort_keys=True, indent=4),
                response_code)
    return resp
Example #2
0
def collection_get(cid='', format="json", include_history=False):
    logger.info(u"in collection_get".format(cid=cid))

    # if not include items, then just return the collection straight from couch
    if (request.args.get("include_items") in ["0", "false", "False"]):
        coll = collection.get_collection_doc(cid)
        if not coll:
            abort_custom(404, "collection not found")

        # except if format is csv.  can't do that.
        if format == "csv":
            abort_custom(405, "csv method not supported for not include_items")
        else:
            response_code = 200
            resp = make_response(json.dumps(coll, sort_keys=True, indent=4),
                                 response_code)
    else:
        include_history = (request.args.get("include_history", 0) in ["1", "true", "True"])
        (coll_with_items, something_currently_updating) = collection.get_collection_with_items_for_client(cid, myrefsets, myredis, mydao, include_history)

        # return success if all reporting is complete for all items    
        if something_currently_updating:
            response_code = 210 # update is not complete yet
        else:
            response_code = 200

        if format == "csv":
            # remove scopus before exporting to csv, so don't add magic keep-scopus keys to clean method
            clean_items = [item_module.clean_for_export(item) for item in coll_with_items["items"]]
            csv = collection.make_csv_stream(clean_items)
            resp = make_response(csv, response_code)
            resp.mimetype = "text/csv;charset=UTF-8"
            resp.headers.add("Content-Disposition",
                             "attachment; filename=impactstory-{cid}.csv".format(
                                cid=cid))
            resp.headers.add("Content-Encoding",
                             "UTF-8")
        else:

            secret_key = os.getenv("API_ADMIN_KEY") 
            if request.args.get("api_admin_key"):
                supplied_key = request.args.get("api_admin_key", "")
            else:
                supplied_key = request.args.get("key", "")

            clean_if_necessary_items = [item_module.clean_for_export(item, supplied_key, secret_key)
                for item in coll_with_items["items"]]

            coll_with_items["items"] = clean_if_necessary_items
            resp = make_response(json.dumps(coll_with_items, sort_keys=True, indent=4),
                                 response_code)
    return resp
Example #3
0
def collection_get(cid='', format="json", include_history=False):
    coll = mydao.get(cid)
    if not coll:
        abort(404)

    # if not include items, then just return the collection straight from couch
    if (request.args.get("include_items") in ["0", "false", "False"]):
        # except if format is csv.  can't do that.
        if format == "csv":
            abort(405)  # method not supported
        else:
            response_code = 200
            resp = make_response(json.dumps(coll, sort_keys=True, indent=4),
                                 response_code)
            resp.mimetype = "application/json"
    else:
        try:
            include_history = (request.args.get("include_history", 0) in ["1", "true", "True"])
            (coll_with_items, something_currently_updating) = collection.get_collection_with_items_for_client(cid, myrefsets, myredis, mydao, include_history)
        except (LookupError, AttributeError):  
            logger.error("couldn't get tiids for GET collection '{cid}'".format(cid=cid))
            abort(404)  # not found

        # return success if all reporting is complete for all items    
        if something_currently_updating:
            response_code = 210 # update is not complete yet
        else:
            response_code = 200

        if format == "csv":
            # remove scopus before exporting to csv, so don't add magic keep-scopus keys to clean method
            clean_items = [item_module.clean_for_export(item) for item in coll_with_items["items"]]
            csv = collection.make_csv_stream(clean_items)
            resp = make_response(csv, response_code)
            resp.mimetype = "text/csv;charset=UTF-8"
            resp.headers.add("Content-Disposition",
                             "attachment; filename=impactstory-{cid}.csv".format(
                                cid=cid))
            resp.headers.add("Content-Encoding",
                             "UTF-8")
        else:
            api_key = request.args.get("key", None)
            clean_if_necessary_items = [item_module.clean_for_export(item, api_key, os.getenv("API_KEY")) 
                for item in coll_with_items["items"]]
            coll_with_items["items"] = clean_if_necessary_items
            resp = make_response(json.dumps(coll_with_items, sort_keys=True, indent=4),
                                 response_code)
            resp.mimetype = "application/json"
    return resp
Example #4
0
def get_item_from_tiid(tiid,
                       format=None,
                       include_history=False,
                       callback_name=None):
    try:
        item = item_module.get_item(tiid, myrefsets, myredis)
    except (LookupError, AttributeError):
        abort_custom(404, "item does not exist")

    if not item:
        abort_custom(404, "item does not exist")

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

    api_key = request.args.get("key", None)
    clean_item = item_module.clean_for_export(item, api_key,
                                              os.getenv("API_ADMIN_KEY"))
    clean_item[
        "HTTP_status_code"] = response_code  # hack for clients who can't read real response codes

    resp_string = json.dumps(clean_item, sort_keys=True, indent=4)
    if callback_name is not None:
        resp_string = callback_name + '(' + resp_string + ')'

    resp = make_response(resp_string, response_code)

    return resp
Example #5
0
def get_item_from_tiid(tiid, format=None, include_history=False, callback_name=None):
    try:
        item = item_module.get_item(tiid, myrefsets, myredis)
    except (LookupError, AttributeError):
        abort_custom(404, "item does not exist")

    if not item:
        abort_custom(404, "item does not exist")

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

    api_key = request.args.get("key", None)
    clean_item = item_module.clean_for_export(item, api_key, os.getenv("API_ADMIN_KEY"))
    clean_item["HTTP_status_code"] = response_code  # hack for clients who can't read real response codes

    resp_string = json.dumps(clean_item, sort_keys=True, indent=4)
    if callback_name is not None:
        resp_string = callback_name + '(' + resp_string + ')'

    resp = make_response(resp_string, response_code)

    return resp
Example #6
0
def cleaned_items(tiids, myredis):
    items_dict = collection.get_items_for_client(tiids, myrefsets, myredis)

    secret_key = os.getenv("API_ADMIN_KEY")
    supplied_key = request.args.get("api_admin_key", "")
    cleaned_items_dict = {}
    for tiid in items_dict:
        cleaned_items_dict[tiid] = item_module.clean_for_export(items_dict[tiid], supplied_key, secret_key)
    return cleaned_items_dict
Example #7
0
def cleaned_items(tiids, myredis, override_export_clean=False, most_recent_metric_date=None, most_recent_diff_metric_date=None):
    items_dict = collection.get_items_for_client(tiids, myrefsets, myredis, most_recent_metric_date, most_recent_diff_metric_date)

    secret_key = os.getenv("API_ADMIN_KEY")
    supplied_key = request.args.get("api_admin_key", "")
    cleaned_items_dict = {}
    for tiid in items_dict:
        cleaned_items_dict[tiid] = item_module.clean_for_export(items_dict[tiid], supplied_key, secret_key, override_export_clean)
    return cleaned_items_dict
Example #8
0
 def test_clean_for_export_given_wrong_secret_key(self):
     self.d.save(self.ITEM_DATA)
     item = item_module.get_item("test", self.myrefsets, self.d)
     item["metrics"]["scopus:citations"] = {"values":{"raw": 22}}
     item["metrics"]["citeulike:bookmarks"] = {"values":{"raw": 33}}
     response = item_module.clean_for_export(item, "WRONG", "SECRET")
     print response["metrics"].keys()
     expected = ['bar:views', 'wikipedia:mentions']
     assert_equals(response["metrics"].keys(), expected)
Example #9
0
def cleaned_items(tiids, myredis):
    items_dict = collection.get_items_for_client(tiids, myrefsets, myredis)

    secret_key = os.getenv("API_ADMIN_KEY")
    supplied_key = request.args.get("api_admin_key", "")
    cleaned_items_dict = {}
    for tiid in items_dict:
        cleaned_items_dict[tiid] = item_module.clean_for_export(
            items_dict[tiid], supplied_key, secret_key)
    return cleaned_items_dict
    def test_clean_for_export_given_correct_secret_key(self):
        self.save_test_item()

        item = item_module.get_item("test", self.myrefsets, self.r)
        item["metrics"]["scopus:citations"] = {"values":{"raw": 22}}
        item["metrics"]["citeulike:bookmarks"] = {"values":{"raw": 33}}
        response = item_module.clean_for_export(item, "SECRET", "SECRET")
        print response["metrics"].keys()
        expected = ['altmetric_com:tweets', 'wikipedia:mentions', 'scopus:citations', 'citeulike:bookmarks']
        assert_equals(sorted(response["metrics"].keys()), sorted(expected))
    def test_clean_for_export_no_key(self):
        self.save_test_item()

        item = item_module.get_item("test", self.myrefsets, self.r)
        item["metrics"]["scopus:citations"] = {"values":{"raw": 22}}
        item["metrics"]["citeulike:bookmarks"] = {"values":{"raw": 33}}
        response = item_module.clean_for_export(item)
        print response["metrics"].keys()
        expected = ['altmetric_com:tweets', 'wikipedia:mentions']
        assert_items_equal(response["metrics"].keys(), expected)
Example #12
0
    def test_clean_for_export_given_wrong_secret_key(self):
        self.save_test_item()

        item = item_module.get_item("test", self.myrefsets, self.d)
        item["metrics"]["scopus:citations"] = {"values":{"raw": 22}}
        item["metrics"]["citeulike:bookmarks"] = {"values":{"raw": 33}}
        response = item_module.clean_for_export(item, "WRONG", "SECRET")
        print response["metrics"].keys()
        expected = ['topsy:tweets', 'wikipedia:mentions']
        assert_equals(response["metrics"].keys(), expected)
Example #13
0
def cleaned_items(tiids,
                  myredis,
                  override_export_clean=False,
                  most_recent_metric_date=None,
                  most_recent_diff_metric_date=None):
    items_dict = collection.get_items_for_client(tiids, myrefsets, myredis,
                                                 most_recent_metric_date,
                                                 most_recent_diff_metric_date)

    secret_key = os.getenv("API_ADMIN_KEY")
    supplied_key = request.args.get("api_admin_key", "")
    cleaned_items_dict = {}
    for tiid in items_dict:
        cleaned_items_dict[tiid] = item_module.clean_for_export(
            items_dict[tiid], supplied_key, secret_key, override_export_clean)
    return cleaned_items_dict
Example #14
0
def get_item_from_tiid(tiid, format=None, include_history=False):

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

    if not item:
        abort(404)

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

    api_key = request.args.get("key", None)
    clean_item = item_module.clean_for_export(item, api_key, os.getenv("API_KEY"))
    resp = make_response(json.dumps(clean_item, sort_keys=True, indent=4),
                         response_code)
    resp.mimetype = "application/json"

    return resp