def _format_path(self, path_format): podspec = self.pod.get_podspec() locale = self.locale.alias if self.locale is not None else self.locale formatters = { 'base': self.base, 'category': self.category, 'collection': structures.AttributeDict( base_path=self.collection_base_path, basename=self.collection.basename, root=self.collection.root), 'env': structures.AttributeDict( fingerpint=self.pod.env.fingerprint), 'locale': locale, 'parent': self.parent if self.parent else utils.DummyDict(), 'root': podspec.root, 'slug': self.slug, } if '{date' in path_format: if isinstance(self.date, datetime.datetime): formatters['date'] = self.date.date() else: formatters['date'] = self.date if '|lower' in path_format: for key, value in formatters.items(): if isinstance(value, basestring): formatters['{}|lower'.format(key)] = value.lower() path = path_format.format(**formatters) while '//' in path: path = path.replace('//', '/') return path
def format_view(self, doc, path, parameterize=False): """Format a URL path using the doc information for views.""" path = '' if path is None else path # Most params should always be replaced. params = self.params_pod() params['base'] = doc.base params['category'] = doc.category params['collection'] = structures.AttributeDict( base_path=doc.collection_base_path, basename=doc.collection.basename, root=doc.collection.root) params['parent'] = doc.parent if doc.parent else utils.DummyDict() params['slug'] = doc.slug if isinstance(doc.date, datetime.datetime): params['date'] = doc.date.date() else: params['date'] = doc.date if '|lower' in path: for key, value in params.items(): if isinstance(value, basestring): params['{}|lower'.format(key)] = value.lower() path = utils.safe_format(path, **params) if parameterize: path = self.parameterize(path) return self.strip_double_slash(path)
def params_doc(self, path, doc): """Selective access to the document properties depending on path.""" params = {} # Remove the base when the path ends with the base and is index. if doc.base == 'index' and path.endswith(INDEX_BASE_ENDINGS): params['base'] = '' else: params['base'] = doc.base params['collection'] = structures.AttributeDict( base_path=doc.collection_base_path, sub_path=doc.collection_base_path, basename=doc.collection.basename, root=doc.collection.root) if '{category}' in path: params['category'] = doc.category if '{parent}' in path: params['parent'] = doc.parent if doc.parent else utils.DummyDict() if '{slug}' in path: params['slug'] = doc.slug if '{date}' in path: if isinstance(doc.date, datetime.datetime): params['date'] = doc.date.date() else: params['date'] = doc.date return params
def params_doc(self, path, doc): """Selective access to the document properties depending on path.""" params = {} params['base'] = doc.base params['collection'] = structures.AttributeDict( base_path=doc.collection_base_path, basename=doc.collection.basename, root=doc.collection.root) if '{category}' in path: params['category'] = doc.category if '{parent}' in path: params['parent'] = doc.parent if doc.parent else utils.DummyDict() if '{slug}' in path: params['slug'] = doc.slug if '{date}' in path: if isinstance(doc.date, datetime.datetime): params['date'] = doc.date.date() else: params['date'] = doc.date return params
def format_doc(self, doc, path, locale=None, parameterize=False): """Format a URL path using the doc information.""" path = '' if path is None else path path = self.format_pod(path) # Most params should always be replaced. params = SafeDict() params['base'] = doc.base params['category'] = doc.category params['collection'] = doc.collection params['parent'] = doc.parent if doc.parent else utils.DummyDict() params['slug'] = doc.slug if isinstance(doc.date, datetime.datetime): params['date'] = doc.date.date() else: params['date'] = doc.date if '|lower' in path: for key, value in params.items(): if isinstance(value, basestring): params['{}|lower'.format(key)] = value.lower() path = self.formatter.vformat(path, (), params) if parameterize: path = self.parameterize(path) params = SafeDict() if locale is None: locale = doc.locale params['locale'] = self._locale_or_alias(locale) path = self.formatter.vformat(path, (), params) return self.strip_double_slash(path)