示例#1
0
文件: tests.py 项目: cuulee/analytics
def populate_db():
    u, _ = get_user_model().objects.get_or_create(username='******', is_superuser=True, first_name='admin')

    test_analyses = {}

    # Create 5 analyses
    for a in _analyses_json(5):
        analysis = Analysis(owner=u, **a)
        analysis.save()
        test_analyses[analysis.title] = analysis.id

    return test_analyses
示例#2
0
文件: views.py 项目: cuulee/analytics
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)