def new_comment(user_id, entity_id, data): data["type"] = "comment" data["likes"] = [] data["user_id"] = user_id data["activity_id"] = entity_id data["datetime"] = datetime.datetime.now().isoformat() data["comment_ids"] = [] #content valid assert data.get("content") new_comment_id = nomagic._new_key() assert nomagic._node(new_comment_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_comment_id, nomagic._pack(data)) entity = nomagic._get_entity_by_id(entity_id) comment_ids = entity.get("comment_ids", []) comment_ids.append(new_comment_id) entity["comment_ids"] = comment_ids nomagic._update_entity_by_id(entity_id, entity) return comment_ids, dict(data, id=new_comment_id, like_count=0, like=False, user=nomagic._get_entity_by_id(user_id))
def create_user(user): email = user["email"] login = conn.get("SELECT * FROM index_login WHERE login = %s", email) assert not login user["type"] = "user" user["name"] = user.get("name", "") user["salt"] = "".join( random.choice(string.ascii_uppercase + string.digits) for x in range(10)) user["password"] = hashlib.sha1(user["password"] + user["salt"]).hexdigest() user["title"] = "" user["department"] = "" user["locatiion"] = "" user["mobile"] = "" user["tel"] = "" user["about"] = "" user["profile_img"] = "" user["datetime"] = datetime.datetime.now().isoformat() new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(user)) assert rowcount #update indexes: email assert "@" in email rowcount = conn.execute_rowcount( "INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id) assert rowcount return (new_id, user)
def update_user(user_id, data): #valid name user = nomagic._get_entity_by_id(user_id) user_json1 = nomagic._pack(user) result = {} if "password0" in data and "password1" in data and data[ "password0"] != data["password1"] and data["password1"] != "": #normal update password with old password0 and new password1 if user["password"] == hashlib.sha1(data["password0"] + user.get("salt", "")).hexdigest(): user["password"] = hashlib.sha1(data["password1"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password0"] del data["password1"] elif "password" in data and data["password"] != "": #force update password user["password"] = hashlib.sha1(data["password"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password"] if user: user.update(data) user_json2 = nomagic._pack(user) if user_json1 != user_json2: assert nomagic._node(user_id).execute_rowcount( "UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id)) return result
def new_status(user_id, data): now = datetime.datetime.now() data["type"] = "status" data["user_id"] = user_id data["datetime"] = now.isoformat() data["likes"] = [] data["comment_ids"] = [] assert data.get("content") new_id = nomagic._new_key() assert nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(data)) user = nomagic._get_entity_by_id(user_id) activity = user.get("activity", []) activity.append(new_id) user["activity"] = activity nomagic._update_entity_by_id(user_id, user) data["user"] = user data["like_count"] = 0 data["like"] = False data["comment_count"] = 0 assert conn.execute_rowcount( "INSERT INTO index_posts (user_id, entity_id) VALUES(%s, %s)", user_id, new_id) return new_id, data
def update_user(user_id, data): #valid name user = nomagic._get_entity_by_id(user_id) user_json1 = nomagic._pack(user) result = {} if "password0" in data and "password1" in data and data["password0"] != data["password1"] and data["password1"] != "": #normal update password with old password0 and new password1 if user["password"] == hashlib.sha1(data["password0"] + user.get("salt", "")).hexdigest(): user["password"] = hashlib.sha1(data["password1"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password0"] del data["password1"] elif "password" in data and data["password"] != "": #force update password user["password"] = hashlib.sha1(data["password"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password"] if user: user.update(data) user_json2 = nomagic._pack(user) if user_json1 != user_json2: assert nomagic._node(user_id).execute_rowcount("UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id)) return result
def create_user(user): email = user["email"] login = conn.get("SELECT * FROM index_login WHERE login = %s", email) assert not login user["type"] = "user" user["name"] = user.get("name", "") user["salt"] = "".join(random.choice(string.ascii_uppercase + string.digits) for x in range(10)) user["password"] = hashlib.sha1(user["password"] + user["salt"]).hexdigest() user["title"] = "" user["department"] = "" user["locatiion"] = "" user["mobile"] = "" user["tel"] = "" user["about"] = "" user["profile_img"] = "" user["datetime"] = datetime.datetime.now().isoformat() new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(user)) assert rowcount #update indexes: email assert "@" in email rowcount = conn.execute_rowcount("INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id) assert rowcount return (new_id, user)
def new_status(user_id, data): now = datetime.datetime.now() data["type"] = "status" data["user_id"] = user_id data["datetime"] = now.isoformat() data["likes"] = [] data["comment_ids"] = [] assert data.get("content") new_id = nomagic._new_key() assert nomagic._node(new_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(data)) user = nomagic._get_entity_by_id(user_id) activity = user.get("activity", []) activity.append(new_id) user["activity"] = activity nomagic._update_entity_by_id(user_id, user) data["user"] = user data["like_count"] = 0 data["like"] = False data["comment_count"] = 0 assert conn.execute_rowcount("INSERT INTO index_posts (user_id, entity_id) VALUES(%s, %s)", user_id, new_id) return new_id, data
def new_comment(user_id, entity_id, data): data["type"] = "comment" data["likes"] = [] data["user_id"] = user_id data["activity_id"] = entity_id data["datetime"] = datetime.datetime.now().isoformat() data["comment_ids"] = [] #content valid assert data.get("content") new_comment_id = nomagic._new_key() assert nomagic._node(new_comment_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_comment_id, nomagic._pack(data)) entity = nomagic._get_entity_by_id(entity_id) comment_ids = entity.get("comment_ids", []) comment_ids.append(new_comment_id) entity["comment_ids"] = comment_ids nomagic._update_entity_by_id(entity_id, entity) return comment_ids, dict(data, id=new_comment_id, like_count=0, like=False, user=nomagic._get_entity_by_id(user_id))
#comment["like"] = self.user_id in set(comment.get("likes", [])) if self.current_user else False #comment["comment_count"] = 0 #print comment["comments"] if comment.get("comments") else [] comment_ids.append(comment["id"]) comment_ids.extend(get_comments(comment["comments"]) if comment.get("comments") else []) return comment_ids if len(sys.argv) < 2: sys.exit() print sys.argv[1] activity_id = sys.argv[1] activity = nomagic._get_entity_by_id(activity_id) print activity assert activity.get("type") == "status" comments, user_ids = nomagic.feeds.get_comments(activity) comment_ids = get_comments(comments) print comment_ids # delete comment_ids for comment_id in comment_ids: nomagic._node(comment_id).execute_rowcount("DELETE FROM entities WHERE id = %s", comment_id) # delete activity_id nomagic._node(activity_id).execute_rowcount("DELETE FROM entities WHERE id = %s", activity_id) conn.execute_rowcount("DELETE FROM index_posts WHERE entity_id = %s", activity_id)