comparison piecrust/page.py @ 856:9bb22bbe093c

refactor: Make the blog archives functional again. The blog archives are using the same pattern as the taxonomy support.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 06 Jun 2017 01:23:25 -0700
parents f070a4fc033c
children d1095774bfcf
comparison
equal deleted inserted replaced
855:448710d84121 856:9bb22bbe093c
87 87
88 @property 88 @property
89 def datetime(self): 89 def datetime(self):
90 if self._datetime is None: 90 if self._datetime is None:
91 try: 91 try:
92 self._datetime = self._computeDateTime() 92 self._datetime = _compute_datetime(self.source_metadata,
93 self.config)
93 except Exception as ex: 94 except Exception as ex:
94 logger.exception(ex) 95 logger.exception(ex)
95 raise Exception( 96 raise Exception(
96 "Error computing time for page: %s" % 97 "Error computing time for page: %s" %
97 self.content_spec) from ex 98 self.content_spec) from ex
110 route_params = self.source_metadata['route_params'] 111 route_params = self.source_metadata['route_params']
111 return self.route.getUri(route_params, sub_num=sub_num) 112 return self.route.getUri(route_params, sub_num=sub_num)
112 113
113 def getSegment(self, name='content'): 114 def getSegment(self, name='content'):
114 return self.segments[name] 115 return self.segments[name]
115
116 def _computeDateTime(self):
117 if 'datetime' in self.source_metadata:
118 # Get the date/time from the source.
119 self._datetime = self.source_metadata['datetime']
120 elif 'date' in self.source_metadata:
121 # Get the date from the source. Potentially get the
122 # time from the page config.
123 page_date = self.source_metadata['date']
124 page_time = _parse_config_time(self.config.get('time'))
125 if page_time is not None:
126 self._datetime = datetime.datetime(
127 page_date.year,
128 page_date.month,
129 page_date.day) + page_time
130 else:
131 self._datetime = datetime.datetime(
132 page_date.year, page_date.month, page_date.day)
133 elif 'date' in self.config:
134 # Get the date from the page config, and maybe the
135 # time too.
136 page_date = _parse_config_date(self.config.get('date'))
137 self._datetime = datetime.datetime(
138 page_date.year,
139 page_date.month,
140 page_date.day)
141 page_time = _parse_config_time(self.config.get('time'))
142 if page_time is not None:
143 self._datetime += page_time
144 else:
145 # No idea what the date/time for this page is.
146 self._datetime = datetime.datetime.fromtimestamp(0)
147 116
148 def _load(self): 117 def _load(self):
149 if self._config is not None: 118 if self._config is not None:
150 return 119 return
151 120
162 131
163 self._config = config 132 self._config = config
164 self._segments = content 133 self._segments = content
165 if was_cache_valid: 134 if was_cache_valid:
166 self._flags |= FLAG_RAW_CACHE_VALID 135 self._flags |= FLAG_RAW_CACHE_VALID
136
137
138 def _compute_datetime(source_metadata, config):
139 # Get the date/time from the source.
140 dt = source_metadata.get('datetime')
141 if dt is not None:
142 return dt
143
144 # Get the date from the source. Potentially get the
145 # time from the page config.
146 page_date = source_metadata.get('date')
147 if page_date is not None:
148 dt = datetime.datetime(
149 page_date.year, page_date.month, page_date.day)
150
151 page_time = _parse_config_time(config.get('time'))
152 if page_time is not None:
153 dt += page_time
154
155 return dt
156
157 # Get the date from the page config, and maybe the
158 # time too.
159 page_date = _parse_config_date(config.get('date'))
160 if page_date is not None:
161 dt = datetime.datetime(
162 page_date.year, page_date.month, page_date.day)
163
164 page_time = _parse_config_time(config.get('time'))
165 if page_time is not None:
166 dt += page_time
167
168 return dt
169
170 # No idea what the date/time for this page is.
171 return datetime.datetime.fromtimestamp(0)
167 172
168 173
169 def _parse_config_date(page_date): 174 def _parse_config_date(page_date):
170 if page_date is None: 175 if page_date is None:
171 return None 176 return None