

@permission_checker.require('change')
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 permission_policy.user_has_permission_for_instance(request.user, 'change', image)):
        return permission_denied(request)
    if (request.method == 'POST'):
        original_file = image.file
        form = ImageForm(request.POST, request.FILES, instance=image, user=request.user)
        if form.is_valid():
            if ('file' in form.changed_data):
                original_file.storage.delete(original_file.name)
                image.renditions.all().delete()
                image.file_size = image.file.size
            form.save()
            for backend in get_search_backends():
                backend.add(image)
            messages.success(request, _("Image '{0}' updated.").format(image.title), buttons=[messages.button(reverse('wagtailimages:edit', 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, user=request.user)
    try:
        reverse('wagtailimages_serve', args=('foo', '1', 'bar'))
        url_generator_enabled = True
    except NoReverseMatch:
        url_generator_enabled = False
    if image.is_stored_locally():
        if (not os.path.isfile(image.file.path)):
            messages.error(request, _('The source image file could not be found. Please change the source or delete the image.').format(image.title), buttons=[messages.button(reverse('wagtailimages:delete', args=(image.id,)), _('Delete'))])
    return render(Image, 'wagtailimages/images/edit.html', {
        'image': image,
        'form': form,
        'url_generator_enabled': url_generator_enabled,
        'filesize': image.get_file_size(),
        'user_can_delete': permission_policy.user_has_permission_for_instance(request.user, 'delete', image),
    })

dataset/ETHPy150Open torchbox/wagtail/wagtail/wagtailimages/views/images.py edit/VarMisuse@393/552 `request`->`Image`