Ejemplo n.º 1
0
def stat(record_id, stat_id):
    # delete the identified 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 exists
    reg = models.Register.pull(record_id)
    if reg is None:
        abort(404)
        
    # check that the stat being removed exists
    stat = models.Statistics.pull(stat_id)
    if stat is None:
        abort(404)
    
    # if we get here, delete can go ahead
    try:
        RegistryAPI.delete_statistic(acc, stat)
    except AuthorisationException:
        abort(401)
    
    # return a json response
    resp = make_response(json.dumps({"success" : "true"}))
    resp.mimetype = "application/json"
    return resp
Ejemplo n.º 2
0
def admin(record_id):
    # replace an existing admin record
    # check that we have admin access
    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)
    
    # get the replacement admin object
    try:
        admin = json.loads(request.data)
    except:
        abort(400)
    
    try:
        RegistryAPI.set_admin(acc, reg, admin)
    except AuthorisationException:
        abort(401)
    
    # return a json response
    resp = make_response(json.dumps({"success" : "true"}))
    resp.mimetype = "application/json"
    return resp
Ejemplo n.º 3
0
def query():
    # extract all the potential query arguments
    es = request.values.get("es") # <elasticsearch query object>
    if es is None:
        es = request.values.get("source") # alternative location for the es query object
    q = request.values.get("q") # <free text search>
    fields = request.values.get("fields") # <list of top level fields required>
    from_number = request.values.get("from") # <start result number>
    size = request.values.get("size") # <page size>
    
    # the es argument is an encoded json string
    if es is not None:
        es = json.loads(es)
    
    print es
    
    # fields is a comma separated list of values
    # which may consist only of "admin" or "register"
    if fields is not None:
        fields = [f.strip() for f in fields.split(",") if f.strip() in ["admin", "register"]]
    
    es_result = RegistryAPI.search(es=es, q=q, fields=fields, from_number=from_number, size=size)
    
    # return a json response
    resp = make_response(json.dumps(es_result))
    resp.mimetype = "application/json"
    return resp
Ejemplo n.º 4
0
def create_record():
    # 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)
    
    # read in the register data
    try:
        newregister = json.loads(request.data)
    except:
        abort(400)
    
    try:
        id = RegistryAPI.create_register(acc, newregister)
    except AuthorisationException:
        abort(401)
    except APIException:
        abort(400)
    
    # return a json response
    resp = make_response(json.dumps({"success" : "true", "id" : id, "location" : "/record/" + id}))
    resp.headers["Location"] = "/record/" + id
    resp.mimetype = "application/json"
    resp.status_code = 201
    return resp
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def history(record_id):
    # get the history of an object
    from_date = request.values.get("from") # <date to get the history stats from>
    until_date = request.values.get("until") # <date to provide history until>
    
    h = RegistryAPI.get_history(record_id, from_date=from_date, until_date=until_date)
    if h is None or len(h) == 0:
        abort(404)
    
    # return a json response
    resp = make_response(json.dumps(h))
    resp.mimetype = "application/json"
    return resp
Ejemplo n.º 7
0
def change():
    from_date = request.values.get("from") # <date to provide changes from>
    until_date =  request.values.get("until") # <date to provide changes until>
    from_number = request.values.get("from") # <start result number>
    size = request.values.get("size") # <page size>
    
    # need to check that the dates are correct
    if from_date is not None and not _validate_date(from_date):
        abort(400)
    if until_date is not None and not _validate_date(until_date):
        abort(400)
    
    es_result = RegistryAPI.change_list(from_date=from_date, until_date=until_date, from_number=from_number, size=size)
    
    # return a json response
    resp = make_response(json.dumps(es_result))
    resp.mimetype = "application/json"
    return resp
Ejemplo n.º 8
0
def record(record_id):
    
    if request.method == "GET":
        # retrieve the record
        # unauthenticated
        register = RegistryAPI.get_registry_entry(record_id)
        if register is None:
            abort(404)
        # return a json response
        resp = make_response(register.json)
        resp.mimetype = "application/json"
        return resp
    
    elif request.method == "POST":
        # 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)
        
        # update the record
        try:
            newregister = json.loads(request.data)
        except:
            abort(400)
        
        try:
            RegistryAPI.update_register(acc, reg, newregister)
        except AuthorisationException:
            abort(401)
        except APIException:
            abort(400)
        
        # return a json response
        resp = make_response(json.dumps({"success" : "true"}))
        resp.mimetype = "application/json"
        return resp
    
    elif request.method == "PUT":
        # 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)
        
        # replace the record
        try:
            newregister = json.loads(request.data)
        except:
            abort(400)
        
        try:
            RegistryAPI.replace_register(acc, reg, newregister)
        except AuthorisationException:
            abort(401)
        except APIException:
            abort(400)
        
        # return a json response
        resp = make_response(json.dumps({"success" : "true"}))
        resp.mimetype = "application/json"
        return resp
        
    elif request.method == "DELETE":
        # 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)
        
        # delete it
        try:
            RegistryAPI.delete_register(acc, reg)
        except AuthorisationException:
            abort(401)
        
        # return a json response
        resp = make_response(json.dumps({"success" : "true"}))
        resp.mimetype = "application/json"
        return resp