zpage / model /

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from _db import McModel, McCache, cursor_by_table, McCacheA, McCacheM
from zkit.algorithm.merge import imerge
from state import STATE_ACTIVE
from feed import PAGE_LIMIT

mc_feed_user_dict = McCacheM('FeedUserDict.%s')
mc_feed_po_iter = McCacheA('FeedPoIter.%s')
mc_feed_po_dict = McCacheM('FeedPoDict.%s')

cursor = cursor_by_table('po')

FEED_PO_ID_LASTEST_SQL = 'select id from po where user_id=%%s and state=%s order by id desc limit %s' % (STATE_ACTIVE, PAGE_LIMIT)
FEED_PO_ID_ITER_SQL = 'select id from po where user_id=%%s and state=%s and id<%%s order by id desc limit %s' % (STATE_ACTIVE, PAGE_LIMIT)

def feed_po_id_lastest(feed_id):
    cursor.execute(FEED_PO_ID_LASTEST_SQL, feed_id)
    return [i for i, in cursor.fetchall()]

def feed_po_iter(zsite_id, start_id=None):
    if start_id is None:
        id_list = feed_po_id_lastest(zsite_id)
        if id_list:
            for i in id_list:
                yield i
            start_id = i
    elif start_id == 0:
    while True:
        cursor.execute(FEED_PO_ID_ITER_SQL, (zsite_id, start_id))
        c = cursor.fetchall()
        if not c:
        for i, in c:
            yield i
        start_id = i

def feed_po_cmp_iter(zsite_id, start_id=None):
    for id in feed_po_iter(zsite_id, start_id):
        yield FeedPoCmp(id, zsite_id)

class FeedPoCmp(object):
    def __init__(self, id, zsite_id): = id
        self.zsite_id = zsite_id

    def __cmp__(self, other):
        return -

class FeedPoMerge(object):
    def __init__(self, zsite_id_list):
        self.zsite_id_list = zsite_id_list

    def merge_iter(self, limit=PAGE_LIMIT, begin_id=None):
        zsite_id_list = self.zsite_id_list
        count = 0
        for i in imerge(
                feed_po_cmp_iter(i, begin_id)
                for i in
            yield i
            count += 1
            if count >= limit:

if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.