示例#1
0
    def handle(self):
        page_num = 10
        search_type = self.request.url_param.get('search_type')
        target = self.request.GET.get('target')
        if search_type and target:
            with MongoCon() as con:
                if self.request.method == 'GET':
                    if search_type == 'tag':
                        # 进入标签搜索模式
                        topics = con.db['topics'].find(
                            {'tag': {'$regex': target}}
                        ).sort('_id', pymongo.DESCENDING).limit(page_num)

                        topics_list = []
                        for topic in topics:
                            topic['author'] = con.dereference(topic['author'], {'nickname': True, 'avatar_url': True})
                            topics_list.append(topic)

                        self.response.set_body(
                            render('forum/topic_list.html', {
                                'request': self.request,
                                'topics': topics_list,
                                'has_more': True if len(topics_list) >= page_num else False,
                            })
                        )

                elif self.request.method == 'POST':
                    topics_num = int(self.request.POST.get('topics_num'))
                    if search_type == 'tag':
                        topics = con.db['topics'].find(
                            {'tag': {'$regex': target}}
                        ).sort('_id', pymongo.DESCENDING).skip(topics_num).limit(page_num)

                        topics_list = []
                        for topic in topics:
                            topic['_id'] = str(topic['_id'])
                            topic['author'] = con.dereference(topic['author'], {'nickname': True, 'avatar_url': True})
                            topic['author']['_id'] = str(topic['author']['_id'])
                            topics_list.append(topic)

                        self.response.set_body(json.dumps({
                            'status': 'ok',
                            'has_more': True if len(topics_list) >= page_num else False,
                            'topics_num': len(topics_list),
                            'content': render('more_topic.html', {'topics': topics_list})
                        }))
示例#2
0
 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()
示例#3
0
    def handle(self):
        with MongoCon() as con:
            page_num = 10
            if self.request.method == 'GET':
                topics = con.db['topics'].find(
                    {},
                    {
                        'author': True,
                        'datetime': True,
                        'page_view': True,
                        'reply_count': True,
                        'tag': True,
                        'thumbs_up': True,
                        'title': True
                    }
                ).sort('_id', pymongo.DESCENDING).limit(page_num)

                topics_list = []
                for topic in topics:
                    topic['author'] = con.dereference(topic['author'], {'nickname': True, 'avatar_url': True})
                    topics_list.append(topic)

                hot_topics = con.db['topics'].find({}, {'title': True}).sort('page_view', pymongo.DESCENDING).limit(5)

                self.response.set_body(
                    render(
                        'index.html',
                        {
                            'request': self.request,
                            'topics': topics_list,
                            'hot_topics': hot_topics,
                            'has_more': True if len(topics_list) >= page_num else False
                        }
                    )
                )
            elif self.request.method == 'POST':
                topics_num = int(self.request.POST.get('topics_num'))
                if topics_num:
                    topics = con.db['topics'].find(
                        {},
                        {
                            'author': True,
                            'datetime': True,
                            'page_view': True,
                            'reply_count': True,
                            'tag': True,
                            'thumbs_up': True,
                            'title': True
                        }
                    ).sort('_id', pymongo.DESCENDING).skip(topics_num).limit(page_num)

                    topics_list = []
                    for topic in topics:
                        topic['_id'] = str(topic['_id'])
                        topic['author'] = con.dereference(topic['author'], {'nickname': True, 'avatar_url': True})
                        topic['author']['_id'] = str(topic['author']['_id'])
                        topics_list.append(topic)

                    self.response.set_body(json.dumps({
                        'status': 'ok',
                        'has_more': True if len(topics_list) >= page_num else False,
                        'topics_num': len(topics_list),
                        'content': render('more_topic.html', {'topics': topics_list})
                    }))
示例#4
0
 def handle(self):
     if hasattr(self.request, 'authenticate'):
         self.response.set_body(render('forum/publish.html', {'request': self.request}))
     else:
         self.response.redirect('/')