def _count_articles(published_only=True): if published_only: return db.select_int( 'select count(id) from articles where website_id=? and draft=?', ctx.website.id, False) return db.select_int('select count(id) from articles where website_id=?', ctx.website.id)
def featured_poems(): total = db.select_int('select count(id) as num from poem where ilike>=100') s = set() while len(s)<5: s.add(random.randint(0, total-1)) L = [] for n in s: L.extend(db.select('select * from poem where ilike>=100 order by id limit ?,?', n, 1)) total = db.select_int('select count(id) from poem where ilike<100') s = set() while len(s)<5: s.add(random.randint(0, total-1)) for n in s: L.extend(db.select('select * from poem where ilike<100 order by id limit ?,?', n, 1)) return dict(poems=L)
def attachments(): i = ctx.request.input(action='', page='1', size='20') if i.action == 'delete': delete_attachment(i.id) raise seeother('attachments') page = int(i.page) size = int(i.size) num = db.select_int('select count(id) from attachments where website_id=?', ctx.website.id) if page < 1: raise APIValueError('page', 'page invalid.') if size < 1 or size > 100: raise APIValueError('size', 'size invalid.') offset = (page - 1) * size atts = db.select( 'select * from attachments where website_id=? order by id desc limit ?,?', ctx.website.id, offset, size + 1) next = False if len(atts) > size: atts = atts[:-1] next = True return Template('templates/attachments.html', attachments=atts, page=page, previous=page > 2, next=next)
def count_by(cls, where, *args): ''' Find by 'select count(pk) from table where ... ' and return int. ''' return db.select_int( 'select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
def api_delete_wikipage(): i = ctx.request.input(id="") if not i.id: raise APIValueError("id", "bad parameter: id") page = _get_wikipage(i.id) if db.select_int("select count(id) from wiki_pages where wiki_id=? and parent_id=?", page.wiki_id, page.id) > 0: raise APIPermissionError("cannot delete non empty page.") db.update("delete from wiki_pages where id=?", page.id) return True
def api_delete_wiki(): " delete a wiki by id. " i = ctx.request.input(id="") if not i.id: raise APIValueError("id", "id cannot be empty.") wiki = _get_wiki(i.id) count = db.select_int("select count(id) from wiki_pages where wiki_id=?", wiki.id) if count > 0: raise APIValueError("id", "cannot delete non-empty wiki.") db.update("delete from wikis where id=?", wiki.id) return True
def api_delete_wikipage(): i = ctx.request.input(id='') if not i.id: raise APIValueError('id', 'bad parameter: id') page = _get_wikipage(i.id) if db.select_int( 'select count(id) from wiki_pages where wiki_id=? and parent_id=?', page.wiki_id, page.id) > 0: raise APIPermissionError('cannot delete non empty page.') db.update('delete from wiki_pages where id=?', page.id) return True
def featured_poems(): total = db.select_int('select count(id) as num from poem where ilike>=100') s = set() while len(s) < 5: s.add(random.randint(0, total - 1)) L = [] for n in s: L.extend( db.select( 'select * from poem where ilike>=100 order by id limit ?,?', n, 1)) total = db.select_int('select count(id) from poem where ilike<100') s = set() while len(s) < 5: s.add(random.randint(0, total - 1)) for n in s: L.extend( db.select( 'select * from poem where ilike<100 order by id limit ?,?', n, 1)) return dict(poems=L)
def api_delete_wiki(): ' delete a wiki by id. ' i = ctx.request.input(id='') if not i.id: raise APIValueError('id', 'id cannot be empty.') wiki = _get_wiki(i.id) count = db.select_int('select count(id) from wiki_pages where wiki_id=?', wiki.id) if count > 0: raise APIValueError('id', 'cannot delete non-empty wiki.') db.update('delete from wikis where id=?', wiki.id) return True
def api_create_wikipage(): i = ctx.request.input(wiki_id="", name="", content="") if not "parent_id" in i: raise APIValueError("parent_id", "bad parameter: parent_id") if not i.wiki_id: raise APIValueError("wiki_id", "bad parameter: wiki_id") if not i.name.strip(): raise APIValueError("name", "invalid name") if not i.content.strip(): raise APIValueError("content", "invalid content") wiki = _get_wiki(i.wiki_id) if i.parent_id: p_page = _get_wikipage(i.parent_id, wiki.id) num = db.select_int("select count(id) from wiki_pages where wiki_id=? and parent_id=?", wiki.id, i.parent_id) return _create_wiki_page(wiki.id, i.parent_id, num, i.name.strip(), i.content)
def api_create_wikipage(): i = ctx.request.input(wiki_id='', name='', content='') if not 'parent_id' in i: raise APIValueError('parent_id', 'bad parameter: parent_id') if not i.wiki_id: raise APIValueError('wiki_id', 'bad parameter: wiki_id') if not i.name.strip(): raise APIValueError('name', 'invalid name') if not i.content.strip(): raise APIValueError('content', 'invalid content') wiki = _get_wiki(i.wiki_id) if i.parent_id: p_page = _get_wikipage(i.parent_id, wiki.id) num = db.select_int( 'select count(id) from wiki_pages where wiki_id=? and parent_id=?', wiki.id, i.parent_id) return _create_wiki_page(wiki.id, i.parent_id, num, i.name.strip(), i.content)
def attachments(): i = ctx.request.input(action='', page='1', size='20') if i.action=='delete': delete_attachment(i.id) raise seeother('attachments') page = int(i.page) size = int(i.size) num = db.select_int('select count(id) from attachments where website_id=?', ctx.website.id) if page < 1: raise APIValueError('page', 'page invalid.') if size < 1 or size > 100: raise APIValueError('size', 'size invalid.') offset = (page - 1) * size atts = db.select('select * from attachments where website_id=? order by id desc limit ?,?', ctx.website.id, offset, size+1) next = False if len(atts)>size: atts = atts[:-1] next = True return Template('templates/attachments.html', attachments=atts, page=page, previous=page>2, next=next)
def main(): if raw_input('To install iTranswarp, type Y and press ENTER: ') != 'Y': print 'Install cancelled.' exit(1) print 'Prepare to install iTranswarp...' try: print 'Checking Python version...', _check_version() print 'Checking Python Imaging Library...', _check_pil() print 'Checking Redis...', _check_redis() host = raw_input('Database host (localhost): ') port = raw_input('Database port (3306): ') user = raw_input('Database user (root): ') dbpass = raw_input('Database password: '******'': port = '3306' db.init(db_type='mysql', db_schema='itrans', \ db_host=host or 'localhost', db_port=int(port), \ db_user=user or 'root', db_password=dbpass, \ use_unicode=True, charset='utf8') print 'Creating tables . . .', for sql in CREATE_TABLES: if not sql.startswith('--'): db.update(sql) print '.', print '\nInit database ok.' email = raw_input('Super admin email: ').strip().lower() passwd = raw_input('Super admin password: '******'iTranswarp', 'localhost') if db.select_int('select count(*) from mysql.user where user=?', 'www-data') == 0: db.update( 'create user \'www-data\'@\'localhost\' identified by \'www-data\'' ) db.update( 'grant select,insert,update,delete on itrans.* to \'www-data\'@\'localhost\' identified by \'www-data\'' ) db.update('update users set role_id=0, passwd=? where email=?', passwd, email) print 'Install successfully!' except Exception, e: print 'Install failed:', e.message raise
def main(): if raw_input('To install iTranswarp, type Y and press ENTER: ')!='Y': print 'Install cancelled.' exit(1) print 'Prepare to install iTranswarp...' try: print 'Checking Python version...', _check_version() print 'Checking Python Imaging Library...', _check_pil() print 'Checking Redis...', _check_redis() host = raw_input('Database host (localhost): ') port = raw_input('Database port (3306): ') user = raw_input('Database user (root): ') dbpass = raw_input('Database password: '******'': port = '3306' db.init(db_type='mysql', db_schema='itrans', \ db_host=host or 'localhost', db_port=int(port), \ db_user=user or 'root', db_password=dbpass, \ use_unicode=True, charset='utf8') print 'Creating tables . . .', for sql in CREATE_TABLES: if not sql.startswith('--'): db.update(sql) print '.', print '\nInit database ok.' email = raw_input('Super admin email: ').strip().lower() passwd = raw_input('Super admin password: '******'iTranswarp', 'localhost') if db.select_int('select count(*) from mysql.user where user=?', 'www-data')==0: db.update('create user \'www-data\'@\'localhost\' identified by \'www-data\'') db.update('grant select,insert,update,delete on itrans.* to \'www-data\'@\'localhost\' identified by \'www-data\'') db.update('update users set role_id=0, passwd=? where email=?', passwd, email) print 'Install successfully!' except Exception, e: print 'Install failed:', e.message raise
def count_by(cls, where, *args): ''' 执行select count(pk) from table where...语句进行查询,返回一个数值 ''' return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
def count_all(cls): ''' Get count of rows in table. ''' return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name, cls.__table__))
def count_all(cls): ''' 执行select count(pk) from table语句,返回一个数值 ''' return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name, cls.__table__))
def count_by(cls, where, *args): ''' Get count of rows that find by where clause. ''' return db.select_int('select count(`%s`) from `%s` where %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
def count_all(cls): ''' Find by 'select count(pk) from table' and return integer. ''' return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name, cls.__table__))
def _count_articles(published_only=True): if published_only: return db.select_int('select count(id) from articles where website_id=? and draft=?', ctx.website.id, False) return db.select_int('select count(id) from articles where website_id=?', ctx.website.id)
def count_by(cls, where, *args): ''' Find by 'select count(pk) from table where ... ' and return int. ''' return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
def count_all(cls): ''' Find by 'select count(pk) from table' and return integer. ''' return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__, cls.__table__))
def count_by(cls,where,*args): return db.select_int('select count(%s) from %s %s'%(cls.__primary_key__,cls.__table__,where),*args)
def count_all(cls): return db.select_int('select count(%s) from %s'%(cls.__primary_key__,cls.__table__))