def handle(self): # 判断是否登录 if hasattr(self.request, 'authenticate'): # 判断内容标题是否为空 content = self.request.POST.get('content') title = self.request.POST.get('title') tag = self.request.POST.get('tag') if content and title: # 标题最长80个字符 if len(title.decode()) > 80: return # 最多设置5个标签 if len(tag.split(',')) > 5: return con = MongoCon() _id = con.db['topics'].insert({ 'author': con.DBRef('users', self.request.user['_id']), 'title': title, 'content': content, 'tag': tag.split(',') if tag else [], 'datetime': datetime.datetime.now(), 'page_view': 0, 'thumbs_up': 0, 'thumbs_up_list': [], 'reply_count': 0, 'reply': [], }) _id = str(_id) con.close() self.response.set_body(json.dumps({'status': 'ok', 'url': '/topic/%s/' % _id})) else: self.response.set_body(json.dumps({'status': 'error', 'msg': '标题/内容不能为空'})) else: self.response.set_body(json.dumps({'status': 'error', 'msg': '请登录'}))
def handle(self): if hasattr(self.request, 'authenticate'): topic_id = self.request.url_param.get('topic_id') content = self.request.POST.get('content') if topic_id and content: con = MongoCon() con.db['topics'].update( {'_id': con.id(topic_id)}, {'$push': {'reply': { 'content': content, 'author': con.DBRef('users', self.request.user['_id']), 'datetime': datetime.datetime.now(), }}} ) con.db['topics'].update( {'_id': con.id(topic_id)}, {'$inc': {'reply_count': 1}} ) con.close() self.response.set_body(json.dumps({ 'status': 'ok', 'nickname': self.request.user['nickname'], 'avatar_url': self.request.user['avatar_url'] }))
def handle(self): topic_id = self.request.url_param.get('topic_id') if topic_id: con = MongoCon() con.db['topics'].update({'_id': con.id(topic_id)}, {'$inc': {'page_view': 1}}) topic = con.db['topics'].find_one( {'_id': con.id(topic_id)} ) topic['author'] = con.dereference(topic['author'], {'nickname': True, 'avatar_url': True}) for reply in topic['reply']: reply['author'] = con.dereference(reply['author'], {'nickname': True, 'avatar_url': True}) self.response.set_body(render( 'forum/topic.html', {'request': self.request, 'topic': topic} )) con.close() else: self.response = Response404()