Beispiel #1
0
    def test_all_details_submitted(self):
        form_instance = PostQuestionForm(
            data={
                "title": "Sample Title",
                "category": self.cat,
                "short_description": "Sample Short Description",
                "description": "Sample Description",
                "time_limit": 2,
                "unique_code": "samques001",
                "constraints": "Sample constraints",
                "input_format": "sample inp format",
                "output_format": "sample_output_format",
                "sample_input": "Sample Input",
                "sample_output": "Sample Output"
            })

        self.assertEqual(form_instance.is_valid(), True)

        question = form_instance.save(commit=False)
        question.author = self.user
        question.save()

        model_instance = Question.objects.get(unique_code="samques001")

        self.assertEqual(model_instance.title, "Sample Title")
        self.assertEqual(model_instance.short_description,
                         "Sample Short Description")
        self.assertEqual(model_instance.description, "Sample Description")
        self.assertEqual(model_instance.time_limit, 2.0)
        self.assertEqual(model_instance.constraints, "Sample constraints")
        self.assertEqual(model_instance.input_format, "sample inp format")
        self.assertEqual(model_instance.output_format, "sample_output_format")
        self.assertEqual(model_instance.sample_input, "Sample Input")
        self.assertEqual(model_instance.sample_output, "Sample Output")
Beispiel #2
0
def board(request, username):
    # TODO: refactor into a class based view
    try:
        profile = Profile.objects.get(user__username=username)
    except Profile.DoesNotExist:
        messages.warning(request, f"No user found with username: {username}")
        return HttpResponse(f"No user found with username: {username}")
    if request.method == 'POST' and request.user.is_authenticated:
        form = PostQuestionForm(request.POST)
        if form.is_valid():
            q = Question(asked_by=request.user if
                         not form.cleaned_data.get('is_anonymous') else None,
                         asked_to=profile.user,
                         content=form.cleaned_data.get('question_text'))
            q.save()
            messages.success(request, 'Question posted successfully!')
    elif request.method == 'POST' and not request.user.is_authenticated:
        messages.warning(request, 'Log in to post a question.')
        return redirect('/login/')
    form = PostQuestionForm()
    questions = profile.user.get_public_questions()
    paginator = Paginator(questions, 5)
    questions_page = paginator.get_page(request.GET.get('page'))
    return render(
        request, 'core/board.html', {
            'question_max_length': 512,
            'profile': profile,
            'questions_page': questions_page,
            'form': form
        })
Beispiel #3
0
 def test_question_text_more_than_max_length(self):
     form = PostQuestionForm(data={
         'question_text': 'A'*514,
         'is_anonymous': True
     })
     self.assertFalse(form.is_valid())
     self.assertEqual(form.clean(), {
         'is_anonymous': True
     })
Beispiel #4
0
 def test_valid_form(self):
     form = PostQuestionForm(data={
         'question_text': 'A cool question?',
         'is_anonymous': True
     })
     self.assertTrue(form.is_valid())
     self.assertEqual(form.clean(), {
         'question_text': 'A cool question?',
         'is_anonymous': True
     })
Beispiel #5
0
 def test_form_can_accept_unicode_characters(self):
     form = PostQuestionForm(data={
         'question_text': 'Въпрос?',
         'is_anonymous': False
     })
     self.assertTrue(form.is_valid())
     self.assertEqual(form.clean(), {
         'question_text': 'Въпрос?',
         'is_anonymous': False
     })
Beispiel #6
0
    def test_missing_sample_output(self):
        form_instance = PostQuestionForm(
            data={
                "title": "Sample Title",
                "short_description": "Sample Short Description",
                "description": "Sample Description",
                "time_limit": 2,
                "unique_code": "samques001",
                "constraints": "Sample constraints",
                "input_format": "sample inp format",
                "output_format": "sample_output_format",
                "sample_input": "Sample Input"
            })

        self.assertEqual(form_instance.is_valid(), False)
Beispiel #7
0
def contest_question_create(request, contest_unique_id=None):
    contest_id = get_object_or_404(Contest, unique_code=contest_unique_id)
    if (contest_id.author != request.user):
        return PermissionDenied("You can not post question in this contest!")
    if (request.method == "POST"):
        form = PostQuestionForm(request.POST)
        contest_question_points = ContestQuestionForm(request.POST)
        if form.is_valid() and contest_question_points.is_valid():
            question = form.save(commit=False)
            question.author = request.user
            question.active = False
            question.save()
            contest_question = contest_question_points.save(commit=False)
            contest_question.contest = contest_id
            contest_question.question = question
            contest_question.save()
            return redirect('questions:view_the_question',
                            question.unique_code)
    else:
        form = PostQuestionForm()
        contest_question_points = ContestQuestionForm()
    context = {
        'form': form,
        'cq': contest_question_points,
    }
    return render(request, 'contests/create_contest_question.html', context)
Beispiel #8
0
def contest_question_edit(request, contest_unique_id, question_unique_id):
    question_instance = get_object_or_404(Question,
                                          unique_code=question_unique_id)
    contest_instance = get_object_or_404(Contest,
                                         unique_code=contest_unique_id)
    instance = get_object_or_404(ContestQuestion,
                                 question=question_instance,
                                 contest=contest_instance)
    form = PostQuestionForm(request.POST or None,
                            instance=question_instance,
                            initial={"category": question_instance.category})
    contest_question_points = ContestQuestionForm(request.POST or None,
                                                  instance=instance)
    if form.is_valid() and contest_question_points.is_valid():
        points = contest_question_points.cleaned_data["points"]
        contest_question_form = form.save(commit=False)
        contest_question_form.save()
        contest_question_points.save()
        return redirect('questions:view_the_question',
                        question_instance.unique_code)
    context = {'form': form, 'cq': contest_question_points}
    return render(request, "contests/create_contest_question.html", context)