def create_puzzle(directory, filename):
    puzzle = Puzzle(key='')
    try:
        full_file_path = os.path.join(directory, filename)
        image = open(full_file_path, 'rb')#Image.open(full_file_path)
        puzzle.image.save(filename, SimpleUploadedFile(filename, image.read(), content_type='image/jpg'), save=True)
        puzzle.save()       
    except Exception as ex:
        if puzzle.id:
            puzzle.delete()
        print ex
def create_puzzle_from_url(url):
    img_response = urllib2.urlopen(root_url + url)
    filename = img_response.geturl().split('/')[-1]
    f = SimpleUploadedFile(filename, img_response.read(), img_response.headers.gettype())
    puzzle = Puzzle(image=f)
    try:
        puzzle.save()
        yield _("Puzzle saved")
    except Exception as ex:
        puzzle.delete()
        yield str(ex)
def create_puzzle(directory, filename):
    puzzle = Puzzle(key="")
    try:
        full_file_path = os.path.join(directory, filename)
        image = open(full_file_path, "rb")
        puzzle = Puzzle(image=File(image))
        puzzle.save()

    except Exception as ex:
        if puzzle.id:
            puzzle.delete()
        print ex