def post(self, topic_id, template_variables = {}): template_variables = {} # validate the fields form = CreateForm(self) if not form.validate(): self.get(topic_id, {"errors": form.errors}) return # continue while validate succeed topic_info = self.topic_model.get_topic_by_topic_id(topic_id) if(not topic_info["author_id"] == self.current_user["uid"]): template_variables["errors"] = {} template_variables["errors"]["invalid_permission"] = [u"没有权限修改该话题"] self.get(topic_id, template_variables) return update_topic_info = { "title": form.title.data, # "content": XssCleaner().strip(form.content.data), "content": form.content.data, "updated": time.strftime('%Y-%m-%d %H:%M:%S'), "last_touched": time.strftime('%Y-%m-%d %H:%M:%S'), } self.topic_model.update_topic_by_topic_id(topic_id, update_topic_info) # create @username notification for username in set(find_mentions(form.content.data)): mentioned_user = self.user_model.get_user_by_username(username) if not mentioned_user: continue if mentioned_user["uid"] == self.current_user["uid"]: continue if mentioned_user["uid"] == topic_info["author_id"]: continue self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 2, # 0: mention, 1: comment, 2: topic mention, 3: topic reply "involved_user_id": mentioned_user["uid"], "involved_post_id": topic_id, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) self.redirect("/t/%s" % topic_id)
def post(self, node = None, template_variables = {}): print "CreateHandler:post" template_variables = {} # validate the fields form = CreateForm(self) if not form.validate(): self.get(node_slug, {"errors": form.errors}) return # continue while validate succeed topic_info = { "author_id": self.current_user["uid"], "title": form.title.data, # "content": XssCleaner().strip(form.content.data), "content": form.content.data, "node_id": 0, "created": time.strftime('%Y-%m-%d %H:%M:%S'), "reply_count": 0, "last_touched": time.strftime('%Y-%m-%d %H:%M:%S'), } topic_id = self.topic_model.add_new_topic(topic_info) # create @username notification for username in set(find_mentions(form.content.data)): mentioned_user = self.user_model.get_user_by_username(username) if not mentioned_user: continue if mentioned_user["uid"] == self.current_user["uid"]: continue if mentioned_user["uid"] == topic_info["author_id"]: continue self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 2, # 0: mention, 1: comment, 2: topic mention, 3: topic reply "involved_user_id": mentioned_user["uid"], "involved_post_id": topic_id, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) self.redirect("/forum")
def post(self, template_variables = {}): template_variables = {} # validate the fields form = ReplyForm(self) if not form.validate(): self.get(form.tid.data, {"errors": form.errors}) return # continue while validate succeed topic_info = self.topic_model.get_topic_by_topic_id(form.tid.data) replied_info = self.reply_model.get_user_last_reply_by_topic_id(self.current_user["uid"], form.tid.data) if(not topic_info): template_variables["errors"] = {} template_variables["errors"]["invalid_topic_info"] = [u"要回复的帖子不存在"] self.get(form.tid.data, template_variables) return if(replied_info): last_replied_fingerprint = hashlib.sha1(str(replied_info.topic_id) + str(replied_info.author_id) + replied_info.content).hexdigest() new_replied_fingerprint = hashlib.sha1(str(form.tid.data) + str(self.current_user["uid"]) + form.content.data).hexdigest() if last_replied_fingerprint == new_replied_fingerprint: template_variables["errors"] = {} template_variables["errors"]["duplicated_reply"] = [u"回复重复提交"] self.get(form.tid.data, template_variables) return reply_info = { "author_id": self.current_user["uid"], "topic_id": form.tid.data, # "content": XssCleaner().strip(form.content.data), "content": form.content.data, "created": time.strftime('%Y-%m-%d %H:%M:%S'), } reply_id = self.reply_model.add_new_reply(reply_info) # update topic last_replied_by and last_replied_time self.topic_model.update_topic_by_topic_id(form.tid.data, { "last_replied_by": self.current_user["uid"], "last_replied_time": time.strftime('%Y-%m-%d %H:%M:%S'), "last_touched": time.strftime('%Y-%m-%d %H:%M:%S'), }) # create reply notification if not self.current_user["uid"] == topic_info["author_id"]: self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 1, # 0: mention, 1: reply "involved_user_id": topic_info["author_id"], "involved_topic_id": form.tid.data, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) # create @username notification for username in set(find_mentions(form.content.data)): mentioned_user = self.user_model.get_user_by_username(username) if not mentioned_user: continue if mentioned_user["uid"] == self.current_user["uid"]: continue if mentioned_user["uid"] == topic_info["author_id"]: continue self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 0, # 0: mention, 1: reply "involved_user_id": mentioned_user["uid"], "involved_topic_id": form.tid.data, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) # update reputation of topic author if not self.current_user["uid"] == topic_info["author_id"] and not replied_info: topic_time_diff = datetime.datetime.now() - topic_info["created"] reputation = topic_info["author_reputation"] or 0 reputation = reputation + 2 * math.log(self.current_user["reputation"] or 0 + topic_time_diff.days + 10, 10) self.user_model.set_user_base_info_by_uid(topic_info["author_id"], {"reputation": reputation}) # self.get(form.tid.data) self.redirect("/t/%s#reply%s" % (form.tid.data, topic_info["reply_count"] + 1))
def post(self, template_variables={}): template_variables = {} # validate the fields form = ReplyForm(self) if not form.validate(): self.get(form.tid.data, {"errors": form.errors}) return # continue while validate succeed topic_info = self.topic_model.get_topic_by_topic_id(form.tid.data) replied_info = self.reply_model.get_user_last_reply_by_topic_id( self.current_user["uid"], form.tid.data) if (not topic_info): template_variables["errors"] = {} template_variables["errors"]["invalid_topic_info"] = [u"要回复的帖子不存在"] self.get(form.tid.data, template_variables) return if (replied_info): last_replied_fingerprint = hashlib.sha1( str(replied_info.topic_id) + str(replied_info.author_id) + replied_info.content).hexdigest() new_replied_fingerprint = hashlib.sha1( str(form.tid.data) + str(self.current_user["uid"]) + form.content.data).hexdigest() if last_replied_fingerprint == new_replied_fingerprint: template_variables["errors"] = {} template_variables["errors"]["duplicated_reply"] = [u"回复重复提交"] self.get(form.tid.data, template_variables) return reply_info = { "author_id": self.current_user["uid"], "topic_id": form.tid.data, # "content": XssCleaner().strip(form.content.data), "content": form.content.data, "created": time.strftime('%Y-%m-%d %H:%M:%S'), } reply_id = self.reply_model.add_new_reply(reply_info) # update topic last_replied_by and last_replied_time self.topic_model.update_topic_by_topic_id( form.tid.data, { "last_replied_by": self.current_user["uid"], "last_replied_time": time.strftime('%Y-%m-%d %H:%M:%S'), "last_touched": time.strftime('%Y-%m-%d %H:%M:%S'), }) # create reply notification if not self.current_user["uid"] == topic_info["author_id"]: self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 1, # 0: mention, 1: reply "involved_user_id": topic_info["author_id"], "involved_topic_id": form.tid.data, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) # create @username notification for username in set(find_mentions(form.content.data)): mentioned_user = self.user_model.get_user_by_username(username) if not mentioned_user: continue if mentioned_user["uid"] == self.current_user["uid"]: continue if mentioned_user["uid"] == topic_info["author_id"]: continue self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 0, # 0: mention, 1: reply "involved_user_id": mentioned_user["uid"], "involved_topic_id": form.tid.data, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) # update reputation of topic author if not self.current_user["uid"] == topic_info[ "author_id"] and not replied_info: topic_time_diff = datetime.datetime.now() - topic_info["created"] reputation = topic_info["author_reputation"] or 0 reputation = reputation + 2 * math.log( self.current_user["reputation"] or 0 + topic_time_diff.days + 10, 10) self.user_model.set_user_base_info_by_uid( topic_info["author_id"], {"reputation": reputation}) # self.get(form.tid.data) self.redirect("/t/%s#reply%s" % (form.tid.data, topic_info["reply_count"] + 1))
class ViewHandler(BaseHandler): def get(self, topic_id, template_variables={}): user_info = self.current_user page = int(self.get_argument("p", "1")) user_info = self.get_current_user() template_variables["user_info"] = user_info if (user_info): template_variables["user_info"]["counter"] = { "topics": self.topic_model.get_user_all_topics_count(user_info["uid"]), "replies": self.reply_model.get_user_all_replies_count(user_info["uid"]), "favorites": self.favorite_model.get_user_favorite_count(user_info["uid"]), } template_variables[ "notifications_count"] = self.notification_model.get_user_unread_notification_count( user_info["uid"]) template_variables[ "topic_favorited"] = self.favorite_model.get_favorite_by_topic_id_and_owner_user_id( topic_id, user_info["uid"]) template_variables["gen_random"] = gen_random template_variables["topic"] = self.topic_model.get_topic_by_topic_id( topic_id) # check reply count and cal current_page if `p` not given reply_num = 106 reply_count = template_variables["topic"]["reply_count"] reply_last_page = (reply_count / reply_num + (reply_count % reply_num and 1)) or 1 page = int(self.get_argument("p", reply_last_page)) template_variables["reply_num"] = reply_num template_variables["current_page"] = page template_variables[ "replies"] = self.reply_model.get_all_replies_by_topic_id( topic_id, current_page=page, num=reply_num) template_variables["active_page"] = "topic" # update topic reply_count and hits self.topic_model.update_topic_by_topic_id( topic_id, { "reply_count": template_variables["replies"]["page"]["total"], "hits": (template_variables["topic"]["hits"] or 0) + 1, }) self.render("topic/view.html", **template_variables) @tornado.web.authenticated def post(self, template_variables={}): template_variables = {} # validate the fields form = ReplyForm(self) if not form.validate(): self.get(form.tid.data, {"errors": form.errors}) return # continue while validate succeed topic_info = self.topic_model.get_topic_by_topic_id(form.tid.data) replied_info = self.reply_model.get_user_last_reply_by_topic_id( self.current_user["uid"], form.tid.data) if (not topic_info): template_variables["errors"] = {} template_variables["errors"]["invalid_topic_info"] = [u"要回复的帖子不存在"] self.get(form.tid.data, template_variables) return if (replied_info): last_replied_fingerprint = hashlib.sha1( str(replied_info.topic_id) + str(replied_info.author_id) + replied_info.content).hexdigest() new_replied_fingerprint = hashlib.sha1( str(form.tid.data) + str(self.current_user["uid"]) + form.content.data).hexdigest() if last_replied_fingerprint == new_replied_fingerprint: template_variables["errors"] = {} template_variables["errors"]["duplicated_reply"] = [u"回复重复提交"] self.get(form.tid.data, template_variables) return reply_info = { "author_id": self.current_user["uid"], "topic_id": form.tid.data, # "content": XssCleaner().strip(form.content.data), "content": form.content.data, "created": time.strftime('%Y-%m-%d %H:%M:%S'), } reply_id = self.reply_model.add_new_reply(reply_info) # update topic last_replied_by and last_replied_time self.topic_model.update_topic_by_topic_id( form.tid.data, { "last_replied_by": self.current_user["uid"], "last_replied_time": time.strftime('%Y-%m-%d %H:%M:%S'), "last_touched": time.strftime('%Y-%m-%d %H:%M:%S'), }) # create reply notification if not self.current_user["uid"] == topic_info["author_id"]: self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 1, # 0: mention, 1: reply "involved_user_id": topic_info["author_id"], "involved_topic_id": form.tid.data, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) user_info = self.user_model.get_user_by_uid( self.current_user["uid"]) template_variables["trigger_user"] = user_info['username'] topic_info = self.topic_model.get_topic_by_topic_id(form.tid.data) template_variables["involved_topic"] = topic_info['title'] reply_email = self.user_model.get_user_by_uid( topic_info["author_id"]) mail_content = self.render_string("user/reply_mail.html", **template_variables) try: reply(mail_content, reply_email['email']) except Exception, e: print(e) return false # create @username notification for username in set(find_mentions(form.content.data)): mentioned_user = self.user_model.get_user_by_username(username) if not mentioned_user: continue if mentioned_user["uid"] == self.current_user["uid"]: continue if mentioned_user["uid"] == topic_info["author_id"]: continue self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 0, # 0: mention, 1: reply "involved_user_id": mentioned_user["uid"], "involved_topic_id": form.tid.data, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) user_info = self.user_model.get_user_by_uid( self.current_user["uid"]) template_variables["trigger_user"] = user_info['username'] topic_info = self.topic_model.get_topic_by_topic_id(form.tid.data) template_variables["involved_topic"] = topic_info['title'] notice_email = self.user_model.get_user_by_uid( mentioned_user["uid"]) mail_content = self.render_string("user/notice_mail.html", **template_variables) try: notice(mail_content, notice_email['email']) except Exception, e: print(e) return false
def post(self, template_variables = {}): tab = self.get_argument('tab', "post") template_variables = {} user_info = self.current_user if (tab=="post"): # validate the fields form = PostForm(self) if not form.validate(): self.get({"errors": form.errors}) return # continue while validate succeed video_link = form.link.data video_id = find_video_id_from_url(video_link) json_link = "http://v.youku.com/player/getPlayList/VideoIDS/"+video_id+"/timezone/+08/version/5/source/out?password=&ran=2513&n=3" video_json = json.load(urllib2.urlopen(json_link)) video_logo = video_json[u'data'][0][u'logo'] video_title = video_json[u'data'][0][u'title'] video_flash = "http://player.youku.com/player.php/sid/"+video_id+"/v.swf" print video_title video_info = { "source": "youku", "flash": video_flash, "link": video_link, "title": video_title, "thumb": video_logo, } vid = self.video_model.add_new_video(video_info) channel_name = form.channel.data channel = self.channel_model.get_channel_by_name(channel_name = channel_name) post_info = { "author_id": self.current_user["uid"], "channel_id": channel["id"], "nav_id": channel["nav_id"], "video_id": vid, "intro": form.intro.data, "created": time.strftime('%Y-%m-%d %H:%M:%S'), } post_id = self.post_model.add_new_post(post_info) self.channel_model.update_channel_info_by_channel_id(channel.id, {"plus":channel.plus+3, "posts": channel.posts+1}) self.user_model.update_user_info_by_user_id(user_info["uid"], {"plus":user_info["plus"]+3}) # create @username follow for username in set(find_mentions(form.intro.data)): print username mentioned_user = self.user_model.get_user_by_username(username) if not mentioned_user: continue if mentioned_user["uid"] == self.current_user["uid"]: continue if mentioned_user["uid"] == post_info["author_id"]: continue self.follow_model.add_new_follow({ "user_id": mentioned_user.uid, "post_id": post_id, "created": time.strftime('%Y-%m-%d %H:%M:%S'), }) self.redirect("/") else: form = ChannelForm2(self) if not form.validate(): self.get({"errors": form.errors}) return nav = self.nav_model.get_nav_by_nav_title(form.nav.data) channel_info = { "name": form.name.data, "intro": form.intro.data, "nav_id": nav["id"], "plus": 0, "followers": 0, "posts": 0, "author_id": self.current_user["uid"], "created": time.strftime('%Y-%m-%d %H:%M:%S'), } self.channel_model.add_new_channel(channel_info) channel = self.channel_model.get_channel_by_name(channel_name = channel_info["name"]) follow_info = { "user_id": self.current_user["uid"], "channel_id": channel["id"], "created": time.strftime('%Y-%m-%d %H:%M:%S'), } self.follow_model.add_new_follow(follow_info) self.redirect("/c/"+str(channel["id"]))
def post(self, template_variables = {}): template_variables = {} # validate the fields form = ReplyForm(self) if not form.validate(): self.get(form.tid.data, {"errors": form.errors}) return # continue while validate succeed topic_info = self.topic_model.get_topic_by_topic_id(form.tid.data) replied_info = self.reply_model.get_user_reply_by_topic_id(self.current_user["uid"], form.tid.data) if(not topic_info): template_variables["errors"] = {} template_variables["errors"]["invalid_topic_info"] = [u"要回复的帖子不存在"] self.get(template_variables) return reply_info = { "author_id": self.current_user["uid"], "topic_id": form.tid.data, # "content": XssCleaner().strip(form.content.data), "content": form.content.data, "created": time.strftime('%Y-%m-%d %H:%M:%S'), } reply_id = self.reply_model.add_new_reply(reply_info) # update topic last_replied_by and last_replied_time self.topic_model.update_topic_by_topic_id(form.tid.data, { "last_replied_by": self.current_user["uid"], "last_replied_time": time.strftime('%Y-%m-%d %H:%M:%S'), "last_touched": time.strftime('%Y-%m-%d %H:%M:%S'), }) # create reply notification if not self.current_user["uid"] == topic_info["author_id"]: self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 3, # 0: mention, 1: comment, 2: topic mention, 3: topic reply "involved_user_id": topic_info["author_id"], "involved_post_id": form.tid.data, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) # create @username notification for username in set(find_mentions(form.content.data)): print username mentioned_user = self.user_model.get_user_by_username(username) if not mentioned_user: continue if mentioned_user["uid"] == self.current_user["uid"]: continue if mentioned_user["uid"] == topic_info["author_id"]: continue self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 2, # 0: mention, 1: comment, 2: topic mention, 3: topic reply "involved_user_id": mentioned_user["uid"], "involved_post_id": form.tid.data, "content": form.content.data, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) # self.get(form.tid.data) self.redirect("/t/%s#reply%s" % (form.tid.data, topic_info["reply_count"] + 1))
def post(self, post_id, template_variables = {}): user_info = self.current_user data = json.loads(self.request.body) comment_content = data["comment_content"] if(user_info): comment_info = { "author_id": user_info["uid"], "post_id": post_id, "content": comment_content, "created": time.strftime('%Y-%m-%d %H:%M:%S'), } comment_id = self.comment_model.add_new_comment(comment_info) post = self.post_model.get_post_by_post_id(user_info["uid"], post_id) self.post_model.update_post_by_post_id(post_id, {"last_comment": comment_id, "comment_count": post.comment_count+1,}) self.write(lib.jsonp.print_JSON({ "success": 1, "message": "successed", })) # create reply notification if not self.current_user["uid"] == post.author_id: self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 1, # 0: mention, 1: reply "involved_user_id": post.author_id, "involved_post_id": post.id, "involved_comment_id": comment_id, "content": comment_content, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) # create @username notification for username in set(find_mentions(comment_content)): print username mentioned_user = self.user_model.get_user_by_username(username) if not mentioned_user: continue if mentioned_user["uid"] == self.current_user["uid"]: continue if mentioned_user["uid"] == post.author_id: continue self.notification_model.add_new_notification({ "trigger_user_id": self.current_user["uid"], "involved_type": 0, # 0: mention, 1: reply "involved_user_id": mentioned_user["uid"], "involved_post_id": post.id, "involved_comment_id": comment_id, "content": comment_content, "status": 0, "occurrence_time": time.strftime('%Y-%m-%d %H:%M:%S'), }) channel = self.channel_model.get_channel_by_channel_id(channel_id = post.channel_id) self.channel_model.update_channel_info_by_channel_id(channel.id, {"plus":channel.plus+1}) user = self.user_model.get_user_by_uid(post.author_id) self.user_model.update_user_info_by_user_id(user.uid, {"plus":user.plus+1}) else: self.write(lib.jsonp.print_JSON({ "success": 0, "message": "failed", }))