view piecrust/taxonomies.py @ 380:f33712c4cfab

routing: Fix bugs with matching URLs with correct route but missing metadata. When matching a route like `/foo/%slug%` against an URL like `/foo`, the route will (correctly) return a match, but it will be completely missing the `slug` metadata, resulting in problems elsewhere. This change makes it so that any missing route metadata will be filled in with an empty string. And because this means generated URLs may differ from the incoming URL when using trailing slashes (`/foo/` _vs._ `/foo`), we make the assert in the chef server handle those discrepancies.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 10 May 2015 00:34:21 -0700
parents 6c5de6edacf7
children e7b865f8f335
line wrap: on
line source

from piecrust.sources.pageref import PageRef, PageNotFoundError


class Taxonomy(object):
    def __init__(self, app, name, config):
        self.app = app
        self.name = name
        self.term_name = config.get('term', name)
        self.is_multiple = config.get('multiple', False)
        self.page_ref = config.get('page')
        self._source_page_refs = {}

    @property
    def setting_name(self):
        if self.is_multiple:
            return self.name
        return self.term_name

    def resolvePagePath(self, source_name):
        pr = self.getPageRef(source_name)
        try:
            return pr.path
        except PageNotFoundError:
            return None

    def getPageRef(self, source_name):
        if source_name in self._source_page_refs:
            return self._source_page_refs[source_name]

        source = self.app.getSource(source_name)
        ref_path = (source.getTaxonomyPageRef(self.name) or
                '%s:%s' % (source_name, self.page_ref))
        page_ref = PageRef(self.app, ref_path)
        self._source_page_refs[source_name] = page_ref
        return page_ref