from django.http import HttpResponse, JsonResponse
import json

# _make_result = lambda result: HttpResponse(json.dumps(result), mimetype='application/json')
_make_result = lambda result: JsonResponse(result)


def jsonifydata():
    def decorator(func):
        def wrapper(request, *args, **kwargs):
            result = func(request, *args, **kwargs)
            return HttpResponse(json.dumps(result), mimetype='application/json')
        return wrapper
    return decorator


def jsonify(validation_form=None):

    def decorator(func):

        def wrapper(request, *args, **kwargs):

            if not validation_form is None:
                form = validation_form(data=request.POST, files=request.FILES)

                if form.is_valid():
                    request.form_data = form.cleaned_data
                else:
                    return _make_result({'result': False, 'errors': form.errors})

            # return _make_result({'result': func(request, *args, **kwargs)})
            return _make_result(func(request, *args, **kwargs))

        return wrapper
    return decorator