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 post(self): if not self.current_user: self.redirect("/login") return self.title = self.get_argument("title") self.url_cn = self.get_argument("url_cn", "") self.url_en = self.get_argument("url_en", "") self.content = self.get_argument("content", "") self.item_id = self.get_argument("id") self.user_id = self.current_user.get("user_id", u"").encode("utf8") if (self.url_en or self.url_cn) or self.content: data = { "title": self.title, "content": self.content, "url_en": self.url_en, "url_cn": self.url_cn, } entity = nomagic._get_entity_by_id(self.item_id) entity.update(data) nomagic._update_entity_by_id(self.item_id, entity) self.redirect("/item?id=%s" % self.item_id) else: self.redirect("/edit_item?id=%s" % self.item_id)
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_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 unlike(user_id, entity_id): entity = nomagic._get_entity_by_id(entity_id) likes = entity.get("likes", []) if user_id in likes: likes.remove(user_id) entity["likes"] = likes nomagic._update_entity_by_id(entity_id, entity) return likes
def like(user_id, entity_id): entity = nomagic._get_entity_by_id(entity_id) likes = entity.get("likes", []) if user_id not in likes: likes.append(user_id) entity["likes"] = likes nomagic._update_entity_by_id(entity_id, entity) return likes
def post(self): if not self.current_user: self.redirect("/login") return comment_id = self.get_argument("id").encode("utf8") user_id = self.current_user["user_id"].encode("utf8") content = self.get_argument("content").encode("utf8") data = {"content": content} entity = nomagic._get_entity_by_id(comment_id) entity.update(data) nomagic._update_entity_by_id(comment_id, entity) self.redirect("/edit_comment?id=%s" % comment_id)
def disconnect_entities(entity_id1, entity_connection_name1, entity_id2, entity_connection_name2): entity1, entity2 = nomagic._get_entities_by_ids([entity_id1, entity_id2]) entity_data1, entity_data2 = entity1[1], entity2[1] entity_connection = set(entity_data1.get(entity_connection_name1, [])) if entity_id2 in entity_connection: entity_connection.remove(entity_id2) entity_data1[entity_connection_name1] = list(entity_connection) nomagic._update_entity_by_id(entity_id1, entity_data1) entity_connection = set(entity_data2.get(entity_connection_name2, [])) if entity_id1 in entity_connection: entity_connection.remove(entity_id1) entity_data2[entity_connection_name2] = list(entity_connection) nomagic._update_entity_by_id(entity_id2, entity_data2)
def disconnect_index_and_entity(table_index, index_name, index_connection_name, entity_id, entity_connection_name): index = conn.get("SELECT * FROM %s WHERE name = %s" % (table_index, "%s"), index_name) index_data = nomagic._unpack(index["data"] or "{}") index_connection = set(index_data.get(index_connection_name, [])) if entity_id in index_connection: index_connection.remove(entity_id) index_data[index_connection_name] = list(index_connection) index_data_updated = nomagic._pack(index_data) conn.execute("UPDATE %s SET data = %s WHERE name = %s" % (table_index, "%s", "%s"), index_data_updated, index_name) entity = nomagic._get_entity_by_id(entity_id) entity_connection = set(entity.get(entity_connection_name, [])) if index_name in entity_connection: entity_connection.remove(index_name) entity[entity_connection_name] = list(entity_connection) nomagic._update_entity_by_id(entity_id, entity)
def disconnect_index_and_entity(table_index, index_name, index_connection_name, entity_id, entity_connection_name): index = conn.get("SELECT * FROM %s WHERE name = %s" % (table_index, "%s"), index_name) index_data = nomagic._unpack(index["data"] or "{}") index_connection = set(index_data.get(index_connection_name, [])) if entity_id in index_connection: index_connection.remove(entity_id) index_data[index_connection_name] = list(index_connection) index_data_updated = nomagic._pack(index_data) conn.execute( "UPDATE %s SET data = %s WHERE name = %s" % (table_index, "%s", "%s"), index_data_updated, index_name) entity = nomagic._get_entity_by_id(entity_id) entity_connection = set(entity.get(entity_connection_name, [])) if index_name in entity_connection: entity_connection.remove(index_name) entity[entity_connection_name] = list(entity_connection) nomagic._update_entity_by_id(entity_id, entity)
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))