Esempio n. 1
0
    def get_users(page=1, category='all', limit=None):
        if category == 'all':
            users = orm.select(rv for rv in User).order_by(lambda:
                                                           orm.desc(rv.created_at))
        elif category == 'hot':
            users = mc.get('hot_users')
            if not users:
                if not limit:
                    limit = 8
                users = orm.select(rv for rv in User).order_by(lambda:
                                                               orm.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)
        elif category == 'new':
            users = orm.select(rv for rv in User).order_by(lambda: orm.desc(rv.created_at))
        else:
            return []

        if limit:
            return users[:limit]
        if page:
            return users[(page - 1) * config.user_paged: page *
                         config.user_paged]
        else:
            return users
Esempio n. 2
0
    def get_topics(self, page=1, category='all', order_by='created_at'):
        if category == 'all':
            topics = orm.select(rv for rv in collipa.models.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 collipa.models.Topic
                        if rv.node_id == self.id and rv.created_at > ago)
                    topics = topics.order_by(lambda: 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 collipa.models.Topic
                                    if rv.node_id == self.id)
            elif category == 'desert':
                topics = orm.select(
                    rv for rv in collipa.models.Topic
                    if rv.node_id == self.id and rv.reply_count == 0)
            else:
                topics = orm.select(
                    rv for rv in collipa.models.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: orm.desc(rv.last_reply_date))
        elif order_by == 'created_at':
            topics = topics.order_by(lambda: orm.desc(rv.created_at))
        elif order_by == 'active':
            topics = topics.order_by(lambda: orm.desc(rv.active))
        elif order_by == 'smart':
            topics = topics.order_by(lambda: 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
Esempio n. 3
0
    def get_topics(self, page=1, category='all', order_by='created_at'):
        if category == 'all':
            topics = orm.select(rv for rv in collipa.models.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 collipa.models.Topic if
                                    rv.node_id == self.id and
                                    rv.created_at > ago)
                    topics = topics.order_by(lambda: 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 collipa.models.Topic if rv.node_id == self.id)
            elif category == 'desert':
                topics = orm.select(rv for rv in collipa.models.Topic if
                                    rv.node_id == self.id and rv.reply_count == 0)
            else:
                topics = orm.select(rv for rv in collipa.models.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: orm.desc(rv.last_reply_date))
        elif order_by == 'created_at':
            topics = topics.order_by(lambda: orm.desc(rv.created_at))
        elif order_by == 'active':
            topics = topics.order_by(lambda: orm.desc(rv.active))
        elif order_by == 'smart':
            topics = topics.order_by(lambda: 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
Esempio n. 4
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: 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: orm.desc(rv.created_at))
     elif category == 'desert':
         topics = orm.select(rv for rv in Topic if rv.reply_count ==
                             0).order_by(lambda: orm.desc(rv.created_at))
     else:
         topics = orm.select(
             rv
             for rv in Topic).order_by(lambda: 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='/')
Esempio n. 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:
                                                               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:
                                                          orm.desc(rv.created_at))
     elif category == 'desert':
         topics = orm.select(rv for rv in Topic if rv.reply_count == 0).order_by(lambda:
                                                                                 orm.desc(rv.created_at))
     else:
         topics = orm.select(rv for rv in Topic).order_by(lambda: orm.desc(rv.last_reply_date))
     if isinstance(topics, list):
         topic_count = len(topics)
     else:
         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='/')
Esempio n. 6
0
 def cover_id(self):
     return mc.get(self.cover_cache_key)
Esempio n. 7
0
 def cover_id(self):
     return mc.get(self.cover_cache_key)