def dispatch_request(self, category='', flav=None, *args, **kwargs): """Query whoosh for collection of articles. Sort the articles. Setup up pagination variables in the g variables. Finally render the template, or abort with a 404 if you don't find a template. """ ainfos, total = [], 0 if is_loaded('yawtext.indexer.YawtIndexer'): query = self.query(category, *args, **kwargs) sortfield = current_app.config['YAWT_COLLECTIONS_SORT_FIELD'] ainfos, total = search_page(query=query, sortedby=sortfield, page=g.page, pagelen=g.pagelen, reverse=True) g.total_results = total g.total_pages = int(ceil(float(g.total_results)/g.pagelen)) g.has_prev_page = g.page > 1 g.has_next_page = g.page < g.total_pages g.prev_page = g.page - 1 g.next_page = g.page + 1 articles = [] for ainfo in ainfos: if self.is_load_articles(flav): article = g.site.fetch_article_by_info(ainfo) else: article = Article() article.info = ainfo articles.append(article) try: return render(self.get_template_name(), category, 'index', flav, {'articles': articles}) except TemplatesNotFound: abort(404)
def _handle_path(path): """ Returns template source corresponding to path """ current_app.logger.debug('handling path: ' + path) config = current_app.config fullname = None flavour = config['YAWT_DEFAULT_FLAVOUR'] path = path.lstrip('/') if path == '' or path.endswith('/'): # user asked for a category page, without an index. # Supply index file. fullname = path + config['YAWT_INDEX_FILE'] else: pattern = re.compile(r'^(.*?)\.([^/.]+)$') match = pattern.match(path) if match: # we have a flavour ending path, which means the user is # requesting a file with particular flavour fullname = match.group(1) flavour = match.group(2) elif g.site.category_exists(path): return redirect('/' + path + '/') else: fullname = path current_app.logger.debug('fullname requested: ' + fullname) current_app.logger.debug('flavour requested: ' + flavour) try: article = g.site.fetch_article(fullname) except ArticleNotFoundError: current_app.logger.debug('no article found at ' + fullname + ', handling the 404') result = _handle_404(fullname, flavour) if not result: abort(404) else: return result else: try: return render('article', article.info.category, article.info.slug, flavour, {'article': article}) except TemplatesNotFound: current_app.logger.debug('could not find, aborting with 404') abort(404)
def dispatch_request(self, category=None, year=None, month=None, day=None, slug=None, flav=None): datefield = current_app.config['YAWT_ARCHIVE_DATEFIELD'] ainfos, _ = search_page(_query(category, year, month, day), datefield, g.page, g.pagelen, True) article = None for info in ainfos: if info.slug == slug: article = g.site.fetch_article(info.fullname) return render(self.get_template_name(), category, slug, flav, {'article': article, 'is_permalink': True})