comparison piecrust/routing.py @ 147:ab6e7e0e9d44

Pass date information to routing when building URLs. This is so that URLs with dates in them can be built even when the date information is not coming from the source metadata, but from the page's config.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 29 Nov 2014 21:00:44 -0800
parents f485ba500df3
children a42469dbdc47
comparison
equal deleted inserted replaced
146:0609739169bd 147:ab6e7e0e9d44
6 6
7 7
8 route_re = re.compile(r'%((?P<qual>path):)?(?P<name>\w+)%') 8 route_re = re.compile(r'%((?P<qual>path):)?(?P<name>\w+)%')
9 template_func_re = re.compile(r'^(?P<name>\w+)\((?P<first_arg>\w+)(?P<other_args>.*)\)\s*$') 9 template_func_re = re.compile(r'^(?P<name>\w+)\((?P<first_arg>\w+)(?P<other_args>.*)\)\s*$')
10 template_func_arg_re = re.compile(r',\s*(?P<arg>\w+)') 10 template_func_arg_re = re.compile(r',\s*(?P<arg>\w+)')
11
12
13 class IRouteMetadataProvider(object):
14 def getRouteMetadata(self):
15 raise NotImplementedError()
11 16
12 17
13 class Route(object): 18 class Route(object):
14 """ Information about a route for a PieCrust application. 19 """ Information about a route for a PieCrust application.
15 Each route defines the "shape" of an URL and how it maps to 20 Each route defines the "shape" of an URL and how it maps to
46 return self.source.realm 51 return self.source.realm
47 52
48 def isMatch(self, source_metadata): 53 def isMatch(self, source_metadata):
49 return True 54 return True
50 55
51 def getUri(self, source_metadata): 56 def getUri(self, source_metadata, provider=None):
57 if provider:
58 source_metadata = dict(source_metadata)
59 source_metadata.update(provider.getRouteMetadata())
52 #TODO: fix this hard-coded shit 60 #TODO: fix this hard-coded shit
53 for key in ['year', 'month', 'day']: 61 for key in ['year', 'month', 'day']:
54 if key in source_metadata and isinstance(source_metadata[key], str): 62 if key in source_metadata and isinstance(source_metadata[key], str):
55 source_metadata[key] = int(source_metadata[key]) 63 source_metadata[key] = int(source_metadata[key])
56 return self.uri_root + (self.uri_format % source_metadata) 64 return self.uri_root + (self.uri_format % source_metadata)