def create(self,**kwargs): # create a new node, get the node based on the type cherrypy.log('type: %s' % kwargs.get('type')) node_class = getattr(m,kwargs.get('type')) if not node_class: error(500,'wrong node class') # don't want to let through an id if 'id' in kwargs: del kwargs['id'] # create our node node = node_class(**kwargs) cherrypy.log('node: %s') # add the current user as a relative for creating it self._modify_relative(node,get_user().get_author(),m_add=True) # see if it has any relatives if '_add_relative' in kwargs: cherrypy.log('adding relative: %s' % kwargs.get('_add_relative')) to_add = m.Node.get(kwargs.get('_add_relative')) self._modify_relative(node,to_add,m_add=True) # commit that shit! m.session.commit() # return it's data return dumps(get_node_data([node.id])[0])
def update(self,**kwargs): # we are going to update an existing node # this can not involve changing it's type (right now) node = m.Node.get(kwargs.get('id')) if not node: error(404) # update the node from the kwargs for k,v in kwargs.iteritems(): if k == 'id': continue if hasattr(node,k): setattr(node,k,v) # see if there are any more tasks if '_add_relative' in kwargs: cherrypy.log('adding relative: %s' % kwargs.get('_add_relative')) to_add = m.Node.get(kwargs.get('_add_relative')) self._modify_relative(node,to_add,m_add=True) if '_remove_relative' in kwargs: to_remove = m.Node.get(kwargs.get('_remove_relative')) self._modify_relative(node,to_remove,m_remove=True) # save our changes m.session.commit() # return the up to date representation return dumps(get_node_data([node.id]))
def recent(self, count=10, depth=1, node_type="Node"): # order the nodes so that the most recently updated ones are first # followed by those who most recently had a relative updated cls = getattr(m, node_type, m.Node) query = cls.query.order_by(m.Node.updated_at.desc(), m.Node.relative_updated_at.desc(), m.Node.id.desc()) # limit to our count query = query.limit(count * 2) # the front page should not have authors or users # TODO: in query nodes = query.all() # if we aren't filtering on node type, filter out some things if cls is m.Node: filter_types = (m.User, m.Author, m.SexyLady) nodes = [n for n in nodes if not isinstance(n, filter_types)] # make sure depth is a # depth = int(depth) # we want to order these such that the newest nodes appear first # but if a node relative (comment) was added to a name # than we want to show the node the comment was added to # but not the comment itself @ root (make sense?) # reverse to work backwards rev_nodes = nodes[::-1] seen = [] for node in rev_nodes: # since we are working backwards, any node which # was already a relative that we've seen should be skipped if node in seen: nodes.remove(node) else: rels = node.get_relatives(depth) seen += rels # pull the id's off the returned tuple ids = [i.id for i in nodes][: int(count)] # get dicts of data data = get_node_data(ids, depth, show_repeats=True) # sort the data by most recently updated relative # I know this is attrocious. should be doing this # in sql data.sort(key=node_most_recent_relative_update) return dumps(data)
def get(self,node_id,depth=1): return dumps(get_node_data(node_id,depth)[0])
def list(self,node_ids,depth=1): return dumps(get_node_data(node_ids,depth))