# Buggy code
def edit(request, image_id):
    Image = get_image_model()
    ImageForm = get_image_form(Image)

    image = get_object_or_404(Image, id=image_id)

    if not image.is_editable_by_user(request.user):
        raise PermissionDenied

    if request.POST:
        original_file = image.file
        form = ImageForm(request.POST, request.FILES, instance=image)
        if form.is_valid():
            if "file" in form.changed_data:
                # if providing a new image file, delete the old one and all renditions.
                # NB Doing this via original_file.delete() clears the file field,
                # which definitely isn't what we want...
                original_file.storage.delete(original_file.name)
                image.renditions.all().delete()
            form.save()

            # Reindex the image to make sure all tags are indexed
            for backend in get_search_backends():
                backend.add(image)

            messages.success(
                request,
                _("Image '{0}' updated.").format(image.title),
                buttons=[
                    messages.button(
                        reverse("wagtailimages_edit_image", args=(image.id,)),
                        _("Edit again"),
                    )
                ],
            )
            return redirect("wagtailimages_index")
        else:
            messages.error(request, _("The image could not be saved due to errors."))
    else:
        form = ImageForm(instance=image)

    # Check if we should enable the frontend url generator
    try:
        reverse("wagtailimages_serve", args=("foo", "1", "bar"))
        url_generator_enabled = True
    except NoReverseMatch:
        url_generator_enabled = False

    return render(
        request,
        "wagtailimages/images/edit.html",
        {
            "image": image,
            "form": form,
            "url_generator_enabled": url_generator_enabled,
        },
    )


# Traceback (OSError)
Traceback (most recent call last):
File "/srv/django/site/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/django/site/env/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/srv/django/site/env/lib/python2.7/site-packages/wagtail/wagtailimages/views/images.py", line 126, in edit
'url_generator_enabled': url_generator_enabled,
File "/srv/django/site/env/lib/python2.7/site-packages/django/shortcuts.py", line 50, in render
return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/loader.py", line 178, in render_to_string
return t.render(context_instance)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 148, in render
return self._render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 142, in _render
return self.nodelist.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 126, in render
return compiled_parent._render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 142, in _render
return self.nodelist.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 126, in render
return compiled_parent._render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 142, in _render
return self.nodelist.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 126, in render
return compiled_parent._render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 142, in _render
return self.nodelist.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 65, in render
result = block.nodelist.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 65, in render
result = block.nodelist.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 844, in render
bit = self.render_node(node, context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
return node.render(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 898, in render
output = self.filter_expression.resolve(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 596, in resolve
obj = self.var.resolve(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 734, in resolve
value = self._resolve_lookup(context)
File "/srv/django/site/env/lib/python2.7/site-packages/django/template/base.py", line 770, in _resolve_lookup
current = getattr(current, bit)
File "/srv/django/site/env/lib/python2.7/site-packages/django/db/models/fields/files.py", line 74, in _get_size
return self.storage.size(self.name)
File "/srv/django/site/env/lib/python2.7/site-packages/django/core/files/storage.py", line 285, in size
return os.path.getsize(self.path(name))
File "/srv/django/site/env/lib/python2.7/genericpath.py", line 49, in getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: '/srv/django/site/media/original_images/img-17686908.jpg'

# Fixed code
def edit(request, image_id):
    Image = get_image_model()
    ImageForm = get_image_form(Image)

    image = get_object_or_404(Image, id=image_id)

    if not image.is_editable_by_user(request.user):
        raise PermissionDenied

    if request.POST:
        original_file = image.file
        form = ImageForm(request.POST, request.FILES, instance=image)
        if form.is_valid():
            if "file" in form.changed_data:
                # if providing a new image file, delete the old one and all renditions.
                # NB Doing this via original_file.delete() clears the file field,
                # which definitely isn't what we want...
                original_file.storage.delete(original_file.name)
                image.renditions.all().delete()
            form.save()

            # Reindex the image to make sure all tags are indexed
            for backend in get_search_backends():
                backend.add(image)

            messages.success(
                request,
                _("Image '{0}' updated.").format(image.title),
                buttons=[
                    messages.button(
                        reverse("wagtailimages_edit_image", args=(image.id,)),
                        _("Edit again"),
                    )
                ],
            )
            return redirect("wagtailimages_index")
        else:
            messages.error(request, _("The image could not be saved due to errors."))
    else:
        form = ImageForm(instance=image)

    # Check if we should enable the frontend url generator
    try:
        reverse("wagtailimages_serve", args=("foo", "1", "bar"))
        url_generator_enabled = True
    except NoReverseMatch:
        url_generator_enabled = False

    # Get file size
    try:
        filesize = image.file.size
    except OSError:
        # File doesn't exist
        filesize = None

    return render(
        request,
        "wagtailimages/images/edit.html",
        {
            "image": image,
            "form": form,
            "url_generator_enabled": url_generator_enabled,
            "filesize": filesize,
        },
    )