def create_associated_thumbnail(ccnode, ccfilemodel): """ Gets the appropriate thumbnail for export (uses or generates a base64 encoding) Args: ccnode (<ContentNode>): node to derive thumbnail from (if encoding is provided) ccfilemodel (<File>): file to get thumbnail from if no encoding is available Returns <File> model of encoded, resized thumbnail """ encoding = None try: encoding = ccnode.thumbnail_encoding and load_json_string( ccnode.thumbnail_encoding).get('base64') except ValueError: logging.error( "ERROR: node thumbnail is not in correct format ({}: {})".format( ccnode.id, ccnode.thumbnail_encoding)) return # Save the encoding if it doesn't already have an encoding if not encoding: try: encoding = get_thumbnail_encoding(str(ccfilemodel)) except IOError: # ImageMagick may raise an IOError if the file is not a thumbnail. Catch that then just return early. logging.error( "ERROR: cannot identify the thumbnail ({}: {})".format( ccnode.id, ccnode.thumbnail_encoding)) return ccnode.thumbnail_encoding = json.dumps({ "base64": encoding, "points": [], "zoom": 0, }) ccnode.save() return create_thumbnail_from_base64( encoding, uploaded_by=ccfilemodel.uploaded_by, file_format_id=ccfilemodel.file_format_id, preset_id=ccfilemodel.preset_id)
def create_associated_thumbnail(ccnode, ccfilemodel): """ Gets the appropriate thumbnail for export (uses or generates a base64 encoding) Args: ccnode (<ContentNode>): node to derive thumbnail from (if encoding is provided) ccfilemodel (<File>): file to get thumbnail from if no encoding is available Returns <File> model of encoded, resized thumbnail """ encoding = None try: encoding = ccnode.thumbnail_encoding and load_json_string(ccnode.thumbnail_encoding).get('base64') except ValueError: logging.error("ERROR: node thumbnail is not in correct format ({}: {})".format(ccnode.id, ccnode.thumbnail_encoding)) return # Save the encoding if it doesn't already have an encoding if not encoding: try: encoding = get_thumbnail_encoding(str(ccfilemodel)) except IOError: # ImageMagick may raise an IOError if the file is not a thumbnail. Catch that then just return early. logging.error("ERROR: cannot identify the thumbnail ({}: {})".format(ccnode.id, ccnode.thumbnail_encoding)) return ccnode.thumbnail_encoding = json.dumps({ "base64": encoding, "points": [], "zoom": 0, }) ccnode.save() return create_thumbnail_from_base64( encoding, uploaded_by=ccfilemodel.uploaded_by, file_format_id=ccfilemodel.file_format_id, preset_id=ccfilemodel.preset_id )
def test_jsons(json_tests): for val1, val2 in json_tests: assert load_json_string(val1) == val2, "JSONs don't match: {} != {}".format(val1, val2)
def test_jsons(json_tests): for val1, val2 in json_tests: assert load_json_string( val1) == val2, "JSONs don't match: {} != {}".format(val1, val2)