def paginate_self(self, per_page=None, page=None): from flask_mongoengine.pagination import Pagination as PG page = int(request.values.get('page', 1)) if page is None else int(page) per_page = int(request.values.get('items', 10)) if per_page is None else int(per_page) pagination = PG(self.objects.all(), page, per_page) pagination.items = [self.transformer(item) for item in pagination.items] return pagination
def index(): # Pagination. page = request.args.get('page', 1, type=int) spu_all = Spu.query.add_descending('createdAt').find() pg = Pagination(spu_all, page, 20) # Get and arrange the item data to render. items = [] for spu_obj in pg.items: spu = spu_obj.dump() sku_objs = Sku.query \ .equal_to('spu', spu_obj) \ .add_ascending('price').find() skus = [sku_obj.dump() for sku_obj in sku_objs] items.append({'spu': spu, 'skus': skus}) # Form to input the URL. url = None form = UrlForm() if form.validate_on_submit(): url = form.url.data parse_new(url) form.url.data = '' return redirect(url_for('index')) return render_template('index.html', form=form, items=items, pagination=pg, current_time=datetime.utcnow())
def my_doc_meta(): """Get all documents meta data of user""" page = request.args.get('page', 1, type=int) category = request.args.get( 'category', Category.SHORT_TERM.value, type=str) search = request.args.get('search', None) filter = {'create_by': current_user.id} if category != ALL_CATEGORY: filter['category'] = category documents = DocumentMeta.objects(**filter).exclude('cache') if search: documents = documents.search_text(search) documents = documents.order_by('$text_score') else: documents = list(documents.aggregate(*MY_DOC_PIPELINE)) documents = Pagination(documents, page=page, per_page=DOCUMENT_PER_PAGE) return render_template( 'task/document_dashboard.html', current_category=category, current_search=search, documents=documents)
def home(): pipeline = [{ "$sample": { "size": SAMPLE_SIZE } }, { "$project": { "_id": 1, "band_name": 1, "strapline": 1, "hometown": 1, "profile": 1, "description": 1, "genres": 1, "media_assets": 1, "letter": { "$toLower": { "$substr": ["$catalogue_name", 0, 1] } } } }] bands = list(Band.objects().aggregate(pipeline)) bands = Pagination(bands, 1, SAMPLE_SIZE) count = bands.pages if bands.total > 0: bands_total = bands.total alphabet = {key: 0 for key in ALPHABET} return render_template("bands_list_home.html", bands=bands, count=count, bands_total=bands_total, display_breadcrumbs=False, alphabet=alphabet) else: return redirect(url_for('user.initial_setup'))
def manage_bands_home(band_view='recent', letter='a'): letter = '_' if letter == '1' else letter user = User.objects(id=current_user.id).first() page = request.args.get("page", 1, type=int) if band_view == 'recent': bands = Band.objects(created_by=user).order_by( '-date_updated', '-date_created').paginate(per_page=PER_PAGE, page=page) alphabet = {key: 0 for key in ALPHABET} return render_template("manage_bands.html", bands=bands, display_breadcrumbs=True, alphabet=alphabet) else: pipeline = [{ "$match": { "created_by": user.id } }, { "$facet": { "numbers_by_letter": [{ "$group": { "_id": { "$substr": ["$catalogue_name", 0, 1] }, "number_of_bands": { "$sum": 1 } } }], "bands_by_letter": [{ "$match": { "catalogue_name": { "$regex": f"^{str(letter.upper())}" } } }, { "$project": { "_id": 1, "band_name": 1, "catalogue_name": 1, "strapline": 1, "hometown": 1, "profile": 1, "description": 1, "genres": 1, "media_assets": 1, "letter": { "$toLower": { "$substr": ["$catalogue_name", 0, 1] } } } }, { "$sort": { "catalogue_name": 1 } }] } }] result = list(Band.objects.aggregate(pipeline))[0] bands_by_letter = Pagination(result["bands_by_letter"], int(page), 12) _alphabet = 'abcdefghijklmnopqrstuvwxyz_' alphabet = { obj["_id"].lower(): int(obj["number_of_bands"]) for obj in result["numbers_by_letter"] } alphabet = {key: alphabet.setdefault(key, 0) for key in _alphabet} alphabet['1'] = alphabet.pop( '_') #swap out for letter links 1 == '#' === '_' count = bands_by_letter.pages if bands_by_letter.total > 0: bands_total = bands_by_letter.total #bands = Band.objects(created_by=user).order_by('-catalogue_name').paginate(per_page=PER_PAGE, page=page) return render_template("manage_bands.html", bands=bands_by_letter, bands_total=bands_total, count=count, display_breadcrumbs=True, alphabet=alphabet) else: return 'no bands created' #redirect
def results(): search_terms = '' filters = {} text_query = None search = None town = None page = request.args.get("page", 1, type=int) if request.args: text_query = request.args.get("q") print(text_query) if text_query != None and len(text_query) == 0: text_query = None genres = request.args.getlist("genres") andor = request.args.get("andor") letter = request.args.get( "letter") if "letter" in request.args else "a" letter = '_' if letter == '1' else letter counties = request.args.getlist("counties") #print(counties) town = request.args.get("town") _request_arqs = request.args.to_dict(flat=False) _search = { key: _request_arqs[key] for key in _request_arqs if key not in ["page", "letter", "munster", "leinster", "connaght", "ulster"] } #re.sub('\=[0-9]*', '=', search) search = urllib.parse.urlencode(_search, doseq=True) # towns if town: search_terms += f"in {town}, County {counties[0]}" bands = Band.objects(hometown__town__iexact=town).paginate( per_page=PER_PAGE, page=page) search_terms = search_terms.replace( 'bands', 'band') if bands.total == 1 else search_terms return render_template("bands_list_results.html", bands=bands, search=search, count=bands.pages, bands_total=bands.total, search_terms=search_terms) # build search_terms text string if len(genres) > 0: if andor == 'f': search_terms = f"<b>{genres[0]}</b> bands" if ( len(genres) <= 1) else "<b>" + ", ".join( genres[:-1]) + " or " + genres[-1] + " bands</b>" else: search_terms = f"<b>{genres[0]}</b> bands" if ( len(genres) <= 1 ) else "<b>" + "-".join(genres) + " bands</b>" if len(counties) > 0: search_terms += f" in County {counties[0].title()}" if ( len(counties) <= 1) else " in counties: " + ", ".join( counties[:-1]) + " and " + counties[-1] if len(genres) == 0 and len(counties) == 0: search_terms = "bands" if text_query: search_terms += f" containing <b>'{text_query}'</b>" if text_query == None: search_terms += f" beginning with the letter <b>{letter.upper()}</b>" # build QSet filters ala mongoengine if len(genres) == 0: pass else: if len(genres) == 1: genres = genres.pop() filters['genres'] = genres else: if andor == "t": filters['genres__all'] = genres else: filters['genres__in'] = genres if len(counties) > 0: filters['hometown__county__in'] = counties if text_query != None: pipeline = [{ "$match": { "$text": { "$search": text_query } } }] #text has to be first pipeline if 'genres' in filters.keys(): pipeline.append({"$match": {"genres": filters['genres']}}) if 'genres__in' in filters.keys(): pipeline.append( {"$match": { "genres": { "$in": filters['genres__in'] } }}) if 'genres__all' in filters.keys(): pipeline.append( {"$match": { "genres": { "$all": filters['genres__all'] } }}) if 'genres__all' in filters.keys(): pipeline.append( {"$match": { "genres": { "$all": filters['genres__all'] } }}) if 'hometown__county__in' in filters.keys(): pipeline.append({ "$match": { "hometown.county": { "$in": filters['hometown__county__in'] } } }) # end with facets pipeline.extend([{ "$facet": { "numbers_by_letter": [{ "$group": { "_id": { "$substr": ["$catalogue_name", 0, 1] }, "number_of_bands": { "$sum": 1 } } }], "bands_by_letter": [{ "$project": { "_id": 1, "band_name": 1, "strapline": 1, "hometown": 1, "profile": 1, "description": 1, "genres": 1, "media_assets": 1, "letter": { "$toLower": { "$substr": ["$catalogue_name", 0, 1] } } } }] } }]) result = list(Band.objects.aggregate(pipeline))[0] bands = Pagination(result["bands_by_letter"], page, 12) _alphabet = 'abcdefghijklmnopqrstuvwxyz_' alphabet = { obj["_id"].lower(): int(obj["number_of_bands"]) for obj in result["numbers_by_letter"] } alphabet = {key: alphabet.setdefault(key, 0) for key in _alphabet} alphabet['1'] = alphabet.pop( '_') #swap out for letter links 1 == '#' === '_' count = bands.pages if bands.total > 0: bands_total = bands.total search_terms = search_terms.replace( 'bands', 'band') if bands_total == 1 else search_terms return render_template("bands_list_results.html", bands=bands, search=search, count=count, bands_total=bands_total, search_terms=search_terms, letter=letter) else: return render_template("bands_list_results.html", display_breadcrumbs=True, search_terms=search_terms, no_results=True) else: if not filters: # mobile test, smaller PER_PAGE? return redirect(url_for("public.home")) if filters: queryset = Q(**filters) # use _AND or _OR Q Node Combinations pipeline = [{ "$facet": { "numbers_by_letter": [{ "$group": { "_id": { "$substr": ["$catalogue_name", 0, 1] }, "number_of_bands": { "$sum": 1 } } }], "bands_by_letter": [{ "$match": { "catalogue_name": { "$regex": f"^{letter.upper()}" } } }, { "$project": { "_id": 1, "band_name": 1, "strapline": 1, "hometown": 1, "profile": 1, "description": 1, "genres": 1, "media_assets": 1, "letter": { "$toLower": { "$substr": ["$catalogue_name", 0, 1] } } } }, { "$sort": { "catalogue_name": 1 } }] } }] result = list(Band.objects.filter(queryset).aggregate(pipeline))[0] bands = Pagination(result["bands_by_letter"], page, 12) _alphabet = 'abcdefghijklmnopqrstuvwxyz_' alphabet = { obj["_id"].lower(): int(obj["number_of_bands"]) for obj in result["numbers_by_letter"] } alphabet = {key: alphabet.setdefault(key, 0) for key in _alphabet} alphabet['1'] = alphabet.pop( '_') #swap out for letter links 1 == '#' === '_' count = bands.pages if bands.total > 0: bands_total = bands.total total_bands_in_query = sum(alphabet.values()) search_terms += f'; there are {total_bands_in_query} resullts in total - use the alphabet to navigate between them.' return render_template("bands_list_results.html", bands=bands, search=search, count=count, bands_total=bands_total, search_terms=search_terms, alphabet=alphabet, letter=letter) elif len(list(Band.objects.filter( queryset))) > 0: #some results irrespective of letter alphaset = [ ltr for ltr in alphabet.keys() if alphabet[ltr] > 0 ] return render_template("bands_list_results.html", closest_letters=closest_letters( alphaset, letter), search=search, display_breadcrumbs=True, alphabet=alphabet, letter=letter) else: return render_template("bands_list_results.html", display_breadcrumbs=True, search_terms=search_terms, no_results=True)
def get_by_genre(genre, letter, page): letter = '_' if letter == '1' else letter.lower() pipeline = [{ "$match": { "genres": genre.lower() } }, { "$facet": { "numbers_by_letter": [{ "$group": { "_id": { "$substr": ["$catalogue_name", 0, 1] }, "number_of_bands": { "$sum": 1 } } }], "bands_by_letter": [{ "$match": { "catalogue_name": { "$regex": f"^{str(letter.upper())}" } } }, { "$project": { "_id": 1, "catalogue_name": 1, "band_name": 1, "strapline": 1, "hometown": 1, "profile": 1, "description": 1, "genres": 1, "media_assets": 1, "letter": { "$toLower": { "$substr": ["$catalogue_name", 0, 1] } } } }, { "$sort": { "catalogue_name": 1 } }] } }] result = list(Band.objects.aggregate(pipeline))[0] bands_by_letter = Pagination(result["bands_by_letter"], int(page), 12) _alphabet = 'abcdefghijklmnopqrstuvwxyz_' alphabet = { obj["_id"].lower(): int(obj["number_of_bands"]) for obj in result["numbers_by_letter"] } alphabet = {key: alphabet.setdefault(key, 0) for key in _alphabet} alphabet['1'] = alphabet.pop( '_') #swap out for letter links 1 == '#' === '_' count = bands_by_letter.pages if bands_by_letter.total > 0: bands_total = bands_by_letter.total return render_template("bands_list.html", bands=bands_by_letter, count=count, bands_total=bands_total, display_breadcrumbs=True, alphabet=alphabet, letter=letter) else: return 'no bands' #closest letters_tup = closest_letters(alphaset, letter)
def a2z(letter, bname=None): page = request.args.get("page", 1, type=int) letter = '_' if letter == '1' else letter pipeline = [{ "$match": {} }, { "$facet": { "numbers_by_letter": [{ "$group": { "_id": { "$substr": ["$catalogue_name", 0, 1] }, "number_of_bands": { "$sum": 1 } } }], "bands_by_letter": [{ "$match": { "catalogue_name": { "$regex": f"^{letter.upper()}" } } }, { "$project": { "_id": 1, "band_name": 1, "catalogue_name": 1, "strapline": 1, "hometown": 1, "profile": 1, "description": 1, "genres": 1, "media_assets": 1, "letter": { "$toLower": { "$substr": ["$catalogue_name", 0, 1] } } } }, { "$sort": { "catalogue_name": 1 } }] } }] result = list(Band.objects.aggregate(pipeline))[0] bands_by_letter = Pagination(result["bands_by_letter"], page, PER_PAGE) _alphabet = 'abcdefghijklmnopqrstuvwxyz_' alphabet = { obj["_id"].lower(): int(obj["number_of_bands"]) for obj in result["numbers_by_letter"] } alphabet = {key: alphabet.setdefault(key, 0) for key in _alphabet} alphabet['1'] = alphabet.pop( '_') #swap out for letter links 1 == '#' === '_' count = bands_by_letter.pages if bands_by_letter.total > 0: bands_total = bands_by_letter.total return render_template("bands_list.html", bands=bands_by_letter, count=count, bands_total=bands_total, display_breadcrumbs=True, alphabet=alphabet, letter=letter) else: alphaset = [ltr for ltr in alphabet.keys() if alphabet[ltr] > 0] return render_template("bands_list.html", closest_letters=closest_letters( alphaset, letter), display_breadcrumbs=True, alphabet=alphabet, letter=letter) #closest