Пример #1
0
    def get_topics(self, page=1, category='all', order_by='created_at'):
        if category == 'all':
            topics = orm.select(rv for rv in m.Topic if rv.node_id == self.id)
            order_by = 'last_reply_date'

        else:
            if category == 'hot':
                topics = mc.get('node_%s_hot_topics' % self.id)
                if not topics:
                    now = int(time.time())
                    ago = now - 60 * 60 * 24
                    topics = orm.select(rv for rv in m.Topic if
                                    rv.node_id == self.id and
                                    rv.created_at > ago)
                    topics = topics.order_by(lambda rv: orm.desc((rv.collect_count +
                                                                  rv.thank_count - rv.report_count) * 10 +
                                                                 (rv.up_count - rv.down_count) * 5 +
                                                                 rv.reply_count * 3))
                    mc.set('node_%s_hot_topics' % self.id, list(topics),
                           60 * 60 * 3)
                order_by = 'none'
            elif category == 'latest':
                topics = orm.select(rv for rv in m.Topic if rv.node_id == self.id)
            elif category == 'desert':
                topics = orm.select(rv for rv in m.Topic if
                                rv.node_id == self.id and rv.reply_count == 0)
            else:
                topics = orm.select(rv for rv in m.Topic if
                                rv.node_id == self.id and rv.role == category)
                order_by = 'last_reply_date'

        if order_by == 'last_reply_date':
            topics = topics.order_by(lambda rv: orm.desc(rv.last_reply_date))
        elif order_by == 'created_at':
            topics = topics.order_by(lambda rv: orm.desc(rv.created_at))
        elif order_by == 'active':
            topics = topics.order_by(lambda rv: orm.desc(rv.active))
        elif order_by == 'smart':
            topics = topics.order_by(lambda rv: orm.desc((rv.collect_count +
                                                          rv.thank_count -
                                                          rv.report_count) * 10 +
                                                         (rv.up_count -
                                                          rv.down_count) * 5 +
                                                         rv.reply_count * 3))

        if page:
            return topics[(page - 1) * config.paged: page * config.paged]
        else:
            return topics
Пример #2
0
    def get_topics(self, page=1, category='all', order_by='created_at'):
        if category == 'all':
            topics = orm.select(rv for rv in m.Topic if rv.node_id == self.id)
            order_by = 'last_reply_date'

        else:
            if category == 'hot':
                topics = mc.get('node_%s_hot_topics' % self.id)
                if not topics:
                    now = int(time.time())
                    ago = now - 60 * 60 * 24
                    topics = orm.select(
                        rv for rv in m.Topic
                        if rv.node_id == self.id and rv.created_at > ago)
                    topics = topics.order_by(lambda rv: orm.desc(
                        (rv.collect_count + rv.thank_count - rv.report_count
                         ) * 10 + (rv.up_count - rv.down_count
                                   ) * 5 + rv.reply_count * 3))
                    mc.set('node_%s_hot_topics' % self.id, list(topics),
                           60 * 60 * 3)
                order_by = 'none'
            elif category == 'latest':
                topics = orm.select(rv for rv in m.Topic
                                    if rv.node_id == self.id)
            elif category == 'desert':
                topics = orm.select(
                    rv for rv in m.Topic
                    if rv.node_id == self.id and rv.reply_count == 0)
            else:
                topics = orm.select(
                    rv for rv in m.Topic
                    if rv.node_id == self.id and rv.role == category)
                order_by = 'last_reply_date'

        if order_by == 'last_reply_date':
            topics = topics.order_by(lambda rv: orm.desc(rv.last_reply_date))
        elif order_by == 'created_at':
            topics = topics.order_by(lambda rv: orm.desc(rv.created_at))
        elif order_by == 'active':
            topics = topics.order_by(lambda rv: orm.desc(rv.active))
        elif order_by == 'smart':
            topics = topics.order_by(lambda rv: orm.desc(
                (rv.collect_count + rv.thank_count - rv.report_count) * 10 +
                (rv.up_count - rv.down_count) * 5 + rv.reply_count * 3))

        if page:
            return topics[(page - 1) * config.paged:page * config.paged]
        else:
            return topics
Пример #3
0
    def flash_message(self, kargs=None):
        def get_category_message(messages):
            for cat, msg in messages:
                yield (cat, msg)

        key = '%s_flash_message' % self.xsrf_token

        if not kargs:
            messages = mc.get(key)
            if not messages:
                return []
            mc.delete(key, time=0)
            return messages

        msg = kargs.get('message', None)
        category = kargs.get('status', None)
        message = (category, msg)
        messages = mc.get(key)
        if isinstance(messages, list):
            messages.append(message)
        else:
            messages = [message]
        mc.set(key, messages, 600)
        return message
Пример #4
0
    def flash_message(self, kargs=None):
        def get_category_message(messages):
            for cat, msg in messages:
                yield (cat, msg)

        key = '%s_flash_message' % self.xsrf_token

        if not kargs:
            messages = mc.get(key)
            if not messages:
                return []
            mc.delete(key, time=0)
            return messages

        msg = kargs.get('message', None)
        category = kargs.get('status', None)
        message = (category, msg)
        messages = mc.get(key)
        if isinstance(messages, list):
            messages.append(message)
        else:
            messages = [message]
        mc.set(key, messages, 600)
        return message
Пример #5
0
 def get(self):
     page = force_int(self.get_argument('page', 1), 1)
     category = self.get_argument('category', None)
     view = self.get_argument('view', 'all')
     user = self.current_user
     if not category:
         category = self.index_category
     else:
         self.set_index_category(category)
     if category == 'timeline' and not user:
         category = self.set_index_category('index')
     if category == 'hot':
         topics = mc.get('hot_topics')
         if not topics:
             now = int(time.time())
             ago = now - 60 * 60 * 24
             topics = orm.select(
                 rv for rv in Topic if rv.created_at > ago
             ).order_by(lambda rv: orm.desc(
                 (rv.collect_count + rv.thank_count - rv.report_count) * 10
                 + (rv.up_count - rv.down_count) * 5 + rv.reply_count * 3))
             mc.set('hot_topics', list(topics), 60 * 60 * 2)
     elif category == 'timeline':
         topics = user.get_followed_topics(page=None, category=view)
     elif category == 'latest':
         topics = orm.select(
             rv
             for rv in Topic).order_by(lambda rv: orm.desc(rv.created_at))
     elif category == 'desert':
         topics = orm.select(rv for rv in Topic if rv.reply_count ==
                             0).order_by(lambda rv: orm.desc(rv.created_at))
     else:
         topics = orm.select(rv for rv in Topic).order_by(
             lambda rv: orm.desc(rv.last_reply_date))
     topic_count = orm.count(topics)
     topics = topics[(page - 1) * config.paged:page * config.paged]
     page_count = (topic_count + config.paged - 1) // config.paged
     return self.render("site/index.html",
                        topics=topics,
                        category=category,
                        page=page,
                        view=view,
                        page_count=page_count,
                        url='/')
Пример #6
0
 def get(self):
     page = force_int(self.get_argument("page", 1), 1)
     category = self.get_argument("category", None)
     view = self.get_argument("view", "all")
     user = self.current_user
     if not category:
         category = self.index_category
     else:
         self.set_index_category(category)
     if category == "timeline" and not user:
         category = self.set_index_category("index")
     if category == "hot":
         topics = mc.get("hot_topics")
         if not topics:
             now = int(time.time())
             ago = now - 60 * 60 * 24
             topics = select(rv for rv in Topic if rv.created_at > ago).order_by(
                 lambda rv: desc(
                     (rv.collect_count + rv.thank_count - rv.report_count) * 10
                     + (rv.up_count - rv.down_count) * 5
                     + rv.reply_count * 3
                 )
             )
             mc.set("hot_topics", list(topics), 60 * 60 * 2)
     elif category == "timeline":
         topics = user.get_timeline(page=None, category=view)
     elif category == "latest":
         topics = select(rv for rv in Topic).order_by(lambda rv: desc(rv.created_at))
     elif category == "desert":
         topics = select(rv for rv in Topic if rv.reply_count == 0).order_by(lambda rv: desc(rv.created_at))
     else:
         topics = select(rv for rv in Topic).order_by(lambda rv: desc(rv.last_reply_date))
     topic_count = count(topics)
     topics = topics[(page - 1) * config.paged : page * config.paged]
     page_count = (topic_count + config.paged - 1) // config.paged
     return self.render(
         "site/index.html", topics=topics, category=category, page=page, view=view, page_count=page_count, url="/"
     )
Пример #7
0
 def get(self):
     page = force_int(self.get_argument('page', 1), 1)
     category = self.get_argument('category', None)
     view = self.get_argument('view', 'all')
     user = self.current_user
     if not category:
         category = self.index_category
     else:
         self.set_index_category(category)
     if category == 'timeline' and not user:
         category = self.set_index_category('index')
     if category == 'hot':
         topics = mc.get('hot_topics')
         if not topics:
             now = int(time.time())
             ago = now - 60 * 60 * 24
             topics = orm.select(rv for rv in Topic if
                                 rv.created_at > ago).order_by(lambda rv:
                                                               orm.desc((rv.collect_count + rv.thank_count
                                                                         - rv.report_count) * 10 +
                                                                        (rv.up_count - rv.down_count) * 5 +
                                                                        rv.reply_count * 3))
             mc.set('hot_topics', list(topics), 60 * 60 * 2)
     elif category == 'timeline':
         topics = user.get_followed_topics(page=None, category=view)
     elif category == 'latest':
         topics = orm.select(rv for rv in Topic).order_by(lambda rv:
                                                          orm.desc(rv.created_at))
     elif category == 'desert':
         topics = orm.select(rv for rv in Topic if rv.reply_count == 0).order_by(lambda rv:
                                                                                 orm.desc(rv.created_at))
     else:
         topics = orm.select(rv for rv in Topic).order_by(lambda rv: orm.desc(rv.last_reply_date))
     topic_count = orm.count(topics)
     topics = topics[(page - 1) * config.paged: page * config.paged]
     page_count = (topic_count + config.paged - 1) // config.paged
     return self.render("site/index.html", topics=topics, category=category,
                        page=page, view=view, page_count=page_count, url='/')
Пример #8
0
 def get_users(page=1, category='all', limit=None):
     if category == 'all':
         users = select(rv for rv in User).order_by(lambda rv:
                 desc(rv.created_at))
     if category == 'hot':
         users = mc.get('hot_users')
         if not users:
             if not limit:
                 limit = 8
             users = select(rv for rv in User).order_by(lambda rv:
                     desc(rv.thank_count * 4 + rv.up_count * 3 + rv.topic_count
                         * 2 + rv.reply_count))[:limit]
             mc.set('hot_users', list(users), 60 * 60 * 12)
     if category == 'new':
         users = select(rv for rv in User).order_by(lambda rv:
                 desc(rv.created_at))
     if limit:
         return users[:limit]
     if page:
         return users[(page - 1) * config.user_paged: page *
                 config.user_paged]
     else:
         return users
Пример #9
0
 def get_users(page=1, category='all', limit=None):
     if category == 'all':
         users = select(
             rv for rv in User).order_by(lambda rv: desc(rv.created_at))
     if category == 'hot':
         users = mc.get('hot_users')
         if not users:
             if not limit:
                 limit = 8
             users = select(rv for rv in User).order_by(
                 lambda rv: desc(rv.thank_count * 4 + rv.up_count * 3 + rv.
                                 topic_count * 2 + rv.reply_count))[:limit]
             mc.set('hot_users', list(users), 60 * 60 * 12)
     if category == 'new':
         users = select(
             rv for rv in User).order_by(lambda rv: desc(rv.created_at))
     if limit:
         return users[:limit]
     if page:
         return users[(page - 1) * config.user_paged:page *
                      config.user_paged]
     else:
         return users
Пример #10
0
 def cover_id(self):
     return mc.get(self.cover_cache_key)
Пример #11
0
 def cover_id(self):
     return mc.get(self.cover_cache_key)