comparison piecrust/admin/views/micropub.py @ 1025:c8366fc15043

admin: Make tumbnails when posting photos, commit to SCM.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 25 Dec 2017 19:01:01 -0800
parents 60b431c57ea9
children 41b7ce0d5131
comparison
equal deleted inserted replaced
1024:60b431c57ea9 1025:c8366fc15043
240 content_item = source.createContent(metadata) 240 content_item = source.createContent(metadata)
241 if content_item is None: 241 if content_item is None:
242 logger.error("Can't create item for: %s" % metadata) 242 logger.error("Can't create item for: %s" % metadata)
243 abort(500) 243 abort(500)
244 244
245 paths_to_commit = []
246
245 # Get the media to attach to the post. 247 # Get the media to attach to the post.
246 photos = None 248 photos = None
247 if 'photo' in request.files: 249 if 'photo' in request.files:
248 photos = [request.files['photo']] 250 photos = [request.files['photo']]
249 elif 'photo[]' in request.files: 251 elif 'photo[]' in request.files:
271 photo_cache_dir = os.path.join( 273 photo_cache_dir = os.path.join(
272 g.site.root_dir, 274 g.site.root_dir,
273 CACHE_DIR, g.site.piecrust_factory.cache_key, 275 CACHE_DIR, g.site.piecrust_factory.cache_key,
274 'uploads') 276 'uploads')
275 277
278 p_thumb_size = pcapp.config.get('micropub/resize_photos', 800)
279
276 for p_url in photo_urls: 280 for p_url in photo_urls:
277 _, __, p_fn = p_url.rpartition('/') 281 _, __, p_fn = p_url.rpartition('/')
278 p_cache_path = os.path.join(photo_cache_dir, p_fn) 282 p_cache_path = os.path.join(photo_cache_dir, p_fn)
279 p_asset_path = os.path.join(photo_dir, p_fn) 283 p_asset_path = os.path.join(photo_dir, p_fn)
280 logger.info("Moving upload '%s' to '%s'." % 284 logger.info("Moving upload '%s' to '%s'." %
281 (p_cache_path, p_asset_path)) 285 (p_cache_path, p_asset_path))
282 try: 286 try:
283 os.rename(p_cache_path, p_asset_path) 287 os.rename(p_cache_path, p_asset_path)
288 paths_to_commit.append(p_asset_path)
284 except OSError: 289 except OSError:
285 logger.error("Can't move '%s' to '%s'." % 290 logger.error("Can't move '%s' to '%s'." %
286 (p_cache_path, p_asset_path)) 291 (p_cache_path, p_asset_path))
287 raise 292 raise
288 293
289 p_fn_no_ext, _ = os.path.splitext(p_fn) 294 p_fn_no_ext, _ = os.path.splitext(p_fn)
290 photo_names.append(p_fn_no_ext) 295 if p_thumb_size > 0:
296 from PIL import Image
297 im = Image.open(p_asset_path)
298 im.thumbnail((p_thumb_size, p_thumb_size))
299 p_thumb_path = os.path.join(photo_dir,
300 '%s_thumb.jpg' % p_fn_no_ext)
301 im.save(p_thumb_path)
302 paths_to_commit.append(p_thumb_path)
303
304 p_thumb_no_ext = '%s_thumb' % p_fn_no_ext
305 photo_names.append((p_thumb_no_ext, p_fn_no_ext))
306 else:
307 photo_names.append((p_fn_no_ext, None))
291 308
292 # There could also be some files uploaded along with the post 309 # There could also be some files uploaded along with the post
293 # so upload them right now. 310 # so upload them right now.
294 if photos: 311 if photos:
295 for photo in photos: 312 for photo in photos:
300 fn = secure_filename(photo.filename) 317 fn = secure_filename(photo.filename)
301 fn = re_unsafe_asset_char.sub('_', fn) 318 fn = re_unsafe_asset_char.sub('_', fn)
302 photo_path = os.path.join(photo_dir, fn) 319 photo_path = os.path.join(photo_dir, fn)
303 logger.info("Uploading file to: %s" % photo_path) 320 logger.info("Uploading file to: %s" % photo_path)
304 photo.save(photo_path) 321 photo.save(photo_path)
322 paths_to_commit.append(photo_path)
323
324 # TODO: generate thumbnail.
305 325
306 fn_no_ext, _ = os.path.splitext(fn) 326 fn_no_ext, _ = os.path.splitext(fn)
307 photo_names.append(fn_no_ext) 327 photo_names.append((fn_no_ext, None))
308 328
309 # Build the config. 329 # Build the config.
310 do_publish = True 330 do_publish = True
311 post_config = {} 331 post_config = {}
312 if name: 332 if name:
329 micro_config = pcapp.config.get('micropub/microblogging') 349 micro_config = pcapp.config.get('micropub/microblogging')
330 if micro_config: 350 if micro_config:
331 merge_dicts(post_config, micro_config) 351 merge_dicts(post_config, micro_config)
332 352
333 logger.debug("Writing to item: %s" % content_item.spec) 353 logger.debug("Writing to item: %s" % content_item.spec)
354 paths_to_commit.append(content_item.spec)
334 with source.openItem(content_item, mode='w', encoding='utf8') as fp: 355 with source.openItem(content_item, mode='w', encoding='utf8') as fp:
335 fp.write('---\n') 356 fp.write('---\n')
336 yaml.dump(post_config, fp, 357 yaml.dump(post_config, fp,
337 default_flow_style=False, 358 default_flow_style=False,
338 allow_unicode=True) 359 allow_unicode=True)
344 fp.write('<!--break-->\n\n') 365 fp.write('<!--break-->\n\n')
345 fp.write(content) 366 fp.write(content)
346 367
347 if photo_names: 368 if photo_names:
348 fp.write('\n\n') 369 fp.write('\n\n')
349 for pn in photo_names: 370 for pthumb, pfull in photo_names:
350 fp.write('<img src="{{assets["%s"]}}" alt="%s"/>\n\n' % 371 if pfull:
351 (pn, pn)) 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))
352 379
353 if os.supports_fd: 380 if os.supports_fd:
354 import stat 381 import stat
355 try: 382 try:
356 os.chmod(fp.fileno(), 383 os.chmod(fp.fileno(),
357 stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP) 384 stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP)
358 except OSError: 385 except OSError:
359 pass 386 pass
360 387
388 autocommit = pcapp.config.get('micropub/autocommit', False)
389 if autocommit:
390 scm = g.site.scm
391 if scm:
392 commit_msg = None
393 if isinstance(autocommit, dict):
394 commit_msg = autocommit.get('message')
395 if not commit_msg:
396 post_title = post_config.get('title')
397 if post_title:
398 commit_msg = "New post: %s" % post_title
399 else:
400 commit_msg = "New post"
401 logger.debug("Commit files: %s" % paths_to_commit)
402 scm.commit(paths_to_commit, commit_msg)
403
361 return source_name, content_item, do_publish 404 return source_name, content_item, do_publish
362 405