Example #1
0
def detail(request, solution_id):
    rating_check_request(request);
    solution = get_object_or_404(Solution.objects.select_related('content',
        'author', 'task', 'task.content'), id=solution_id)

    if not solution.status == SolutionStatus.SUBMITTED:
        return (404, u'Rješenje nije dostupno.')

    # You are not allowed to view solution (including your own) if the task is
    # not accessible to you. It would be a very rare case for someone to send a
    # solution and then later lose his accessibilty permission.
    if not solution.task.is_allowed_to_solve(request.user):
        return (403, u'Zadatak nije dostupan.')

    if solution.correctness_avg:
        ratings = solution.correctness.select_related('user')
    else:
        ratings = []

    # If I can view the solution, it means I can view the Task. Note that I
    # might not even have met the actual prerequisites, but it means that I do
    # have VIEW_SOLUTION permission or am the author of the task itself.
    # (look at the docs of is_allowed_to_solve in task/models.py)

    can_view, obfuscate = solution.check_accessibility(request.user)

    if not can_view:
        if solution.task.solution_settings == Task.SOLUTIONS_NOT_VISIBLE:
            return (403, u'Autor zadatka je onemogućio pristup rješenjima '  \
                u'drugih korisnika.')
        else: # Task.SOLUTIONS_VISIBLE_IF_ACCEPTED
            return (403, u'Rješenje dostupno samo korisnicima s točnim '
                u'vlastitim rješenjem.')

    return {
        'can_edit': solution.can_edit(request.user),
        'can_mark_as_official': \
                can_mark_as_official(request.user, solution.task),
        'can_view': can_view,
        'obfuscate': obfuscate,
        'ratings': ratings,
        'solution': solution,
    }
Example #2
0
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