Mercurial > piecrust2
comparison piecrust/admin/views/micropub.py @ 1024:60b431c57ea9
admin: Handle multiple photo uploads in the micropub endpoint.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 12 Dec 2017 22:47:06 -0800 |
parents | 660250c95246 |
children | c8366fc15043 |
comparison
equal
deleted
inserted
replaced
1023:0302f690a4c5 | 1024:60b431c57ea9 |
---|---|
67 data = None | 67 data = None |
68 | 68 |
69 if data: | 69 if data: |
70 entry_type = _mf2get(data, 'type') | 70 entry_type = _mf2get(data, 'type') |
71 if entry_type == 'h-entry': | 71 if entry_type == 'h-entry': |
72 source_name, content_item = _create_hentry(data['properties']) | 72 source_name, content_item, do_publish = \ |
73 _run_publisher() | 73 _create_hentry(data['properties']) |
74 if do_publish: | |
75 _run_publisher() | |
74 return _get_location_response(source_name, content_item) | 76 return _get_location_response(source_name, content_item) |
75 | 77 |
76 else: | 78 else: |
77 logger.error("Post type '%s' is not supported." % post_type) | 79 logger.error("Post type '%s' is not supported." % post_type) |
78 else: | 80 else: |
92 abort(400) | 94 abort(400) |
93 return | 95 return |
94 | 96 |
95 fn = secure_filename(photo.filename) | 97 fn = secure_filename(photo.filename) |
96 fn = re_unsafe_asset_char.sub('_', fn) | 98 fn = re_unsafe_asset_char.sub('_', fn) |
97 fn = '%s_%s' % (str(uuid.uuid1()), fn) | 99 fn = '%s_%s' % (uuid.uuid1().hex, fn) |
100 fn = fn.rstrip('_') | |
98 | 101 |
99 photo_cache_dir = os.path.join( | 102 photo_cache_dir = os.path.join( |
100 g.site.root_dir, | 103 g.site.root_dir, |
101 CACHE_DIR, g.site.piecrust_factory.cache_key, | 104 CACHE_DIR, g.site.piecrust_factory.cache_key, |
102 'uploads') | 105 'uploads') |
269 g.site.root_dir, | 272 g.site.root_dir, |
270 CACHE_DIR, g.site.piecrust_factory.cache_key, | 273 CACHE_DIR, g.site.piecrust_factory.cache_key, |
271 'uploads') | 274 'uploads') |
272 | 275 |
273 for p_url in photo_urls: | 276 for p_url in photo_urls: |
274 _, __, p_url = p_url.rpartition('/') | 277 _, __, p_fn = p_url.rpartition('/') |
275 p_path = os.path.join(photo_cache_dir, p_url) | 278 p_cache_path = os.path.join(photo_cache_dir, p_fn) |
276 p_uuid, p_fn = p_url.split('_', 1) | 279 p_asset_path = os.path.join(photo_dir, p_fn) |
277 p_asset = os.path.join(photo_dir, p_fn) | 280 logger.info("Moving upload '%s' to '%s'." % |
278 logger.info("Moving upload '%s' to '%s'." % (p_path, p_asset)) | 281 (p_cache_path, p_asset_path)) |
279 try: | 282 try: |
280 os.rename(p_path, p_asset) | 283 os.rename(p_cache_path, p_asset_path) |
281 except OSError: | 284 except OSError: |
282 logger.error("Can't move '%s' to '%s'." % (p_path, p_asset)) | 285 logger.error("Can't move '%s' to '%s'." % |
286 (p_cache_path, p_asset_path)) | |
283 raise | 287 raise |
284 | 288 |
285 p_fn_no_ext, _ = os.path.splitext(p_fn) | 289 p_fn_no_ext, _ = os.path.splitext(p_fn) |
286 photo_names.append(p_fn_no_ext) | 290 photo_names.append(p_fn_no_ext) |
287 | 291 |
301 | 305 |
302 fn_no_ext, _ = os.path.splitext(fn) | 306 fn_no_ext, _ = os.path.splitext(fn) |
303 photo_names.append(fn_no_ext) | 307 photo_names.append(fn_no_ext) |
304 | 308 |
305 # Build the config. | 309 # Build the config. |
310 do_publish = True | |
306 post_config = {} | 311 post_config = {} |
307 if name: | 312 if name: |
308 post_config['title'] = name | 313 post_config['title'] = name |
309 if categories: | 314 if categories: |
310 post_config['tags'] = categories | 315 post_config['tags'] = categories |
312 post_config['location'] = location | 317 post_config['location'] = location |
313 if reply_to: | 318 if reply_to: |
314 post_config['reply_to'] = reply_to | 319 post_config['reply_to'] = reply_to |
315 if status and status != 'published': | 320 if status and status != 'published': |
316 post_config['draft'] = True | 321 post_config['draft'] = True |
322 do_publish = False | |
317 if post_format: | 323 if post_format: |
318 post_config['format'] = post_format | 324 post_config['format'] = post_format |
319 post_config['time'] = '%02d:%02d:%02d' % (now.hour, now.minute, now.second) | 325 post_config['time'] = '%02d:%02d:%02d' % (now.hour, now.minute, now.second) |
320 | 326 |
321 # If there's no title, this is a "microblogging" post. | 327 # If there's no title, this is a "microblogging" post. |
339 fp.write(content) | 345 fp.write(content) |
340 | 346 |
341 if photo_names: | 347 if photo_names: |
342 fp.write('\n\n') | 348 fp.write('\n\n') |
343 for pn in photo_names: | 349 for pn in photo_names: |
344 fp.write('<img src="{{assets.%s}}" alt="%s"/>\n\n' % | 350 fp.write('<img src="{{assets["%s"]}}" alt="%s"/>\n\n' % |
345 (pn, pn)) | 351 (pn, pn)) |
346 | 352 |
347 if os.supports_fd: | 353 if os.supports_fd: |
348 import stat | 354 import stat |
349 try: | 355 try: |
350 os.chmod(fp.fileno(), | 356 os.chmod(fp.fileno(), |
351 stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP) | 357 stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP) |
352 except OSError: | 358 except OSError: |
353 pass | 359 pass |
354 | 360 |
355 return source_name, content_item | 361 return source_name, content_item, do_publish |
356 | 362 |