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 self.current_user: user_id = self.current_user["user_id"].encode("utf8") self.user = nomagic._get_entity_by_id(user_id) post_data = {} name = self.get_argument("name", None) if name: post_data["name"] = name receive_daily_email = bool(self.get_argument("receive_daily_email", False)) if self.user.get("receive_daily_email", True) != receive_daily_email: print receive_daily_email post_data["receive_daily_email"] = receive_daily_email if post_data: nomagic.auth.update_user(user_id, post_data) password0 = self.get_argument("password0", None) password1 = self.get_argument("password1", None) password2 = self.get_argument("password2", None) if password0 is not None and password1 is not None and password1 == password2: post_data = {} post_data["password0"] = password0 post_data["password1"] = password1 nomagic.auth.update_user(user_id, post_data) self.redirect("/setting?status=password_updated") return self.redirect("/setting")
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 unfollow_users(user_id, friend_ids): user = nomagic._get_entity_by_id(user_id) following = user.get("following", []) for friend_id in friend_ids: if friend_id in following: friend = nomagic._get_entity_by_id(friend_id) followed = friend.get("followed", []) if user_id in followed: followed.remove(user_id) friend["followed"] = followed auth.update_user(friend_id, friend) following.remove(friend_id) user["following"] = following auth.update_user(user_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 get(self): user = {} if self.current_user: user_id = self.current_user["user_id"].encode("utf8") user = nomagic._get_entity_by_id(user_id) self.finish(user)
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 post(self): if self.current_user: user_id = self.current_user["user_id"].encode("utf8") self.user = nomagic._get_entity_by_id(user_id) post_data = {} name = self.get_argument("name", None) if name: post_data["name"] = name receive_daily_email = bool( self.get_argument("receive_daily_email", False)) if self.user.get("receive_daily_email", True) != receive_daily_email: print receive_daily_email post_data["receive_daily_email"] = receive_daily_email if post_data: nomagic.auth.update_user(user_id, post_data) password0 = self.get_argument("password0", None) password1 = self.get_argument("password1", None) password2 = self.get_argument("password2", None) if password0 is not None and password1 is not None and password1 == password2: post_data = {} post_data["password0"] = password0 post_data["password1"] = password1 nomagic.auth.update_user(user_id, post_data) self.redirect("/setting?status=password_updated") return self.redirect("/setting")
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 get(self): self.set_header("Cache-Control", "max-age=0") 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") parent = nomagic._get_entity_by_id(comment_id) assert parent["type"] in ["comment", "status"] parent["like"] = user_id in parent["likes"] parent["like_count"] = len(parent["likes"]) parent["content"] = markdown2.markdown(parent["content"], safe_mode=True) parent["edit_content"] = "" parent["user"] = nomagic._get_entity_by_id(user_id) self.render("../template/html_comment.html", **parent)
def check_user(login, password): login_info = conn.get("SELECT entity_id FROM index_login WHERE login = %s", login) if login_info: user_id = login_info["entity_id"] user = nomagic._get_entity_by_id(user_id) if user["password"] == hashlib.sha1(password + user.get("salt", "")).hexdigest(): return (user_id, user) return (None, None)
def get(self): self.set_header("Cache-Control", "max-age=0") if not self.current_user: self.redirect("/login") return user_id = self.current_user["user_id"].encode("utf8") self.user = nomagic._get_entity_by_id(user_id) self.render('../template/setting.html')
def get_news_by_id(activity_id): activity = nomagic._get_entity_by_id(activity_id) activity["id"] = activity_id comments, user_ids = get_comments(activity) return dict(activity, id = activity_id, like_count = len(activity.get("likes", [])), comment_count = 0, #len(activity.get("comment_ids", [])), comments = comments), user_ids
def get(self): self.set_header("Cache-Control", "max-age=0") if not self.current_user: self.redirect("/login") return user_id = self.current_user["user_id"].encode("utf8") self.item_id = self.get_argument("id") item = nomagic._get_entity_by_id(self.item_id) if item['user_id'] != user_id: raise tornado.web.HTTPError(401, "Not allow to edit") return self.title = item["title"] self.url_cn = item["url_cn"] self.url_en = item["url_en"] self.content = item["content"] self.user = nomagic._get_entity_by_id(user_id) self.render("../template/html_submit.html")
def get_news_by_id(activity_id): activity = nomagic._get_entity_by_id(activity_id) activity["id"] = activity_id comments, user_ids = get_comments(activity) return dict( activity, id=activity_id, like_count=len(activity.get("likes", [])), comment_count=0, #len(activity.get("comment_ids", [])), comments=comments), user_ids
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 get(self): user_id = self.get_argument("user_id").encode("utf8") email_verify_code = self.get_argument("verify_code") self.user = nomagic._get_entity_by_id(user_id) self.verified = False if self.user.get("email_verify_code") == email_verify_code: result = nomagic.auth.update_user(user_id, {"email_verified": True}) self.verified = True self.set_header("Cache-Control", "max-age=0") #self.write("done! " + str(self.verified)) self.render('../template/verify_email.html')
def get(self): self.set_header("Cache-Control", "max-age=0") if not self.current_user: self.redirect("/login") return self.title = self.get_argument("title", "") self.url_cn = self.get_argument("url", "") self.url_en = "" self.content = "" user_id = self.current_user["user_id"].encode("utf8") self.user = nomagic._get_entity_by_id(user_id) self.render("../template/html_submit.html")
def follow_users(user_id, friend_ids): user = nomagic._get_entity_by_id(user_id) following = user.get("following", []) suggested_friend_ids = user.get("suggested_friend_ids", []) changed = False for friend_id in friend_ids: if friend_id not in following: friend = nomagic._get_entity_by_id(friend_id) followed = friend.get("followed", []) if user_id not in followed: followed.append(user_id) friend["followed"] = followed auth.update_user(friend_id, friend) following.append(friend_id) changed = True if friend_id in suggested_friend_ids: suggested_friend_ids.remove(friend_id) changed = True if changed: user["following"] = following user["suggested_friend_ids"] = suggested_friend_ids auth.update_user(user_id, user)
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 get_news_by_id(self, activity_id): activity = nomagic._get_entity_by_id(activity_id) activity["id"] = activity_id comments, user_ids = nomagic.feeds.get_comments(activity) self.users = dict(nomagic._get_entities_by_ids(user_ids)) data = dict(activity, id = activity_id, like_count = len(activity.get("likes", [])), like = False, user = self.users[activity["user_id"]], comment_count = 0, #len(activity.get("comment_ids", [])), comments = self.get_comments(comments)) return self.topic_temp.generate(**data)
def get_news_by_id(self, activity_id): activity = nomagic._get_entity_by_id(activity_id) activity["id"] = activity_id comments, user_ids = nomagic.feeds.get_comments(activity) self.users = dict(nomagic._get_entities_by_ids(user_ids)) data = dict( activity, id=activity_id, like_count=len(activity.get("likes", [])), like=False, user=self.users[activity["user_id"]], comment_count=0, #len(activity.get("comment_ids", [])), comments=self.get_comments(comments)) return self.topic_temp.generate(**data)
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 _on_auth(self, google_user): if not google_user: raise tornado.web.HTTPError(500, "Google auth failed") current_user = conn.get("SELECT * FROM index_login WHERE login = %s", google_user["email"]) if current_user is None: user_id, user = nomagic.create_user({"email":google_user["email"], "name":google_user["name"], "google": google_user}) else: user_id = current_user["entity_id"] user = nomagic._get_entity_by_id(user_id) if "google" not in user: raise tornado.web.HTTPError(500, "Can not login with Google") self.set_secure_cookie("user", tornado.escape.json_encode({"user_id": user_id})) self.redirect(self.redirect_url)
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 get_item_by_id(self, item_id): item = nomagic._get_entity_by_id(item_id) comments, user_ids = nomagic.feeds.get_comments(item) user_ids.add(item["user_id"]) self.users = dict(nomagic._get_entities_by_ids(user_ids)) url = item.get("url_cn") if item.get("url_cn") else item.get("url_en") #url = url if url else "/item?id=%s" % item.get("id") data = dict(item, id = item_id, like_count = len(item.get("likes", [])), like = self.user_id in set(item.get("likes", [])) if self.current_user else False, user = self.users[item["user_id"]], comment_count = 0, #len(item.get("comment_ids", [])), comments = self.get_comments(comments), url = url, content = markdown2.markdown(item["content"], safe_mode=True), _ = self.locale.translate, handler = self) return self.topic_temp.generate(**data)
def get_item_by_id(self, item_id): item = nomagic._get_entity_by_id(item_id) comments, user_ids = nomagic.feeds.get_comments(item) user_ids.add(item["user_id"]) self.users = dict(nomagic._get_entities_by_ids(user_ids)) url = item.get("url_cn") if item.get("url_cn") else item.get("url_en") #url = url if url else "/item?id=%s" % item.get("id") data = dict( item, id=item_id, like_count=len(item.get("likes", [])), like=self.user_id in set(item.get("likes", [])) if self.current_user else False, user=self.users[item["user_id"]], comment_count=0, #len(item.get("comment_ids", [])), comments=self.get_comments(comments), url=url, content=markdown2.markdown(item["content"], safe_mode=True), _=self.locale.translate, handler=self) return self.topic_temp.generate(**data)
def get_user_by_login(login): index_login = conn.get("SELECT * FROM index_login WHERE login = %s", login) entity_id = index_login["entity_id"] return nomagic._get_entity_by_id(entity_id)
#comment["like_count"] = len(comment.get("likes", [])) #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)