Example #1
0
def new_analysis_json(request):
    """ The view that saves a new analysis in the database. """
    if request.method == 'POST':
        if not request.user.is_authenticated():
            return HttpResponse(
                _PERMISSION_MSG_LOGIN,
                mimetype="text/plain",
                status=401
            )
        try:
            data = json.loads(request.body)
            analysis_obj = Analysis(owner=request.user, title=data['title'], abstract=data['abstract'], data=json.dumps(data['data']))
            analysis_obj.save()
            analysis_obj.set_default_permissions() # This needs to be after .save() so that the analysis has an id.
            return HttpResponse(analysis_obj.id, status=200, mimetype='text/plain')

        except (ValueError, KeyError):
            return HttpResponse('Invalid data.', status=400, mimetype='text/plain')

    else:
        return HttpResponse(status=405)