Esempio n. 1
0
def item_list(req, pager, body, **kwargs):
    body = ', body ' if body else ''

    public = kwargs.pop('public', False)
    keys = list("%s %s %%s" % (k, 'in' if islistable(v) else '=')
                for k, v in kwargs.items())
    if public:       # public is alias key
        keys.append("public_date > 0")
        keys.append("state != 0")

    cond = "WHERE " + ' AND '.join(keys) if keys else ''

    tran = req.db.transaction(req.log_info)
    c = tran.cursor(DictCursor)
    c.execute("""
        SELECT new_id, author_id, email, create_date, public_date,
            title, locale, state %s
        FROM news n LEFT JOIN logins l ON (n.author_id = l.login_id) %s
            ORDER BY %s %s LIMIT %%s, %%s
        """ % (body, cond, pager.order, pager.sort),
              tuple(kwargs.values()) + (pager.offset, pager.limit))
    items = []
    for row in iter(c.fetchone, None):
        item = New(row['new_id'])
        item.author_id = row['author_id']
        item.author = row['email']
        item.state = row['state']
        item.create_date = datetime.fromtimestamp(row['create_date'])
        item.public_date = datetime.fromtimestamp(row['public_date'])
        item.title = row['title']
        item.locale = row['locale']
        if body:
            item.body = row['body']
        items.append(item)
    # endfor

    c.execute("SELECT count(*) FROM news %s" % cond, kwargs.values())
    pager.total = c.fetchone()['count(*)']
    tran.commit()

    return items