@login_required
def avatarcrop(request):
    avatar = get_object_or_404(Avatar, user=request.user, valid=False)
    if not request.method == "POST":
        form = AvatarCropForm()
    else:
        image = Image.open(ContentFile(avatar.image.read()))
        form = AvatarCropForm(image, request.POST)
        if form.is_valid():
            top = int(form.cleaned_data.get('top'))
            left = int(form.cleaned_data.get('left'))
            right = int(form.cleaned_data.get('right'))
            bottom = int(form.cleaned_data.get('bottom'))

            if not (top or left or right or bottom):
                (width, height) = image.size
                if width > height:
                    diff = (width-height) / 2
                    left = diff
                    right = width-diff
                    bottom = height
                else:
                    diff = (height-width) / 2
                    top = diff
                    right = width
                    bottom = height-diff

            box = [ left, top, right, bottom ]
            image = image.crop(box)
            if image.mode not in ('L', 'RGB'):
                image = image.convert('RGB')

            if hasattr(settings, "AWS_SECRET_ACCESS_KEY"):
                f = StringIO()
                image.save(f, "JPEG")
                f.seek(0)
                avatar.image.delete()
                avatar.image.save("%s.jpg" % request.user.username, ContentFile(f.read()))
            else:
                image.save(avatar.image.path)

            avatar.valid = True
            avatar.save()
            messages.add_message(request, messages.INFO, _("Your new avatar has been saved successfully."))

            signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form)
            return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar"))

    template = "userprofile/avatar/crop.html"
    data = { 'section': 'avatar', 'avatar': avatar, 'form': form, }
    signals.context_signal.send(sender=avatarcrop, request=request, context=data)
    return render_to_response(template, data, context_instance=RequestContext(request))
If a ``POST`` request made to the endpoint indicates a non-existent
dataset/ETHPy150Open tualatrix/django-profile/userprofile/views.py/avatarcrop--crossover with--dataset/ETHPy150Open ClusterHQ/flocker/flocker/control/test/test_httpapi.py/CreateDatasetTestsMixin.test_unknown_primary_node