<commit_before>import json

from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import permission_required
from django.core.exceptions import PermissionDenied
from django.views.decorators.vary import vary_on_headers
from django.http import HttpResponseBadRequest

from wagtail.wagtailimages.models import get_image_model
from wagtail.wagtailimages.forms import get_image_form_for_multi


@permission_required('wagtailimages.add_image')
@vary_on_headers('X-Requested-With')
def add(request):
    Image = get_image_model()
    ImageForm = get_image_form_for_multi()

    if request.POST and request.is_ajax():
        if not request.FILES:
            return HttpResponseBadRequest("Must upload a file")
        else:
            image = Image(uploaded_by_user=request.user,  title=request.FILES['files[]'].name, file=request.FILES['files[]'])
            image.save()
            form = ImageForm(instance=image, prefix='image-%d'%image.id)

            return render(request, 'wagtailimages/multiple/edit_form.html', {
                'image': image,
                'form': form
            })

    return render(request, 'wagtailimages/multiple/add.html', {})


@permission_required('wagtailadmin.access_admin')  # more specific permission tests are applied within the view
def edit(request, image_id, callback=None):
    Image = get_image_model()
    ImageForm = get_image_form_for_multi()

    image = get_object_or_404(Image, id=image_id)

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

    if request.POST:
        form = ImageForm(request.POST, request.FILES, instance=image, prefix='image-'+image_id)
        if form.is_valid():
            form.save()
            return render(request, 'wagtailimages/multiple/confirmation.json', {
                'success': True,
                'image_id': image_id
            }, content_type='application/json')

    return render(request, 'wagtailimages/multiple/confirmation.json', {
        'success': False,
        'image_id': image_id<commit_msg>Added error message if user attempts to POST to multiple/add view without AJAX<commit_after>import json

from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import permission_required
from django.core.exceptions import PermissionDenied
from django.views.decorators.vary import vary_on_headers
from django.http import HttpResponseBadRequest

from wagtail.wagtailimages.models import get_image_model
from wagtail.wagtailimages.forms import get_image_form_for_multi


@permission_required('wagtailimages.add_image')
@vary_on_headers('X-Requested-With')
def add(request):
    Image = get_image_model()
    ImageForm = get_image_form_for_multi()

    if request.POST:
        if not request.is_ajax():
            return HttpResponseBadRequest("Cannot POST to this view without AJAX")

        if not request.FILES:
            return HttpResponseBadRequest("Must upload a file")

        image = Image(uploaded_by_user=request.user,  title=request.FILES['files[]'].name, file=request.FILES['files[]'])
        image.save()
        form = ImageForm(instance=image, prefix='image-%d'%image.id)

        return render(request, 'wagtailimages/multiple/edit_form.html', {
            'image': image,
            'form': form
        })

    return render(request, 'wagtailimages/multiple/add.html', {})


@permission_required('wagtailadmin.access_admin')  # more specific permission tests are applied within the view
def edit(request, image_id, callback=None):
    Image = get_image_model()
    ImageForm = get_image_form_for_multi()

    image = get_object_or_404(Image, id=image_id)

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

    if request.POST:
        form = ImageForm(request.POST, request.FILES, instance=image, prefix='image-'+image_id)
        if form.is_valid():
            form.save()
            return render(request, 'wagtailimages/multiple/confirmation.json', {
                'success': True,
                'image_id': image_id
            }, content_type='application/json')

    return render(request, 'wagtailimages/multiple/confirmation.json', {
        'success': False,
        'image_id': image_id
{'fraction_non_alphanumeric': 0.09995318352059925, 'fraction_numerical': 0.0028089887640449437, 'mean_word_length': 3.5360934182590236, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4319109', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1222, 'n_words': 261}