예제 #1
0
파일: views.py 프로젝트: ikicic/skoljka
def submit(request, task_id=None, solution_id=None):
    if solution_id:
        solution = get_object_or_404(Solution, pk=solution_id)
        if not solution.can_edit(request.user):
            return (403, u'Not allowed to edit the solution!')
        task = solution.task
        edit = True
    elif task_id:
        task = get_object_or_404(Task, pk=task_id)
        edit = False
    else:
        return 404

    if not task.solvable:
        return (403, u'This task is not solvable!')

    if not task.is_allowed_to_solve(request.user):
        return (403, u'You are not allowed to send solutions for this task.')

    if not edit:
        solution, dummy = Solution.objects.get_or_create(
                task=task, author=request.user)

    math_content = solution.content

    if request.method == 'POST':
        math_content_form = MathContentForm(request.POST, instance=math_content)
        if math_content_form.is_valid():
            math_content = math_content_form.save()

            was_solved = solution.is_solved()

            solution.content = math_content
            solution.status = SolutionStatus.SUBMITTED
            solution.date_created = datetime.now()
            solution.save()
            if not edit:
                _action.replace_or_add(request.user, _action.SOLUTION_SUBMIT,
                    action_object=solution, target=task)

            # update solved count if necessary
            delta = solution.is_solved() - was_solved
            if delta:
                _update_solved_count(delta, task, request.user.get_profile())

            return ("/solution/%d/" % (solution.id,),)
    else:
        math_content_form = MathContentForm(instance=math_content)

    math_content_form.fields['text'].widget.attrs['placeholder'] = 'Rješenje'
    return {
        'action_url': request.path,
        'form': math_content_form,
        'task': task,
    }
예제 #2
0
파일: views.py 프로젝트: ikicic/skoljka
def _do_mark(request, solution, task):
    """
        Update solution status:
            As Solved
            To Do
            Blank

        Or mark / unmark official flag

        Creates Solution if it doesn't exist (in that case Task is given).

        Returns None if no error.
    """

    action = request.POST['action']

    # check requset and privileges
    if action not in ['official0', 'official1', 'blank', 'as_solved', 'todo']:
        return (403, u'Action "%s" not valid.' % action)

    if action in ['official0', 'official1'] and \
            not can_mark_as_official(request.user, task):
        return (403, u'No permission to mark as official solution.')

    if not task.solvable:
        return (403, u'This task is not solvable!')

    if not task.is_allowed_to_solve(request.user):
        return (403, u'Not allowed to view the task or send solutions.')

    # as_solved, todo, blank
    if solution is None:
        solution, dummy = Solution.objects.get_or_create(
                task=task, author=request.user)
    elif not solution.can_edit(request.user):
        return (403, 'Not allowed to modify this solution.')

    # keep track of the number of solutions for the task
    was_solved = solution.is_solved()

    # update
    if action in ['official0', 'official1']:
        solution.is_official = action == 'official1'
    elif action in ['blank', 'as_solved', 'todo']:
        if action != 'blank':
            solution.date_created = datetime.now()
        solution.status = SOLUTION_STATUS_BY_NAME[action]

    solution.save()


    # log the action
    # TODO: use signals!
    if action in ['official1', 'as_solved', 'todo']:
        type_desc = {'official1': _action.SOLUTION_AS_OFFICIAL,
                     'as_solved': _action.SOLUTION_AS_SOLVED,
                     'todo': _action.SOLUTION_TODO,
            }
        _action.replace_or_add(request.user, type_desc[action],
            action_object=solution, target=task)
    elif action == 'official0':
        # temporary solution...
        _action.remove(request.user, type=_action.SOLUTION_AS_OFFICIAL[0],
            action_object=solution, target=task)
    elif action == 'blank':
        _action.remove(request.user, type=_action.SOLUTION_SEND,
            action_object=solution, target=task)


    # update solved count if necessary
    # TODO: use signals!
    delta = solution.is_solved() - was_solved
    if delta:
        _update_solved_count(delta, task, request.user.get_profile())

    return None     # ok