@task(rate_limit='15/m')
@timeit
def generate_thumbnail(for_obj, from_field, to_field, max_size=settings.THUMBNAIL_SIZE):
 from_ = getattr(for_obj, from_field)
 to_ = getattr(for_obj, to_field)
 if (not (from_ and os.path.isfile(from_.path))):
  log_msg = 'No file to generate from: {model} {id}, {from_f} -> {to_f}'
  log.info(log_msg.format(model=for_obj.__class__.__name__, id=for_obj.id, from_f=from_field, to_f=to_field))
  return
 log_msg = 'Generating thumbnail for {model} {id}: {from_f} -> {to_f}'
 log.info(log_msg.format(model=for_obj.__class__.__name__, id=for_obj.id, from_f=from_field, to_f=to_field))
 thumb_content = _create_image_thumbnail(from_.path, longest_side=max_size)
 file_path = from_.path
 if to_:
  to_.delete(save=False)
 to_.save(file_path, thumb_content, save=False)
 for_obj.update(**{to_field: to_.name})

@ task ( rate_limit = '15/m' ) @ timeit def generate_thumbnail ( for_obj , from_field , to_field , max_size = settings . THUMBNAIL_SIZE ) : from_ = getattr ( for_obj , from_field ) to_ = getattr ( for_obj , to_field ) if ( not ( from_ and os . path . isfile ( from_ . path ) ) ) : log_msg = 'No file to generate from: {model} {id}, {from_f} -> {to_f}' log . info ( log_msg . format ( model = for_obj . __class__ . __name__ , id = for_obj . id , from_f = from_field , to_f = to_field ) ) return log_msg = 'Generating thumbnail for {model} {id}: {from_f} -> {to_f}' log . info ( log_msg . format ( model = for_obj . __class__ . __name__ , id = for_obj . id , from_f = from_field , to_f = to_field ) ) thumb_content = _create_image_thumbnail ( from_ . path , longest_side = max_size ) file_path = from_ . path if to_ : to_ . delete ( save = False ) to_ . save ( file_path , thumb_content , save = False ) for_obj . update ( ** { to_field : to_ . name } )
generate a thumbnail .