def get(self, uri): max_visit = self.application.settings['max_visit'] tags = util.check_tags(self.get_argument('tags', '')) node_store = cache.NodeStore(self.db, max_visit + 1) try: from_node = node_store[model.node_key(uri)] except KeyError: raise web.HTTPError(404, 'could not find node') recs = engine.recommendations(node_store, from_node, tags, self.application.settings) serialize(self, recs)
def put(self, from_node, to_node): from_hash = scarecrow.ident(model.node_key(from_node)) to_hash = scarecrow.ident(model.node_key(to_node)) weight = util.check_weight(self.get_argument('weight', None)) tags = util.check_tags(self.get_argument('tags', None)) try: node = self.db[from_hash] except KeyError: #Return a not found if the node doesn't exist raise web.HTTPError(404, 'could not find from node') if not to_hash in self.db: #Return a not found if the node doesn't exist raise web.HTTPError(404, 'could not find to node') #Return a forbidden if the current user doesn't own the node if node.owner != self.current_user: raise web.HTTPError(403, 'you do not own the from node') if to_node in node.links: #Update the link if it already exists link = node.links[to_node] if weight != None: link.weight = weight if tags: link.tags = tags else: #Require the weight parameter if the link doesn't exist yet if weight == None: raise web.HTTPError(400, "requires 'weight' parameter") #Create a new link if it doesn't exist yet link = model.Storage() node.links[to_node] = link link.weight = weight link.tags = tags if tags else set([]) link.update_date = datetime.now() self.db[from_hash] = node serialize(self, link)
def put(self, uri): hash = scarecrow.ident(model.node_key(uri)) tags = util.check_tags(self.get_argument('tags', None)) if not tags: raise web.HTTPError(400, "requires 'tags' parameter") try: node = self.db[hash] except KeyError: #return a not found if the node doesn't exist raise web.HTTPError(404) if node.owner != self.current_user: #return a forbidden if the current user doesn't own the node raise web.HTTPError(403) for tag in tags: node.tags.add(tag) self.db[hash] = node serialize(self, tags)
def delete(self, uri): hash = scarecrow.ident(model.node_key(uri)) delete_tags = util.check_tags(self.get_argument('tags', None)) try: node = self.db[hash] except KeyError: #return a not found if the node doesn't exist raise web.HTTPError(404) if node['owner'] != self.current_user: #return a forbidden if the current user doesn't own the node raise web.HTTPError(403) try: if delete_tags == None: node.tags = ([]) else: for tag in delete_tags: node.tags.remove(tag) except KeyError: raise web.HTTPError(404) self.db[hash] = node
def put_node(request, uri): """Updates an existing or creates a new node identified by the given URI""" hash = scarecrow.ident(model.node_key(uri)) tags = util.check_tags(request.get_argument('tags', None)) date = util.check_datetime(request.get_argument('creation_date', None)) try: node = request.db[hash] #Update an existing node if node.owner != request.current_user: raise web.HTTPError(403, 'you do not own the node') if tags: node.tags = tags if date: node.creation_date = date except KeyError: if not tags: tags = set([]) if not date: date = datetime.now() #Create a new node if it doesn't exist node = model.Entity(uri, 'node') node.owner = request.current_user node.creation_date = date node.tags = tags node.links = {} node._cache = model.Storage() node._cache.candidates = model.Storage() node._cache.expired = False node.update_date = datetime.now() request.db[hash] = node serialize(request, node)