Ejemplo n.º 1
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