def update( self, _id, nodeDisplay, nodeDescription, nodeTags, nodeParents, nodeChildren, nodeVotes, nodeStatus, nodeCreateAt, ): conn = ConnectDB().connect() conn.put( "test_nodes", _id, { "nodeDisplay": nodeDisplay, "nodeDescription": nodeDescription, "nodeTags": nodeTags, "nodeParents": nodeParents, "nodeChildren": nodeChildren, "nodeVotes": nodeVotes, # This can be replaced by invoke methods in vote class "nodeStatus": str(nodeStatus), "nodeCreateAt": nodeCreateAt, }, )
def retrieveAll(self): conn = ConnectDB().connect() nodes = conn.list("test_nodes").all() list = [] for node in nodes: list.append(node["value"]) return list
def create(self, nodeDisplay, nodeDescription, nodeTags, nodeParents, nodeChildren, nodeVotes, nodeStatus): current_time = datetime.datetime.now() conn = ConnectDB().connect() node = conn.post( "test_nodes", { "nodeDisplay": nodeDisplay, "nodeDescription": nodeDescription, "nodeTags": nodeTags, "nodeParents": nodeParents, "nodeChildren": nodeChildren, "nodeVotes": nodeVotes, # This can be replaced by invoke methods in vote class "nodeStatus": str(nodeStatus), "nodeCreateAt": str(current_time), }, ) if nodeParents: pa_node = self.retrieveById(nodeParents) pa_nodeChildren = pa_node["nodeChildren"] pa_nodeChildren.append(node.key) patch = Patch() patch.replace("nodeChildren", pa_nodeChildren) conn.patch("test_nodes", nodeParents, patch) # update parentnode's nodeChildren return node
def retrieveChild(self, _id): conn = ConnectDB().connect() node = conn.get("test_nodes", _id) _list = node["nodeChildren"] if not _list: _list = "This node has no child" return _list
def retrieveAll(): client = ConnectDB().connect() vote_list = [] vote_list_response = client.list('votes').all() for vote_res in vote_list_response: value = vote_res['value'] vote_list.append(value) return vote_list
def retrieveById(user_key): client = ConnectDB().connect() response = client.get('users', user_key) status = response.status_code if status == 200: return response.json else: return None
def retrieveAll(): client = ConnectDB().connect() user_list = [] user_list_response = client.list('users').all() for user_res in user_list_response: value = user_res['value'] user_list.append(value) return user_list
def delete(self, _id): """ Delete a node from database :param _id: node ID :return: nothing """ conn = ConnectDB().connect() conn.delete('test_nodes', _id)
def retrieveAll(): client = ConnectDB().connect() add_list =[] add_list_response = client.list('adds').all() for add_res in add_list_response: value = add_res['value'] add_list.append(value) return add_list
def retrieveByEmail(email): client = ConnectDB().connect() user_list =[] users = client.search('users', 'email:'+email).all()[0]['value'] # for user in users: # value = user['value'] # user_list.append(value) return users
def retrieveAll(): client = ConnectDB().connect() tag_list = [] tag_list_response = client.list('tags').all() for tag_res in tag_list_response: value = tag_res['value'] tag_list.append(value) return tag_list
def retrieveAll(): client = ConnectDB().connect() vote_list =[] vote_list_response = client.list('votes').all() for vote_res in vote_list_response: value = vote_res['value'] vote_list.append(value) return vote_list
def retrieveDescendent(self, _id): conn = ConnectDB().connect() node = conn.get("test_nodes", _id) _list = node["nodeChildren"] if not _list: _list = "This node has no child." for child in _list: findChild(child, _list, conn) return _list
def retrieveById(self, _id): """ Retrieve a node by its ID :param _id: node ID :return: node object """ conn = ConnectDB().connect() node = conn.get('test_nodes', _id) print node.status_code return node
def update(_id, event): """ Update an event :param event: :return: """ print 'update' db_conn = ConnectDB().connect() update_result = db_conn.put('node_events', _id, event) event_key = update_result.key return event_key
def retrieveAll(self): """ Retrieve all the nodes from database :return: array of all the nodes """ conn = ConnectDB().connect() nodes = conn.list('test_nodes').all() list = [] for node in nodes: list.append(node['value']) return list
def retrieveChild(self, _id): """ Retrieve all direct children nodes of this node :param _id: node ID :return: all the children nodes of this node """ conn = ConnectDB().connect() node = conn.get('test_nodes', _id) _list = node['nodeChildren'] if not _list: _list = 'This node has no child' return _list
def downvoteNode(self, node_id, user_id): conn = ConnectDB().connect() node = conn.get("test_nodes", node_id) nodeVotes = node["nodeVotes"] nodeStatus = int(node["nodeStatus"]) - 1 vote = {"_id": str(user_id), "type": "-1"} assert isinstance(nodeVotes, list) nodeVotes.append(vote) patch = Patch() patch.replace("nodeStatus", str(nodeStatus)).replace("nodeVotes", nodeVotes) conn.patch("test_nodes", node_id, patch) new_node = conn.get("test_nodes", "node_id") return new_node
def retrieve_by_id(_id): """ Retrieve an event by its id :param _id: event id :return: event object """ print 'retrieve by event id' db_conn = ConnectDB().connect() event = db_conn.get('node_events', _id) print event.status_code return event
def retrieve_all(): """ Retrieve all the events from database :return: json array of events """ print 'retrieve all' db_conn = ConnectDB().connect() events = db_conn.list('node_events').all() events_list = [] for event in events: events_list.append(event['value']) return events_list
def retrieveDescendant(self, _id): """ Retrieve all the descendant nodes of this node :param _id: node ID :return: descendant nodes of the node """ conn = ConnectDB().connect() node = conn.get('test_nodes', _id) _list = node['nodeChildren'] if not _list: _list = 'This node has no descendant node.' for child in _list: findChild(child, _list, conn) return _list
def delete(add_key): add = Add.retrieveById(add_key) if None == add: result = {"result": "failed", "message": "add doesn't exist"} return result client = ConnectDB().connect() response = client.delete('adds', add_key) add = Add.retrieveById(add_key) if None == add: result = {"result": "success", "message": "success"} return result else: reason = response.reason result = {"result": "failed", "message": reason} return result
def delete(user_key): user = User.retrieveById(user_key) if None == user: result = {"result": "failed", "message": "user doesn't exist"} return result client = ConnectDB().connect() response = client.delete('users', user_key) user = User.retrieveById(user_key) if None == user: result = {"result": "success", "message": "success"} return result else: reason = response.reason result = {"result": "failed", "message": reason} return result
def delete(tag_key): tag = Tag.retrieveById(tag_key) if None == tag: result = {"result": "failed", "message": "tag doesn't exist"} return result client = ConnectDB().connect() response = client.delete('tags', tag_key) tag = Tag.retrieveById(tag_key) if None == tag: result = {"result": "success", "message": "success"} return result else: reason = response.reason result = {"result": "failed", "message": reason} return result
def update(add_key, add): client = ConnectDB().connect() response = client.put('adds', add_key, { "_id": add_key, "node": add.node, "addAt": add.addAt }) status = response.status_code reason = response.reason if status == 201: result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def delete(vote_key): vote = Vote.retrieveById(vote_key) if None == vote: result = {"result": "failed", "message": "vote doesn't exist"} return result client = ConnectDB().connect() response = client.delete('votes', vote_key) vote = Vote.retrieveById(vote_key) if None == vote: result = {"result": "success", "message": "success"} return result else: reason = response.reason result = {"result": "failed", "message": reason} return result
def update(user_key, user): client = ConnectDB().connect() response = client.put('users', user_key, { "_id": user_key, "given_name": user.given_name, "surname": user.surname, "email": user.email }) status = response.status_code reason = response.reason if status == 201: result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def update(tag_key, tag): client = ConnectDB().connect() response = client.put('tags', tag_key, { "_id": tag_key, "tagName": tag.tagName, "tagDescription": tag.tagDescription, "createdBy": tag.createdBy, "createdAt": tag.createdAt }) status = response.status_code reason = response.reason if status == 201: result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def create(self): client = ConnectDB().connect() response = client.post('adds', { "node": self.node, "addAt": self.addAt }) status = response.status_code reason = response.reason if status == 201: patch = Patch() patch.add("_id", response.key) client.patch('adds', response.key, patch) result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def update(user_key, user): client = ConnectDB().connect() response = client.put('users', user_key, { "_id": user_key, "username": user.username, "loginToken": user.loginToken, "email": user.email, "intro": user.intro, "registerAt": user.registerAt }) status = response.status_code reason = response.reason if status == 201: result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def update(vote_key, vote): client = ConnectDB().connect() response = client.put('votes', vote_key, { "_id": vote_key, "type": vote.type, "description": vote.description, "voteUser": vote.voteUser, "voteAt": vote.voteAt, "voteOnNode": vote.voteOnNode }) status = response.status_code reason = response.reason if status == 201: result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def create(self): client = ConnectDB().connect() response = client.post('users', { "given_name": self.given_name, "surname": self.surname, "email": self.email }) status = response.status_code reason = response.reason if status == 201: patch = Patch() patch.add("_id", response.key) client.patch('users', response.key, patch) result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def update(vote_key, vote): client = ConnectDB().connect() response = client.put( 'votes', vote_key, { "_id": vote_key, "type": vote.type, "description": vote.description, "voteUser": vote.voteUser, "voteAt": vote.voteAt, "voteOnNode": vote.voteOnNode }) status = response.status_code reason = response.reason if status == 201: result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def create(self): client = ConnectDB().connect() response = client.post('tags', { "tagName": self.tagName, "tagDescription": self.tagDescription, "createdBy": self.createdBy, "createdAt": self.createdAt }) status = response.status_code reason = response.reason if status == 201: patch = Patch() patch.add("_id", response.key) client.patch('tags', response.key, patch) result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def create(self): client = ConnectDB().connect() response = client.post('votes', { "type": self.type, "description": self.description, "voteUser": self.voteUser, "voteAt": self.voteAt, "voteOnNode": self.voteOnNode }) status = response.status_code reason = response.reason if status == 201: patch = Patch() patch.add("_id", response.key) client.patch('votes', response.key, patch) result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def create(self): client = ConnectDB().connect() response = client.post('users', { "username": self.username, "loginToken": self.loginToken, "email": self.email, "intro": self.intro, "registerAt": self.registerAt }) status = response.status_code reason = response.reason if status == 201: patch = Patch() patch.add("_id", response.key) client.patch('users', response.key, patch) result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def create(self, nodeDisplay, nodeDescription, nodeTags, nodeParents, nodeChildren, nodeVotes, nodeStatus, userId): """ Create a new node The check of node data has been completed by service :param nodeDisplay: display name of a node :param nodeDescription: description of a node :param nodeTags: tags of a node :param nodeParents: parent node ID of this node :param nodeChildren: children nodes (if already has) :param nodeVotes: votes on node :param nodeStatus: status of node :param userId: author identification :return: a key of the node as an identification """ current_time = datetime.datetime.now() conn = ConnectDB().connect() node = conn.post( 'test_nodes', { "nodeDisplay": nodeDisplay, "nodeDescription": nodeDescription, "nodeTags": nodeTags, "nodeParents": nodeParents, "nodeChildren": nodeChildren, "nodeVotes": nodeVotes, # This can be replaced by invoke methods in vote class "nodeStatus": str(nodeStatus), "nodeCreateAt": str(current_time), "userId": userId }) patch = Patch() nodeKey = node.key patch.add("_id", node.key) conn.patch('test_nodes', node.key, patch) print 'nodeKey is ' + node.key return nodeKey
def create(event): """ Create a new event object :param event: :return: """ print 'create' db_conn = ConnectDB().connect() current_time = datetime.datetime.now() # create_at event['create_at'] = str(current_time) event['status'] = data_checker.EVENT_UNREAD event_create = db_conn.post('node_events', event) # append a key as ID to event patch = Patch() event_key = event_create.key patch.add("_id", event_key) db_conn.patch('node_events', event_key, patch) print 'event_key=' + event_key return event_key
def create(self): client = ConnectDB().connect() response = client.post( 'votes', { "type": self.type, "description": self.description, "voteUser": self.voteUser, "voteAt": self.voteAt, "voteOnNode": self.voteOnNode }) status = response.status_code reason = response.reason if status == 201: patch = Patch() patch.add("_id", response.key) client.patch('votes', response.key, patch) result = {"result": "success", "message": reason} return result else: result = {"result": "failed", "message": reason} return result
def downvoteNode(self, node_id, user_id, comment): """ Voting down on a node :param node_id: ID of node :param user_id: ID of user (not required) :param comment: Comment on the vote :return: node object after voting """ conn = ConnectDB().connect() node = conn.get('test_nodes', node_id) nodeVotes = node['nodeVotes'] nodeStatus = int(node['nodeStatus']) - 1 vote = { 'userId': str(user_id), 'type': '-1', 'voteDate': str(datetime.datetime.now()), 'comment': comment } assert isinstance(nodeVotes, list) nodeVotes.append(vote) patch = Patch() patch.replace('nodeStatus', str(nodeStatus)).replace('nodeVotes', nodeVotes) conn.patch('test_nodes', node_id, patch) new_node = conn.get('test_nodes', node_id) return new_node
def update(self, _id, nodeDisplay, nodeDescription, nodeTags, nodeParents, nodeChildren, nodeVotes, nodeStatus, nodeCreateAt, userId): """ Update an existing node in database :param _id: node ID :param nodeDisplay: display name of a node :param nodeDescription: description :param nodeTags: tags of the node :param nodeParents: parent of the node :param nodeChildren: children nodes of the node :param nodeVotes: votes on the node :param nodeStatus: node status :param nodeCreateAt: node creation time :param userId: user identification :return: the key (ID) of the node """ conn = ConnectDB().connect() update_result = conn.put( 'test_nodes', _id, { "nodeDisplay": nodeDisplay, "nodeDescription": nodeDescription, "nodeTags": nodeTags, "nodeParents": nodeParents, "nodeChildren": nodeChildren, "nodeVotes": nodeVotes, # This can be replaced by invoke methods in vote class "nodeStatus": str(nodeStatus), "nodeCreateAt": nodeCreateAt, "userId": userId, "_id": _id }) node_key = update_result.key return node_key
def retrieve_numb_vote_by_node_id(node_key, vote_type): client = ConnectDB().connect() vote_list = client.search('votes', 'voteOnNode:'+node_key + ' AND type:'+vote_type).all() return len(vote_list)
def retrieve_numb_vote_by_node_id(node_key, vote_type): client = ConnectDB().connect() vote_list = client.search( 'votes', 'voteOnNode:' + node_key + ' AND type:' + vote_type).all() return len(vote_list)
class GenericOperation(object): client = ConnectDB().connect() def __init__(self, obj): self.operate_obj = obj
def delete(self, _id): conn = ConnectDB().connect() conn.delete("test_nodes", _id)
def retrieveById(self, _id): conn = ConnectDB().connect() node = conn.get("test_nodes", _id) return node