def find_by_slug(self, slug): """Find all categories by slug sql like rule""" data = db.select(self.table).fields('title', 'slug', 'description', 'cid').condition('slug', slug).execute() if data: return self.load(data[0], self.model)
def find(self, cid): """Find category by category id, return the category model instance if category id exists in database""" data = db.select(self.table).fields('title', 'slug', 'description', 'cid').condition('cid', cid).execute() if data: return self.load(data[0], self.model)
def paginate(self, page=1, perpage=10, status='all'): q = db.select(self.table).fields('post_id', 'name', 'email', 'content', 'status', 'created', 'cid') if status != 'all': q.condition('status', status) results = q.limit(perpage).offset((page - 1) * perpage).order_by('created').execute() pages = [self.load(page, self.model) for page in results] return pages
def find_by_post_id(self, post_id, status='approved'): q = db.select(self.table).fields('post_id', 'name', 'email', 'content', 'status', 'created', 'cid').condition('post_id', post_id) if status: q.condition('status', status) data = q.execute() return [self.load(_, self.model) for _ in data]
def find(self, type, node_id, extend_id): data = (db.select(self.table).fields( 'node_id', 'type', 'extend', 'data', 'mid').condition('type', type).condition( 'node_id', node_id).condition('extend', extend_id).execute()) if data: return self.load(data[0])
def take(self, page=1, perpage=10): count = self.count() q = db.select(self.table).select('username', 'email', 'real_name', 'password', 'bio', 'status', 'role', 'uid') results = q.limit(perpage).offset((page - 1) * perpage).order_by('real_name', 'desc').execute() users = [self.load(user, self.model) for user in results] return users
def find_by_username(self, username): """Return user by username if find in database otherwise None""" data = (db.select(self.table).select( 'username', 'email', 'real_name', 'password', 'bio', 'status', 'role', 'uid').condition('username', username).execute()) if data: return self.load(data[0], self.model)
def serach_count(self, key): q = db.select(self.table).fields(db.expr('count(*)', 'total')).condition('status', 'published') _or = db.or_() _or.condition('slug', '%%%s%%' % key, 'LIKE').condition('title', '%%%s%%' % key, 'LIKE') q.condition(_or) return q.execute()[0][0]
def find(self, cid): data = db.select(self.table).fields('post_id', 'name', 'email', 'content', 'status', 'created', 'cid').condition('cid', cid).execute() if data: return self.load(data[0], self.model)
def find_by_type(self, type): """Find and load the extend from database by eid(extend id)""" data = (db.select(self.table).fields('type', 'key', 'label', 'field', 'attributes', 'eid').condition('type', type).execute()) return [self.load(_) for _ in data]
def find(self, type, node_id, extend_id): data = (db.select(self.table).fields('node_id', 'type', 'extend', 'data', 'mid') .condition('type', type) .condition('node_id', node_id) .condition('extend', extend_id) .execute()) if data: return self.load(data[0])
def find(self, uid): """Find and load the user from database by uid(user id)""" data = (db.select(self.table).select('username', 'email', 'real_name', 'password', 'bio', 'status', 'role', 'uid'). condition('uid', uid).execute() ) if data: return self.load(data[0], self.model)
def find_by_email(self, email): """Return user by email if find in database otherwise None""" data = (db.select(self.table).select('username', 'email', 'real_name', 'password', 'bio', 'status', 'role', 'uid'). condition('email', email).execute() ) if data: return self.load(data[0], self.model)
def menu(self, is_menu=False): q = db.select(self.table).fields('parent', 'name', 'title', 'slug', 'content', 'status', 'redirect', 'show_in_menu', 'pid').condition('show_in_menu', 1) if not is_menu: res = q.execute() else: res = q.condition('status', 'published').order_by('menu_order').execute() return [self.load(data,self.model) for data in res]
def get_published_posts(self, page=1, perpage=10, category=None): q = db.select(self.table).fields('title', 'slug', 'description', 'html', 'css', 'js', 'category', 'status', 'allow_comment', 'author', 'updated', 'created', 'pid') if category: q.condition('category', category) results = (q.limit(perpage).offset((page - 1) * perpage).condition('status', 'published') .order_by('created', 'DESC').execute()) return [self.load(data, self.model) for data in results]
def paginate(self, page=1, perpage=10, status='all'): q = db.select(self.table).fields('parent', 'name', 'title', 'slug', 'content', 'status', 'redirect', 'show_in_menu', 'pid') if status != 'all': q.condition('status', status) results = q.limit(perpage).offset((page - 1) * perpage).order_by('title', 'desc').execute() pages = [self.load(page, self.model) for page in results] return pages
def lists(self, exclude=None, sorted=False): q = db.select(self.table) if sorted: q.sort_by('key') if exclude: db.condition('key', exculde, '<>') res = q.execute() return [self.load(row, self.model) for row in res]
def find(self, eid): """Find and load the extend from database by eid(extend id)""" data = (db.select(self.table).fields('type', 'key', 'label', 'field', 'attributes', 'eid').condition('eid', eid).execute()) if data: return self.load(data[0])
def take(self, page=1, perpage=10): count = self.count() q = db.select(self.table).select('username', 'email', 'real_name', 'password', 'bio', 'status', 'role', 'uid') results = q.limit(perpage).offset( (page - 1) * perpage).order_by('real_name', 'desc').execute() users = [self.load(user, self.model) for user in results] return users
def search(self, key, page=1, perpage=10): q = db.select(self.table).fields('title', 'slug', 'description', 'html', 'css', 'js', 'category', 'status', 'allow_comment', 'author', 'updated', 'created', 'pid') _or = db.or_() _or.condition('slug', '%%%s%%' % key, 'LIKE').condition('title', '%%%s%%' % key, 'LIKE') q.condition(_or) results = (q.condition('status', 'published').limit(perpage).offset((page - 1) * perpage) .order_by('created', 'DESC').execute()) return [self.load(data, self.model) for data in results]
def find(self, uid): """Find and load the user from database by uid(user id)""" data = (db.select(self.table).select('username', 'email', 'real_name', 'password', 'bio', 'status', 'role', 'uid').condition('uid', uid).execute()) if data: return self.load(data[0], self.model)
def serach_count(self, key): q = db.select(self.table).fields(db.expr('count(*)', 'total')).condition( 'status', 'published') _or = db.or_() _or.condition('slug', '%%%s%%' % key, 'LIKE').condition('title', '%%%s%%' % key, 'LIKE') q.condition(_or) return q.execute()[0][0]
def find(self, pid): data = db.select(self.table).fields('title', 'slug', 'description', 'html', 'css', 'js', 'category', 'status', 'allow_comment', 'author', 'updated', 'created', 'pid').condition('pid', pid).execute() if data: return self.load(data[0], self.model)
def paginate(self, page=1, perpage=10, status='all'): q = db.select(self.table).fields('post_id', 'name', 'email', 'content', 'status', 'created', 'cid') if status != 'all': q.condition('status', status) results = q.limit(perpage).offset( (page - 1) * perpage).order_by('created').execute() pages = [self.load(page, self.model) for page in results] return pages
def search(self, **kw): """Find the users match the condition in kw""" q = db.select(self.table).condition('status', 'active') for k, v in kw: q.condition(k, v) data = q.execute() users = [] for user in data: users.append(self.load(user, self.model)) return users
def get_published_posts(self, page=1, perpage=10, category=None): q = db.select(self.table).fields('title', 'slug', 'description', 'html', 'css', 'js', 'category', 'status', 'allow_comment', 'author', 'updated', 'created', 'pid') if category: q.condition('category', category) results = (q.limit(perpage).offset((page - 1) * perpage).condition( 'status', 'published').order_by('created', 'DESC').execute()) return [self.load(data, self.model) for data in results]
def dropdown(self, show_empty_option=True, exclude=[]): items = [] if show_empty_option: items.append((0, '--')) pages = db.select(self.table).fields('pid', 'name').execute() for page in pages: if page[0] in exclude: continue items.append((page[0], page[1])) return items
def search(self, key, page=1, perpage=10): q = db.select(self.table).fields('title', 'slug', 'description', 'html', 'css', 'js', 'category', 'status', 'allow_comment', 'author', 'updated', 'created', 'pid') _or = db.or_() _or.condition('slug', '%%%s%%' % key, 'LIKE').condition('title', '%%%s%%' % key, 'LIKE') q.condition(_or) results = (q.condition('status', 'published').limit(perpage).offset( (page - 1) * perpage).order_by('created', 'DESC').execute()) return [self.load(data, self.model) for data in results]
def count(self, **kw): q = db.select(self.table).fields(db.expr('COUNT(*)')) if kw: for k, v in kw.iteritems(): q.condition(k, v) return q.execute()[0][0]
def find(self, key): data = db.select(self.table).condition('key', key).execute() if data: return self.load(data[0], self.model)
def find(self, id): q = db.select(self.table).condition(self.primary_id, id) data = q.query() if data: return self.load(data[0], self.model)
def count_slug(self, slug): return db.select(self.table).fields(db.expr('COUNT(*)')).condition('slug', slug).execute()[0][0]
def paginate(self, page=1, perpage=10): """Paginate the categories""" results = (db.select(self.table).fields('title', 'slug', 'description', 'cid') .limit(perpage).offset((page - 1) * perpage) .order_by('title').execute()) return [self.load(data, self.model) for data in results]
def category_count(self, category_id): return db.select(self.table).fields(db.expr( 'count(*)', 'total')).condition('category', category_id).condition( 'status', 'published').execute()[0][0]
def count(self): return db.select(self.table).fields( db.expr('COUNT(*)')).execute()[0][0]
def find_by_slug(self, slug): data = db.select(self.table).fields('parent', 'name', 'title', 'slug', 'content', 'status', 'redirect', 'show_in_menu', 'pid').condition('slug', slug).execute() if data: return self.load(data[0], self.model)
def count(self, status=None): q= db.select(self.table).fields(db.expr('COUNT(*)')) if status != 'all': q.condition('status', status) return q.execute()[0][0]
def field(self, type, key, eid=-1): field = db.select(self.table).fields( 'type', 'key', 'label', 'field', 'attributes', 'eid').condition('type', type).condition('key', key).execute() if field: return self.load(field[0])
def find(self, eid): """Find and load the extend from database by eid(extend id)""" data = (db.select(self.table).fields('type', 'key', 'label', 'field', 'attributes', 'eid'). condition('eid', eid).execute()) if data: return self.load(data[0])
def count(self): return db.select(self.table).fields(db.expr('COUNT(*)')).execute()[0][0]
def spam_count(self, domain): return db.select(self.table).fields(db.expr('COUNT(*)')).condition( 'email', domain, 'LIKE').execute()[0][0]
def find_by_type(self, type): """Find and load the extend from database by eid(extend id)""" data = (db.select(self.table).fields('type', 'key', 'label', 'field', 'attributes', 'eid'). condition('type', type).execute()) return [self.load(_) for _ in data]
def paginate(self, page=1, perpage=10): data = db.select(self.table).fields('type', 'key', 'label', 'field', 'attributes', 'eid').limit(perpage).offset((page - 1) * perpage).execute() return [self.load(_) for _ in data]
def field(self, type, key, eid=-1): field = db.select(self.table).fields('type', 'key', 'label', 'field', 'attributes', 'eid').condition('type', type).condition('key', key).execute() if field: return self.load(field[0])
def dropdown(self): """Returns the all category id""" return db.select(self.table).fields('cid', 'title').execute(as_dict=True)
def count(self, status=None): q = db.select(self.table).fields(db.expr('COUNT(*)')) if status: q.condition('status', status) return q.execute()[0][0]
def order_by_title(self): results = db.select(self.table).fields('title', 'slug', 'description', 'cid').order_by('title').execute() return [self.load(data, self.model) for data in results]