def test_list_of(): assert utils.list_of_strings(1) is False assert utils.list_of_strings("a") is False assert utils.list_of_strings([]) is True assert utils.list_of_strings(["a"]) is True assert utils.list_of_strings(["a", 1]) is False assert utils.list_of_strings(["a", []]) is False assert utils.list_of_strings(["a", ["a"]]) is False assert utils.list_of_strings([lambda x: x]) is False assert utils.list_of_ints(1) is False assert utils.list_of_ints("1") is False assert utils.list_of_ints(["1"]) is False assert utils.list_of_ints([1]) is True assert utils.list_of_ints([1, "1"]) is False assert utils.list_of_ints([1, 2]) is True assert utils.list_of_ints([lambda x: x]) is False
def tasks_recent(request, body): limit = body.get("limit", 100) offset = body.get("offset", 0) # Various filters. cats = body.get("cats") packs = body.get("packs") score_range = body.get("score") filters = {} if cats: if not list_of_strings(cats): return json_error_response("invalid categories") filters["info.category"] = {"$in": cats} if packs: if not list_of_strings(packs): return json_error_response("invalid packages") filters["info.package"] = {"$in": packs} if score_range and isinstance(score_range, basestring): if score_range.count("-") != 1: return json_error_response("faulty score") score_min, score_max = score_range.split("-") if not score_min.isdigit() or not score_max.isdigit(): return json_error_response("faulty score") score_min = int(score_min) score_max = int(score_max) if score_min < 0 or score_min > 10: return json_error_response("faulty score") if score_max < 0 or score_max > 10: return json_error_response("faulty score") # Because scores can be higher than 10. # TODO Once we start capping the score, limit this naturally. if score_max == 10: score_max = 999 filters["info.score"] = { "$gte": score_min, "$lte": score_max, } if not isinstance(offset, (int, long)): return json_error_response("invalid offset") if not isinstance(limit, (int, long)): return json_error_response("invalid limit") # TODO Use a mongodb abstraction class once there is one. cursor = mongo.db.analysis.find(filters, ["info", "target"], sort=[("_id", pymongo.DESCENDING) ]).limit(limit).skip(offset) tasks = {} for row in cursor: info = row.get("info", {}) if not info: continue category = info.get("category") if category == "file": f = row.get("target", {}).get("file", {}) if f.get("name"): target = os.path.basename(f["name"]) else: target = None md5 = f.get("md5") or "-" elif category == "url": target = row["target"]["url"] md5 = "-" elif category == "archive": target = row.get("target", {}).get("human", "-") md5 = "-" else: target = None md5 = "-" tasks[info["id"]] = { "id": info["id"], "target": target, "md5": md5, "category": category, "added_on": info.get("added"), "completed_on": info.get("ended"), "status": "reported", "score": info.get("score"), } return JsonResponse( { "tasks": sorted( tasks.values(), key=lambda task: task["id"], reverse=True), }, safe=False)
def tasks_recent(request, body): limit = body.get("limit", 100) offset = body.get("offset", 0) # Various filters. cats = body.get("cats") packs = body.get("packs") score_range = body.get("score") filters = {} if cats: if not list_of_strings(cats): return json_error_response("invalid categories") filters["info.category"] = {"$in": cats} if packs: if not list_of_strings(packs): return json_error_response("invalid packages") filters["info.package"] = {"$in": packs} if score_range and isinstance(score_range, basestring): if score_range.count("-") != 1: return json_error_response("faulty score") score_min, score_max = score_range.split("-") if not score_min.isdigit() or not score_max.isdigit(): return json_error_response("faulty score") score_min = int(score_min) score_max = int(score_max) if score_min < 0 or score_min > 10: return json_error_response("faulty score") if score_max < 0 or score_max > 10: return json_error_response("faulty score") # Because scores can be higher than 10. # TODO Once we start capping the score, limit this naturally. if score_max == 10: score_max = 999 filters["info.score"] = { "$gte": score_min, "$lte": score_max, } if not isinstance(offset, (int, long)): return json_error_response("invalid offset") if not isinstance(limit, (int, long)): return json_error_response("invalid limit") # TODO Use a mongodb abstraction class once there is one. cursor = mongo.db.analysis.find( filters, ["info", "target"], sort=[("_id", pymongo.DESCENDING)] ).limit(limit).skip(offset) tasks = {} for row in cursor: info = row.get("info", {}) if not info or info["id"] in tasks: continue category = info.get("category") if category == "file": f = row.get("target", {}).get("file", {}) if f.get("name"): target = os.path.basename(f["name"]) else: target = None md5 = f.get("md5") or "-" elif category == "url": target = row["target"]["url"] md5 = "-" elif category == "archive": target = row.get("target", {}).get("human", "-") md5 = "-" else: target = None md5 = "-" tasks[info["id"]] = { "id": info["id"], "target": target, "md5": md5, "category": category, "added_on": info.get("added"), "completed_on": info.get("ended"), "status": "reported", "score": info.get("score"), } return JsonResponse({ "tasks": sorted( tasks.values(), key=lambda task: task["id"], reverse=True ), }, safe=False)