Exemplo n.º 1
0
def edit(request, contest_id, genre_id, problem_id):
    result = check(request, contest_id, genre_id)
    if not isinstance(result, tuple):
        return result
    contest, genre = result

    problem = get_object_or_404(Problem, pk = problem_id)
    if request.method == 'POST':
        form = ProblemEditForm(request.POST, instance = problem)
        if form.is_valid():
            if request.POST['figure-sequence']:
                figures = Figure.objects.filter(problem = problem).order_by('sequence_number')
                sequence = json.loads(request.POST['figure-sequence'])
                for figure in figures:
                    figure.sequence_number = sequence[str(figure.id)]
                    figure.save()
            form.save()
            if 'preview' in request.POST and request.POST['preview'] == 'true':
                return redirect('scan.views.problems.preview', contest_id, genre_id, problem_id)
            else:
                return redirect('scan.views.problems.index', contest_id, genre_id)
    else:
        form = ProblemEditForm(instance = problem)
    figure_form = FigureAddForm()
    comment_form = CommentForm()

    comments = Comment.objects.filter(problem = problem).order_by('datetime')
    figures = Figure.objects.filter(problem = problem).order_by('sequence_number')
    context = {'subtitles': [contest.name, _(u'問題編集')], 'contest': contest, 'genre': genre, 'form': form, 'is_edit': True, 'problem': problem, 'figure_form': figure_form, 'figures':figures, 'comment_form': comment_form, 'comments': comments}
    return render(request, 'problems/edit.html', context)
Exemplo n.º 2
0
def add(request, contest_id, genre_id):
    result = check(request, contest_id, genre_id)
    if not isinstance(result, tuple):
        return result
    contest, genre = result

    if request.method == 'POST':
        form = ProblemEditForm(request.POST)
        if form.is_valid():
            problem = form.save(commit = False)
            problem.contest = contest
            problem.genre = genre
            problem.author = request.user
            problem.sequence_number = Problem.objects.filter(contest = contest, genre = genre).count() + 1
            problem.save()
            if 'preview' in request.POST and request.POST['preview'] == 'true':
                return redirect('scan.views.problems.preview', contest_id, genre_id, problem.id)
            else:
                return redirect('scan.views.problems.index', contest_id, genre_id)
    else:
        form = ProblemEditForm()

    context = {'subtitles': [contest.name, _(u'問題追加')], 'contest': contest, 'genre': genre, 'form': form}
    return render(request, 'problems/edit.html', context)