def post(self, nodeid='1'): n_obj = Node.get_by_key('n-' + str(nodeid)) if not n_obj: self.set_status(404) self.write('404') return errors = [] author = str(self.get_cookie('username', '')) title = self.get_argument('title', '') content = self.get_argument('content', '') t_obj = TOPIC_DICT.copy() if title and content: if len(title) <= TITLE_MAX_S and len(content) <= CONTENT_MAX_S: int_time = int(time()) #check spam u_topic_time = kv.get('u_topic_time:' + author) if u_topic_time: tlist = u_topic_time.split(',') if len(tlist) == MEMBER_RECENT_TOPIC and ( int_time - int(tlist[-1])) < 3600: self.write( u'403:不要发帖太频繁了 <a href="/newpost/%s">请返回</a>' % nodeid) return #check repeat content = textilize(content) #content = safe_encode(content) con_md5 = md5(content.encode('utf-8')).hexdigest() if mc.get('c_' + con_md5): self.write(u'403:请勿灌水 <a href="/newpost/%s">请返回</a>' % nodeid) return else: mc.set('c_' + con_md5, '1', 36000) t_obj['title'] = title t_obj['nodeid'] = str(nodeid) t_obj['nodename'] = n_obj['name'] t_obj['author'] = author t_obj['add'] = int_time t_obj['content'] = content if n_obj['count']: topic_id = int(n_obj['count']) + 1 else: topic_id = 1 if Topic.add(topic_id, t_obj): topic_key = 't-%s-%s' % (str(nodeid), str(topic_id)) #node count +1 n_obj['count'] = str(topic_id) Commomkvdb.save('n-' + str(nodeid), n_obj) #member recent +key #Member.add_key_rencent_topic(author, topic_key) rt_obj = kv.get('topic-' + author) if rt_obj: olist = rt_obj.split(',') if topic_key not in olist: olist.insert(0, topic_key) rt_obj = ','.join(olist[:MEMBER_RECENT_TOPIC]) kv.set('topic-' + author, rt_obj) else: rt_obj = topic_key kv.set('topic-' + author, topic_key) #recent in home +key Commomkvdb.add_key_rencent_topic('recent-topic-home', topic_key) #all topic counter +1 Count.key_incr('all-topic-num') #hot node tqueue = TaskQueue('default') tqueue.add( Task('/task/hotnode/%s/%s' % ('n-' + str(nodeid), str(topic_id)), delay=5)) #notifications mentions = findall_mentions(t_obj['content'], author) if mentions: tqueue.add( Task('/task/mentions/' + topic_key, 'member=' + ','.join(mentions), delay=8)) #set for check spam #u_topic_time = kv.get('u_topic_time:'+author) if u_topic_time: tlist = u_topic_time.split(',') if str(int_time) not in tlist: tlist.insert(0, str(int_time)) u_topic_time = ','.join( tlist[:MEMBER_RECENT_TOPIC]) kv.set('u_topic_time:' + author, u_topic_time) else: u_topic_time = str(int_time) kv.set('u_topic_time:' + author, u_topic_time) ##set new sr_code cur_user = self.cur_user() code_list = [cur_user['code'], u_topic_time] u_comment_time = kv.get('u_comment_time:' + author) if u_comment_time: code_list.append(u_comment_time) self.set_cookie('usercode', md5(''.join(code_list)).hexdigest(), path="/", expires_days=365) #del cache clear_cache_multi([ 'get_topic_by_keys:recent-topic-home', 'get_topic_by_keys:topic-' + author, 'get_comment_topic_by_keys:recent-topic-home', 'get_comment_topic_by_keys:recent-comment-topic-home', 'cur_user:'******'/' + topic_key) return else: errors.append("服务器出现错误,请稍后再试") else: t_obj['title'] = title t_obj['content'] = content errors.append(u"注意标题和内容的最大字数:%s %d" % (len(title), len(content))) else: errors.append("标题和内容必填") self.echo('newpost.html', { 'title': "发新帖子", 'errors': errors, 'n_obj': n_obj, 't_obj': t_obj, }, layout='_layout.html')
def post(self, nodeid='1'): if self.cur_user and self.cur_user.flag > 1: n_obj = Node.get_by_id(int(nodeid)) if not n_obj: self.error(404) self.write('404: not found') return errors = [] author = self.cur_user.name title = self.POST['title'] content = self.POST['content'] if title and content: if len(title) <= TITLE_MAX_S and len(content) <= CONTENT_MAX_S: int_time = int(time()) #check spam mi_obj = MemberInfo.get_or_insert(author) if self.cur_user.flag < 99: if mi_obj.topict: t_list = mi_obj.topict.split(',') if len(t_list) == MEMBER_RECENT_TOPIC and ( int_time - int(t_list[-1])) < 3600: self.write( u'403:不要发帖太频繁了 <a href="/newpost/%s">请返回</a>' % nodeid) 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="/newpost/%s">请返回</a>' % nodeid) return else: memcache.set('c_' + con_md5, '1', 36000) if n_obj.count: topic_id = n_obj.count + 1 else: topic_id = 1 topic_key = '%s-%s' % (nodeid, str(topic_id)) t_obj = Topic(key_name=topic_key) t_obj.title = title t_obj.author = author t_obj.add = int_time t_obj.con = content t_obj.put() if t_obj.is_saved(): #node count +1 n_obj.count += 1 db.run_in_transaction(obj_runput, n_obj) #memberinfo mi_obj.topicn += 1 if mi_obj.topick: t_list = mi_obj.topick.split(',') t_list.insert(0, topic_key) mi_obj.topick = ','.join( t_list[:MEMBER_RECENT_TOPIC]) else: mi_obj.topick = topic_key if mi_obj.topict: t_list = mi_obj.topict.split(',') t_list.insert(0, str(int_time)) mi_obj.topict = ','.join( t_list[:MEMBER_RECENT_TOPIC]) else: mi_obj.topict = str(int_time) db.run_in_transaction(obj_runput, mi_obj, ['topic_objs:' + author]) #recent in home +key hi_obj = KeyStrValue.get_or_insert('recent-topic-home') if hi_obj.value: t_list = hi_obj.value.split(',') t_list.insert(0, topic_key) hi_obj.value = ','.join(t_list[:RECENT_POST_NUM]) else: hi_obj.value = topic_key db.run_in_transaction(obj_runput, hi_obj, [ 'get_topic_objs:recent-topic-home', 'get_topic_key_title:recent-topic-home' ]) #all topic counter +1 at_obj = Counter.get_or_insert('all-topic-num', name='all-topic-num') at_obj.value += 1 db.run_in_transaction(obj_runput, at_obj) #notifications mentions = findall_mentions(t_obj.con, author) if mentions: deferred.defer(set_mentions, topic_key, ','.join(mentions), _countdown=8, _queue="default") self.redirect('/t-' + topic_key) return else: t_obj = Topic(title=title, con=content) errors.append(u"注意标题和内容的最大字数限制,当前字数:%s %d" % (len(title), len(content))) else: t_obj = Topic(title=title, con=content) errors.append("标题和内容必填") self.echo('newpost.html', { 'title': "发新帖子", 'errors': errors, 'n_obj': n_obj, 't_obj': t_obj, 'newest_node': Node.get_newest(), }, layout='_layout.html') else: self.error(403) self.write('403:forbidden')
def post(self, nodeid='1'): n_obj = Node.get_by_key('n-'+str(nodeid)) if not n_obj: self.set_status(404) self.write('404') return errors = [] author = str(self.get_cookie('username','')) title = self.get_argument('title','') content = self.get_argument('content','') t_obj = TOPIC_DICT.copy() if title and content: if len(title)<=TITLE_MAX_S and len(content)<=CONTENT_MAX_S: int_time = int(time()) #check spam u_topic_time = kv.get('u_topic_time:'+author) if u_topic_time: tlist = u_topic_time.split(',') if len(tlist)== MEMBER_RECENT_TOPIC and (int_time-int(tlist[-1])) < 3600: self.write(u'403:不要发帖太频繁了 <a href="/newpost/%s">请返回</a>' % nodeid) return #check repeat content = textilize(content) #content = safe_encode(content) con_md5 = md5(content.encode('utf-8')).hexdigest() if mc.get('c_'+con_md5): self.write(u'403:请勿灌水 <a href="/newpost/%s">请返回</a>' % nodeid) return else: mc.set('c_'+con_md5, '1', 36000) t_obj['title'] = title t_obj['nodeid'] = str(nodeid) t_obj['nodename'] = n_obj['name'] t_obj['author'] = author t_obj['add'] = int_time t_obj['content'] = content if n_obj['count']: topic_id = int(n_obj['count']) + 1 else: topic_id = 1 if Topic.add(topic_id, t_obj): topic_key = 't-%s-%s' % (str(nodeid), str(topic_id)) #node count +1 n_obj['count'] = str(topic_id) Commomkvdb.save('n-'+str(nodeid), n_obj) #member recent +key #Member.add_key_rencent_topic(author, topic_key) rt_obj = kv.get('topic-'+author) if rt_obj: olist = rt_obj.split(',') if topic_key not in olist: olist.insert(0, topic_key) rt_obj = ','.join(olist[:MEMBER_RECENT_TOPIC]) kv.set('topic-'+author, rt_obj) else: rt_obj = topic_key kv.set('topic-'+author, topic_key) #recent in home +key Commomkvdb.add_key_rencent_topic('recent-topic-home', topic_key) #all topic counter +1 Count.key_incr('all-topic-num') #hot node tqueue = TaskQueue('default') tqueue.add(Task('/task/hotnode/%s/%s' % ('n-'+str(nodeid), str(topic_id)), delay=5)) #notifications mentions = findall_mentions(t_obj['content'], author) if mentions: tqueue.add(Task('/task/mentions/'+topic_key, 'member='+','.join(mentions), delay=8)) #set for check spam #u_topic_time = kv.get('u_topic_time:'+author) if u_topic_time: tlist = u_topic_time.split(',') if str(int_time) not in tlist: tlist.insert(0, str(int_time)) u_topic_time = ','.join(tlist[:MEMBER_RECENT_TOPIC]) kv.set('u_topic_time:'+author, u_topic_time) else: u_topic_time = str(int_time) kv.set('u_topic_time:'+author, u_topic_time) ##set new sr_code cur_user = self.cur_user() code_list = [cur_user['code'],u_topic_time] u_comment_time = kv.get('u_comment_time:'+author) if u_comment_time: code_list.append(u_comment_time) self.set_cookie('usercode', md5(''.join(code_list)).hexdigest(), path="/", expires_days = 365 ) #del cache clear_cache_multi(['get_topic_by_keys:recent-topic-home','get_topic_by_keys:topic-' + author, 'get_comment_topic_by_keys:recent-topic-home', 'get_comment_topic_by_keys:recent-comment-topic-home','cur_user:'******'/'+topic_key) return else: errors.append("服务器出现错误,请稍后再试") else: t_obj['title'] = title t_obj['content'] = content errors.append(u"注意标题和内容的最大字数:%s %d" % (len(title), len(content))) else: errors.append("标题和内容必填") self.echo('newpost.html', { 'title': "发新帖子", 'errors':errors, 'n_obj': n_obj, 't_obj': t_obj, }, layout='_layout.html')
def post(self, nodeid='1'): if self.cur_user and self.cur_user.flag>1: n_obj = Node.get_by_id(int(nodeid)) if not n_obj: self.error(404) self.write('404: not found') return errors = [] author = self.cur_user.name title = self.POST['title'] content = self.POST['content'] if title and content: if len(title)<=TITLE_MAX_S and len(content)<=CONTENT_MAX_S: int_time = int(time()) #check spam mi_obj = MemberInfo.get_or_insert(author) if mi_obj.topict: t_list = mi_obj.topict.split(',') if len(t_list) == MEMBER_RECENT_TOPIC and (int_time-int(t_list[-1])) < 3600: self.write(u'403:不要发帖太频繁了 <a href="/newpost/%s">请返回</a>' % nodeid) 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="/newpost/%s">请返回</a>' % nodeid) return else: memcache.set('c_'+con_md5, '1', 36000) if n_obj.count: topic_id = n_obj.count + 1 else: topic_id = 1 topic_key = '%s-%s' % (nodeid, str(topic_id)) t_obj = Topic(key_name=topic_key) t_obj.title = title t_obj.author = author t_obj.add = int_time t_obj.con = textilize(content) #t_obj.con = safe_encode(content) t_obj.put() if t_obj.is_saved(): #node count +1 n_obj.count += 1 db.run_in_transaction(obj_runput,n_obj) #memberinfo mi_obj.topicn += 1 if mi_obj.topick: t_list = mi_obj.topick.split(',') t_list.insert(0, topic_key) mi_obj.topick = ','.join(t_list[:MEMBER_RECENT_TOPIC]) else: mi_obj.topick = topic_key if mi_obj.topict: t_list = mi_obj.topict.split(',') t_list.insert(0, str(int_time)) mi_obj.topict = ','.join(t_list[:MEMBER_RECENT_TOPIC]) else: mi_obj.topict = str(int_time) db.run_in_transaction(obj_runput,mi_obj,['topic_objs:'+author]) #recent in home +key hi_obj = KeyStrValue.get_or_insert('recent-topic-home') if hi_obj.value: t_list = hi_obj.value.split(',') t_list.insert(0, topic_key) hi_obj.value = ','.join(t_list[:RECENT_POST_NUM]) else: hi_obj.value = topic_key db.run_in_transaction(obj_runput,hi_obj,['get_topic_objs:recent-topic-home', 'get_topic_key_title:recent-topic-home']) #all topic counter +1 at_obj = Counter.get_or_insert('all-topic-num', name = 'all-topic-num') at_obj.value += 1 db.run_in_transaction(obj_runput,at_obj) #notifications mentions = findall_mentions(t_obj.con, author) if mentions: deferred.defer(set_mentions, topic_key, ','.join(mentions), _countdown=8, _queue="default") self.redirect('/t-'+topic_key) return else: t_obj = Topic(title = title, con = content) errors.append(u"注意标题和内容的最大字数限制,当前字数:%s %d" % (len(title), len(content))) else: t_obj = Topic(title = title, con = content) errors.append("标题和内容必填") self.echo('newpost.html', { 'title': "发新帖子", 'errors':errors, 'n_obj': n_obj, 't_obj': t_obj, 'newest_node': Node.get_newest(), }, layout='_layout.html') else: self.error(403) self.write('403:forbidden')