Esempio n. 1
0
def view(
    solution_id: int,
    file_id: Optional[int] = None,
    shared_url: str = '',
):
    solution = Solution.get_or_none(Solution.id == solution_id)
    if solution is None:
        return fail(404, 'Solution does not exist.')

    viewer_is_solver = solution.solver.id == current_user.id
    has_viewer_access = current_user.role.is_viewer
    if not shared_url and not viewer_is_solver and not has_viewer_access:
        return fail(403, 'This user has no permissions to view this page.')

    versions = solution.ordered_versions()
    test_results = solution.test_results()
    is_manager = current_user.role.is_manager

    solution_files = tuple(solution.files)
    if not solution_files:
        if not is_manager:
            return fail(404, 'There are no files in this solution.')
        return done_checking(solution.exercise.id, solution.id)

    files = solutions.get_files_tree(solution.files)
    file_id = file_id or files[0]['id']
    file_to_show = next((f for f in solution_files if f.id == file_id), None)
    if file_to_show is None:
        return fail(404, 'File does not exist.')

    view_params = {
        'solution': model_to_dict(solution),
        'files': files,
        'comments': solution.comments_per_file,
        'current_file': file_to_show,
        'is_manager': is_manager,
        'role': current_user.role.name.lower(),
        'versions': versions,
        'test_results': test_results,
        'shared_url': shared_url,
    }

    if is_manager:
        view_params = {
            **view_params,
            'exercise_common_comments':
            _common_comments(exercise_id=solution.exercise),
            'all_common_comments':
            _common_comments(),
            'user_comments':
            _common_comments(user_id=current_user.id),
            'left':
            Solution.left_in_exercise(solution.exercise),
        }

    if viewer_is_solver:
        notifications.read_related(solution_id, current_user.id)

    return render_template('view.html', **view_params)
Esempio n. 2
0
def get_view_parameters(
    solution: Solution,
    file_id: Optional[int],
    shared_url: str,
    is_manager: bool,
    solution_files: Tuple[SolutionFile, ...],
    viewer_is_solver: bool,
) -> Dict[str, Any]:
    versions = solution.ordered_versions()
    test_results = solution.test_results()
    files = get_files_tree(solution.files)
    file_id = file_id or (files[0]['id'] if files else None)
    file_to_show = next((f for f in solution_files if f.id == file_id), None)
    if file_to_show is None:
        raise ResourceNotFound('File does not exist.', 404)

    view_params = {
        'solution': model_to_dict(solution),
        'files': files,
        'comments': solution.comments_per_file,
        'current_file': file_to_show,
        'is_manager': is_manager,
        'role': current_user.role.name.lower(),
        'versions': versions,
        'test_results': test_results,
        'shared_url': shared_url,
        'image_extensions': ALLOWED_IMAGES_EXTENSIONS,
    }

    if is_manager:
        view_params = {
            **view_params,
            'exercise_common_comments':
            comments._common_comments(exercise_id=solution.exercise),
            'all_common_comments':
            comments._common_comments(),
            'user_comments':
            comments._common_comments(user_id=current_user.id),
            'left':
            Solution.left_in_exercise(solution.exercise),
        }

    if viewer_is_solver:
        notifications.read_related(solution.id, current_user.id)

    return view_params
Esempio n. 3
0
 def test_read_related(
     self,
     student_user: User,
     solution: Solution,
     exercise: Exercise,
 ):
     solution2 = conftest.create_solution(student_user, exercise)
     student_user2 = conftest.create_user(index=1)
     messages = [
         conftest.create_notification(student_user, solution),
         conftest.create_notification(student_user, solution),
         conftest.create_notification(student_user, solution),
         conftest.create_notification(student_user, solution2),
         conftest.create_notification(student_user2, solution),
     ]
     assert all(not n.viewed for n in messages)
     notifications.read_related(solution.id, student_user)
     # peewee...
     messages = [Notification.get_by_id(n.id) for n in messages]
     assert all(n.viewed for n in messages[:3])
     assert all(not n.viewed for n in messages[3:])