Ejemplo n.º 1
0
def stats(record_id):
    if request.method == "GET":
        from_date = request.values.get("from") # <date to provide stats from>
        until_date = request.values.get("until") # <date to provide stats until>
        provider = request.values.get("provider") # <name of third party who generated the stats>
        stat_type = request.values.get("type") # <type of statistic to return>
        
        statistics = RegistryAPI.get_statistics(record_id, from_date=from_date, until_date=until_date, provider=provider, stat_type=stat_type)
        
        # return a json response
        resp = make_response(json.dumps(statistics))
        resp.mimetype = "application/json"
        return resp
        
    elif request.method == "POST":
        # add a new statistic
        # check that we are authorised
        apikey = request.values.get("api_key")
        acc = models.Account.pull_by_auth_token(apikey)
        if acc is None:
            abort(401)
        
        # check that the record being updated exists
        reg = models.Register.pull(record_id)
        if reg is None:
            abort(404)
        
        # add the new statistics
        try:
            stat = json.loads(request.data)
        except:
            abort(400)
        
        try:
            stat_id = RegistryAPI.add_statistic(acc, reg, stat)
        except AuthorisationException:
            abort(401)
        except APIException:
            abort(400)
        
        # return a json response
        resp = make_response(json.dumps({"success" : "true", "id" : stat_id, "location" : "/record/" + record_id + "/stat/" + stat_id}))
        resp.headers["Location"] = "/record/" + record_id + "/stat/" + stat_id
        resp.mimetype = "application/json"
        resp.status_code = 201
        return resp