def del_survey(request): body = json.loads(request.body) survey_obj = get_survey(body["survey_id"]) if survey_obj is None: return HttpResponseNotFound("No such survey") if survey_obj.author_id == request.user.id: survey_obj.delete() return JsonResponse({"data": survey_obj.id}) return HttpResponseForbidden("You cannot delete someone else's survey")
def view_survey(request, survey_id): survey_obj = get_survey(survey_id) if survey_obj is None: return HttpResponseNotFound("No such survey") question_answers = [] for survey_question in SurveyQuestion.objects.filter(survey_id=survey_id): question_answers.append( QuestionAnswer.objects.filter(question_id=survey_question.question_id) ) return render( request, "surveys/survey.html", {"question_answers": question_answers, "survey": survey_obj}, )
def test_successful_get_one_survey(client): create_user(login="******") client.login(username="******", password="******") create_survey_area("Anything") create_survey(name="Survey", author_id=1, area_id=1) response = client.get(get_survey_get_one_url(1)) assert response.status_code == 200 survey = parse_survey(get_survey(survey_id=1)) assert survey == { "id": 1, "name": "Survey", "author_id": 1, "area_id": 1, "type": "Formal", }
def test_successful_delete_survey(client): create_user(login="******") client.login(username="******", password="******") create_survey_area("TestSurveyArea") survey = create_survey(name="TestSurvey", author_id=1, area_id=1) response = client.post( get_survey_delete_url(), { # fmt off "survey_id": 1, }, # fmt on content_type="application/json", ) assert response.status_code == 200 survey_obj = get_survey(survey_id=survey.id) assert survey_obj is None
def test_create_survey(client): create_user(login="******") client.login(username="******", password="******") create_survey_area("Anything") response = client.post( get_survey_create_url(), { # fmt off "author_id": 1, "area_id": 1, "name": "I don't know", "type": "Formal", }, # fmt on content_type="application/json", ) assert response.status_code == 200 survey_obj = parse_survey(get_survey(survey_id=1)) assert survey_obj == { "id": 1, "author_id": 1, "area_id": 1, "name": "I don't know", "type": "Formal", }
def survey(request, survey_id): survey_obj = get_survey(survey_id) if survey_obj is None: return HttpResponseNotFound("No such survey") return JsonResponse({"data": parse_survey(survey_obj)})