def create(self, request, user_id): # fix-up "self" user if user_id.lower() == "self": user_id = request.user.id # can only create your own entries if not request.user.is_superuser and int(user_id) != request.user.id: return rc.FORBIDDEN rdfType = RDF["type"] rdfTypeStr = str(rdfType) try: body_obj = json.loads(request.body) except: return rc.BAD_REQUEST # check for existance and determine class data = body_obj.get("data", []) classURI = data.get(rdfTypeStr) if not classURI: resp = rc.BAD_REQUEST resp.write(" - missing required predicate: " + rdfTypeStr) return resp USER = Namespace(str(USER_GRAPH_URI).format(userId=user_id)) subjectId = uuid.uuid4().hex # inject the new subject-id into the JSON subject_uri = USER[subjectId] body_obj["id"] = subject_uri try: rdfutils.update_from_json( sparql_graphs_for_user(request.user), USER, COMMON_GRAPH_URI, subject_uri, body_obj ) result = rdfutils.object_to_json(sparql_graphs_for_user(request.user), COMMON_GRAPH_URI, USER, subject_uri) except Exception, e: print "Error creating new entry: " + e raise
def update(self, request, user_id, entry_id): # fix-up "self" user if user_id.lower() == "self": user_id = request.user.id # can only update your own entries if not request.user.is_superuser and int(user_id) != request.user.id: return rc.FORBIDDEN try: body_obj = json.loads(request.body) except: resp = rc.BAD_REQUEST resp.write(". Error parsing JSON") return resp if not body_obj.get("id"): resp = rc.BAD_REQUEST resp.write(". Missing required 'id' property") return resp if body_obj["id"] != entry_id: resp = rc.BAD_REQUEST resp.write(". Arguments don't match payload") return resp try: USER = Namespace(str(USER_GRAPH_URI).format(userId=user_id)) # inject id into JSON entry_uri = body_obj["id"] rdfutils.update_from_json(sparql_graphs_for_user(request.user), USER, COMMON_GRAPH_URI, entry_uri, body_obj) result = rdfutils.object_to_json(sparql_graphs_for_user(request.user), COMMON_GRAPH_URI, USER, entry_uri) except Exception, e: raise e