def post(self): verify = self.request.get('sdamwegmw') if verify != "AfnGDASsdafSA" : return self.error(403) post_id_ = self.request.get('post_id') post = Post.get_by_id(int(post_id_)) if post is None: return self.redirect('/') comment = Comment() comment.post = post comment.author = self.request.get('comm_name') if users.is_current_user_admin(): comment.author_is_admin = True comment.authorEmail = self.request.get('comm_email') comment.authorWebsite = self.request.get('comm_url') comment.content = self.request.get('comment') comment.userIp = self.request.remote_addr user = users.get_current_user() if user is not None: comment.user = user comment.author = str(user.nickname()) comment.authorEmail = str(user.email()) cookies = Cookie.SimpleCookie() cookies['comm_name'] = urllib.quote(comment.author.encode('utf8')) cookies['comm_name']['path'] = '/' cookies['comm_name']['max-age'] = 3600*24*365 cookies['comm_email'] = comment.authorEmail cookies['comm_email']['path'] = '/' cookies['comm_email']['max-age'] = 3600*24*365 cookies['comm_url'] = comment.authorWebsite cookies['comm_url']['path'] = '/' cookies['comm_url']['max-age'] = 3600*24*365 output_headers = [] output_headers.append('%s\r\n' % cookies) for header in output_headers: sys.stdout.write(header) comment.save() util.flushRecentComment() return self.redirect(post.full_permalink())
def add_comment(): """添加评论操作""" context = request.form.get('comment') comment = Comment(context=context) community_id = request.form.get('community_id') community = Community.query.filter(Community.id == community_id).first() comment.community = community username = session.get('user_name') author = User.query.filter(User.username == username).first() comment.author = author db.session.add(comment) db.session.commit() return redirect(url_for('question_detail', community_id=community_id))
def add_comment(): content = request.form.get('comment-content') question_id = request.form.get('question_id') comment = Comment(content=content) user_id = session.get('user_id') user = User.query.filter(User.id == user_id).first() comment.author = user question = Question.query.filter(Question.id == question_id).first() comment.question = question db.session.add(comment) db.session.commit() return redirect(url_for('detail', question_id=question_id))
def process_comment(operation): comment = Comment() comment.author = operation[1]['author']
def post(self, nodeid, topicid): if self.cur_user and self.cur_user.flag > 1: author = self.cur_user.name content = self.POST['content'] if content and len(content) <= COMMENT_MAX_S: int_time = int(time()) #check spam mi_obj = MemberInfo.get_or_insert(author) if self.cur_user.flag < 99: if mi_obj.replyt: t_list = mi_obj.replyt.split(',') if len(t_list) == MEMBER_RECENT_REPLY and ( int_time - int(t_list[-1])) < 3600: self.write( u'403:不要回复太频繁了 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid)) return #check repeat content = textilize(content) #safe_encode(content) con_md5 = md5(content.encode('utf-8')).hexdigest() if memcache.get('c_' + con_md5): self.write(u'403:请勿灌水 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid)) return else: memcache.set('c_' + con_md5, '1', 36000) topic_key = '%s-%s' % (nodeid, topicid) t_obj = Topic.get_by_key_name(topic_key) if not t_obj: self.error(404) self.write('404: not found') return if t_obj.cnum: id_num = t_obj.cnum + 1 else: id_num = 1 c_key = '%s-%d' % (topic_key, id_num) c_obj = Comment(key_name=c_key) c_obj.author = author c_obj.add = int_time c_obj.con = content c_obj.put() if c_obj.is_saved(): #topic commont count +1 t_obj.cnum = id_num t_obj.reply = author t_obj.edit = int_time db.run_in_transaction(obj_runput, t_obj) #memberinfo mi_obj.replyn += 1 if mi_obj.replyk: t_list = mi_obj.replyk.split(',') if topic_key in t_list: t_list.remove(topic_key) t_list.insert(0, topic_key) mi_obj.replyk = ','.join(t_list[:MEMBER_RECENT_REPLY]) else: mi_obj.replyk = topic_key if mi_obj.replyt: t_list = mi_obj.replyt.split(',') t_list.insert(0, str(int_time)) mi_obj.replyt = ','.join(t_list[:MEMBER_RECENT_REPLY]) else: mi_obj.replyt = str(int_time) db.run_in_transaction(obj_runput, mi_obj, ['reply_objs:' + author]) #recent reply +key hi_obj = KeyStrValue.get_or_insert('recent-reply-topic') if hi_obj.value: t_list = hi_obj.value.split(',') if topic_key in t_list: t_list.remove(topic_key) t_list.insert(0, topic_key) hi_obj.value = ','.join(t_list[:RECENT_COMMENT_NUM]) db.run_in_transaction( obj_runput, hi_obj, ['get_topic_key_title:recent-reply-topic']) else: hi_obj.value = topic_key db.run_in_transaction( obj_runput, hi_obj, ['get_topic_key_title:recent-reply-topic']) #all reply counter +1 at_obj = Counter.get_or_insert('all-reply-num', name='all-reply-num') at_obj.value += 1 db.run_in_transaction(obj_runput, at_obj) #notifications if t_obj.author != author: mentions = findall_mentions( c_obj.con + ' @%s ' % t_obj.author, author) else: mentions = findall_mentions(c_obj.con, author) if mentions: deferred.defer(set_mentions, topic_key, ','.join(mentions), _countdown=8, _queue="default") #del cache cache_keys = [] hi_obj = KeyStrValue.get_or_insert('recent-topic-home') if hi_obj.value: if topic_key in hi_obj.value.split(','): cache_keys.append( 'get_topic_objs:recent-topic-home') if id_num < EACH_PAGE_COMMENT_NUM: cache_keys.append('get_comments:%s:1' % topic_key) else: cache_keys.append('get_comments:%s:%d' % (topic_key, [ i for i in range(1, id_num, EACH_PAGE_COMMENT_NUM) ][-1])) if cache_keys: memcache.delete_multi(cache_keys) self.redirect('/t-%s#reply%d' % (topic_key, id_num)) return else: self.write('错误: 没有内容 或 内容太长了,请后退返回修改!') else: self.error(403) self.write('403:forbidden')
def post(self, nodeid, topicid): if self.cur_user and self.cur_user.flag>1: author = self.cur_user.name content = self.POST['content'] if content and len(content)<=COMMENT_MAX_S: int_time = int(time()) #check spam mi_obj = MemberInfo.get_or_insert(author) if mi_obj.replyt: t_list = mi_obj.replyt.split(',') if len(t_list) == MEMBER_RECENT_REPLY and (int_time-int(t_list[-1])) < 3600: self.write(u'403:不要回复太频繁了 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid)) return #check repeat content = textilize(content) #content = safe_encode(content) con_md5 = md5(content.encode('utf-8')).hexdigest() if memcache.get('c_'+con_md5): self.write(u'403:请勿灌水 <a href="/t-%s-%s">请返回</a>' % (nodeid, topicid)) return else: memcache.set('c_'+con_md5, '1', 36000) topic_key = '%s-%s' % (nodeid, topicid) t_obj = Topic.get_by_key_name(topic_key) if not t_obj: self.error(404) self.write('404: not found') return if t_obj.cnum: id_num = t_obj.cnum + 1 else: id_num = 1 c_key = '%s-%d' % (topic_key, id_num) c_obj = Comment(key_name=c_key) c_obj.author = author c_obj.add = int_time c_obj.con = content c_obj.put() if c_obj.is_saved(): #topic commont count +1 t_obj.cnum = id_num t_obj.reply = author t_obj.edit = int_time db.run_in_transaction(obj_runput,t_obj) #memberinfo mi_obj.replyn += 1 if mi_obj.replyk: t_list = mi_obj.replyk.split(',') if topic_key in t_list: t_list.remove(topic_key) t_list.insert(0, topic_key) mi_obj.replyk = ','.join(t_list[:MEMBER_RECENT_REPLY]) else: mi_obj.replyk = topic_key if mi_obj.replyt: t_list = mi_obj.replyt.split(',') t_list.insert(0, str(int_time)) mi_obj.replyt = ','.join(t_list[:MEMBER_RECENT_REPLY]) else: mi_obj.replyt = str(int_time) db.run_in_transaction(obj_runput,mi_obj,['reply_objs:'+author]) #recent reply +key hi_obj = KeyStrValue.get_or_insert('recent-reply-topic') if hi_obj.value: t_list = hi_obj.value.split(',') if topic_key in t_list: t_list.remove(topic_key) t_list.insert(0, topic_key) hi_obj.value = ','.join(t_list[:RECENT_COMMENT_NUM]) db.run_in_transaction(obj_runput,hi_obj,['get_topic_key_title:recent-reply-topic']) else: hi_obj.value = topic_key db.run_in_transaction(obj_runput,hi_obj,['get_topic_key_title:recent-reply-topic']) #all reply counter +1 at_obj = Counter.get_or_insert('all-reply-num', name = 'all-reply-num') at_obj.value += 1 db.run_in_transaction(obj_runput,at_obj) #notifications if t_obj.author != author: mentions = findall_mentions(c_obj.con+' @%s '%t_obj.author, author) else: mentions = findall_mentions(c_obj.con, author) if mentions: deferred.defer(set_mentions, topic_key, ','.join(mentions), _countdown=8, _queue="default") #del cache cache_keys = [] hi_obj = KeyStrValue.get_or_insert('recent-topic-home') if hi_obj.value: if topic_key in hi_obj.value.split(','): cache_keys.append('get_topic_objs:recent-topic-home') if id_num<EACH_PAGE_COMMENT_NUM: cache_keys.append('get_comments:%s:1' % topic_key) else: cache_keys.append('get_comments:%s:%d' % (topic_key, [i for i in range(1,id_num,EACH_PAGE_COMMENT_NUM)][-1])) if cache_keys: memcache.delete_multi(cache_keys) self.redirect('/t-%s#reply%d'%(topic_key,id_num)) return else: self.write('错误: 没有内容 或 内容太长了,请后退返回修改!') else: self.error(403) self.write('403:forbidden')