def get_content_list(per_page=20): """ Create a query over ``Content`` objects using query string parameters. :param per_page: number of items to return per page :returns: ``QueryResult`` object """ search = request.params.getunicode('q', '').strip() status = request.params.get('status') license = request.params.get('license') votes = request.params.get('votes') page = int(request.params.get('p', '1')) q = Content.query() if search: keywords = Content.get_keywords(search) if len(keywords) > 1: q = q.filter(ndb.AND(*[Content.keywords == kw for kw in keywords])) if len(keywords) == 1: q = q.filter(Content.keywords == keywords[0]) if status: q = q.filter(Content.status == status) if license == 'free': q = q.filter(Content.is_free == True) elif license == 'nonfree': q = q.filter(Content.is_free == False) elif license == 'unknown': q = q.filter(Content.license == None) if votes == 'asc': q = q.order(+Content.votes) elif votes == 'desc': q = q.order(-Content.votes) q = q.order(-Content.updated) count = q.count() if not count: return QueryResult([], count, 1, 1) npages = int(math.ceil(count / per_page)) if page * per_page > count: page = npages offset = int(per_page * (page - 1)) return QueryResult(q.fetch(per_page, offset=offset), count, page, npages)
def get_content_list(): """ Create a query over ``Content`` objects using query string parameters. :param per_page: number of items to return per page :returns: ``QueryResult`` object """ search = request.params.getunicode('q', '').strip() archive = request.params.get('archives') license = request.params.get('license') votes = request.params.get('votes') notes = request.params.get('notes') try: page = int(request.params.get('p', '1')) except ValueError: page = 1 try: per_page = int(request.params.get('pp', '20')) except ValueError: per_page = 10 if per_page not in ALLOWED_PER_PAGE: per_page = 10 q = Content.query() if search: keywords = Content.get_keywords(search) q = q.filter(ndb.AND(*[Content.keywords == kw for kw in keywords])) if archive: q = q.filter(Content.archive == archive) if license: if license == 'NONE': q = q.filter(Content.license == None) elif license == 'FREE': q = q.filter(Content.license.IN(Content.FREE_LICENSES)) elif license == 'NONFREE': q = q.filter(Content.license.IN(Content.NONFREE_LICENSES)) else: q = q.filter(Content.license == license) if votes == 'asc': q = q.order(+Content.votes) elif votes == 'desc': q = q.order(-Content.votes) elif votes == 'cont': q = q.filter( ndb.AND(Content.votes_ratio <= 1.2, Content.votes_ratio >= 0.8)) q = q.order(-Content.votes_ratio) if notes == '1': q = q.filter(Content.has_notes == True) elif notes == '0': q = q.filter(Content.has_notes == False) q = q.order(-Content.updated) count = q.count() if not count: return QueryResult([], count, 1, 1) npages = int(math.ceil(count / per_page)) if page * per_page > count: page = npages offset = int(per_page * (page - 1)) items = q.fetch(per_page, offset=offset) return QueryResult(items, count, per_page, page)