Mercurial > piecrust2
comparison piecrust/sources/posts.py @ 892:c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Sun, 02 Jul 2017 22:20:32 -0700 |
| parents | d9059257743c |
| children | 86b684cc0551 |
comparison
equal
deleted
inserted
replaced
| 891:f13d618cfec6 | 892:c445a3d5d950 |
|---|---|
| 152 "in `year/month/day` format (defaults to today).") | 152 "in `year/month/day` format (defaults to today).") |
| 153 parser.add_argument('slug', help="The URL slug for the new post.") | 153 parser.add_argument('slug', help="The URL slug for the new post.") |
| 154 | 154 |
| 155 def createContent(self, args): | 155 def createContent(self, args): |
| 156 dt = datetime.date.today() | 156 dt = datetime.date.today() |
| 157 if args.date: | 157 date = args.get('date') |
| 158 if args.date == 'today': | 158 if isinstance(date, str): |
| 159 if date == 'today': | |
| 159 pass # Keep the default we had. | 160 pass # Keep the default we had. |
| 160 elif args.date == 'tomorrow': | 161 elif date == 'tomorrow': |
| 161 dt += datetime.timedelta(days=1) | 162 dt += datetime.timedelta(days=1) |
| 162 elif args.date.startswith('+'): | 163 elif date.startswith('+'): |
| 163 try: | 164 try: |
| 164 dt += datetime.timedelta(days=int(args.date.lstrip('+'))) | 165 dt += datetime.timedelta(days=int(date.lstrip('+'))) |
| 165 except ValueError: | 166 except ValueError: |
| 166 raise Exception("Date offsets must be numbers.") | 167 raise Exception("Date offsets must be numbers.") |
| 167 else: | 168 else: |
| 168 try: | 169 try: |
| 169 year, month, day = [int(s) for s in args.date.split('/')] | 170 year, month, day = [int(s) for s in date.split('/')] |
| 170 except ValueError: | 171 except ValueError: |
| 171 raise Exception("Dates must be of the form: " | 172 raise Exception("Dates must be of the form: " |
| 172 "YEAR/MONTH/DAY.") | 173 "YEAR/MONTH/DAY.") |
| 173 dt = datetime.date(year, month, day) | 174 dt = datetime.date(year, month, day) |
| 174 | 175 elif isinstance(date, datetime.datetime): |
| 175 slug, ext = os.path.splitext(args.slug) | 176 year = date.year |
| 177 month = date.month | |
| 178 day = date.day | |
| 179 else: | |
| 180 raise Exception("Unknown date: %s" % date) | |
| 181 | |
| 182 slug, ext = os.path.splitext(args.get('slug')) | |
| 176 if not ext: | 183 if not ext: |
| 177 ext = self.default_auto_format | 184 ext = self.default_auto_format |
| 178 year, month, day = dt.year, dt.month, dt.day | 185 year, month, day = dt.year, dt.month, dt.day |
| 179 tokens = { | 186 tokens = { |
| 180 'slug': args.slug, | 187 'slug': args.get('slug'), |
| 181 'ext': ext, | 188 'ext': ext, |
| 182 'year': '%04d' % year, | 189 'year': '%04d' % year, |
| 183 'month': '%02d' % month, | 190 'month': '%02d' % month, |
| 184 'day': '%02d' % day | 191 'day': '%02d' % day |
| 185 } | 192 } |
| 186 rel_path = self.path_format % tokens | 193 rel_path = self.path_format % tokens |
| 187 path = os.path.join(self.fs_endpoint_path, rel_path) | 194 path = os.path.join(self.fs_endpoint_path, rel_path) |
| 188 metadata = { | 195 metadata = self._parseMetadataFromPath(path) |
| 189 'config': {'title': uri_to_title(slug)} | 196 metadata['config'] = {'title': uri_to_title(slug)} |
| 190 } | |
| 191 return ContentItem(path, metadata) | 197 return ContentItem(path, metadata) |
| 192 | 198 |
| 193 def getInteractiveFields(self): | 199 def getInteractiveFields(self): |
| 194 dt = datetime.date.today() | 200 dt = datetime.date.today() |
| 195 return [ | 201 return [ |
