comparison piecrust/admin/views/micropub.py @ 1149:be74ba54a06f

admin: Improvements to micropub endpoint's photo handling. - Add `.jpg` extension to photos that don't have any extension. - Add photos in the config section instead of the body, so that the layout can put them in separate `u-photo` tags.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 10 Jul 2018 21:03:58 -0700
parents 41b7ce0d5131
children
comparison
equal deleted inserted replaced
1148:0aeb6f18d6a0 1149:be74ba54a06f
96 96
97 fn = secure_filename(photo.filename) 97 fn = secure_filename(photo.filename)
98 fn = re_unsafe_asset_char.sub('_', fn) 98 fn = re_unsafe_asset_char.sub('_', fn)
99 fn = '%s_%s' % (uuid.uuid1().hex, fn) 99 fn = '%s_%s' % (uuid.uuid1().hex, fn)
100 fn = fn.rstrip('_') 100 fn = fn.rstrip('_')
101 _, fnext = os.path.splitext(fn)
102 if not fnext:
103 fn = fn + '.jpg'
101 104
102 photo_cache_dir = os.path.join( 105 photo_cache_dir = os.path.join(
103 g.site.root_dir, 106 g.site.root_dir,
104 CACHE_DIR, g.site.piecrust_factory.cache_key, 107 CACHE_DIR, g.site.piecrust_factory.cache_key,
105 'uploads') 108 'uploads')
300 '%s_thumb.jpg' % p_fn_no_ext) 303 '%s_thumb.jpg' % p_fn_no_ext)
301 im.save(p_thumb_path) 304 im.save(p_thumb_path)
302 paths_to_commit.append(p_thumb_path) 305 paths_to_commit.append(p_thumb_path)
303 306
304 p_thumb_no_ext = '%s_thumb' % p_fn_no_ext 307 p_thumb_no_ext = '%s_thumb' % p_fn_no_ext
305 photo_names.append((p_thumb_no_ext, p_fn_no_ext)) 308 photo_names.append((p_fn_no_ext, p_thumb_no_ext))
306 else: 309 else:
307 photo_names.append((p_fn_no_ext, None)) 310 photo_names.append((p_fn_no_ext, None))
308 311
309 # There could also be some files uploaded along with the post 312 # There could also be some files uploaded along with the post
310 # so upload them right now. 313 # so upload them right now.
352 355
353 logger.debug("Writing to item: %s" % content_item.spec) 356 logger.debug("Writing to item: %s" % content_item.spec)
354 paths_to_commit.append(content_item.spec) 357 paths_to_commit.append(content_item.spec)
355 with source.openItem(content_item, mode='w', encoding='utf8') as fp: 358 with source.openItem(content_item, mode='w', encoding='utf8') as fp:
356 fp.write('---\n') 359 fp.write('---\n')
360
357 yaml.dump(post_config, fp, 361 yaml.dump(post_config, fp,
358 default_flow_style=False, 362 default_flow_style=False,
359 allow_unicode=True) 363 allow_unicode=True)
364
365 if photo_names:
366 fp.write('photos:\n')
367 for pfull, pthumb in photo_names:
368 fp.write('- name: %s\n' % pfull)
369 if pthumb:
370 fp.write(' thumb: %s\n' % pthumb)
371
360 fp.write('---\n') 372 fp.write('---\n')
361 373
362 if summary: 374 if summary:
363 fp.write(summary) 375 fp.write(summary)
364 fp.write('\n') 376 fp.write('\n')
365 fp.write('<!--break-->\n\n') 377 fp.write('<!--break-->\n\n')
366 fp.write(content) 378 fp.write(content)
367
368 if photo_names:
369 fp.write('\n\n')
370 for pthumb, pfull in photo_names:
371 if pfull:
372 fp.write('<a href="{{assets["%s"]}}">'
373 '<img src="{{assets["%s"]}}" alt="%s"/>'
374 '</a>\n\n' %
375 (pfull, pthumb, pthumb))
376 else:
377 fp.write('<img src="{{assets["%s"]}}" alt="%s"/>\n\n' %
378 (pthumb, pthumb))
379 379
380 if os.supports_fd: 380 if os.supports_fd:
381 import stat 381 import stat
382 try: 382 try:
383 os.chmod( 383 os.chmod(