Ejemplo n.º 1
0
    def post(self, request, repository_name):
        access_token = take_access_token_from_session(request)
        if access_token:
            user = request.session['user']

            assert isinstance(user, GithubUser)

            resp_data = GithubAPI.get_user_public_repository(user.login, repository_name)
            github_repository = GithubRepositoryBuilder.build(resp_data, user)
            github_repository.save()

            # try to fetch repository by session_key and check if exists on file_system
            # before initializing the new one

            # also think of uniqueness of user_session

            session_key = request.session.session_key
            query_set = LocalRepository.objects.filter(user_session_key=session_key,
                                                       github_repository_id=github_repository.id) \
                .order_by('-last_modified')

            if query_set.exists():
                local_repo = query_set.first()
                if FileBrowser.repo_exists(local_repo.path):
                    return Response(data={'id': local_repo.id})

            local_repository = LocalRepositoryService.init_repository(session_key, github_repository)
            local_repository.save()

            return Response(data={'id': local_repository.id})
        else:
            return HttpResponseForbidden()
Ejemplo n.º 2
0
    def test_cut_path_to_base_dir(self):
        base_dir = 'base_dir'
        path = '/opt/vagrant/base_dir/project/file.txt'
        expected_path = 'base_dir/project/file.txt'

        actual_path = FileBrowser._cut_path_to_base_dir(base_dir, path)

        self.assertEqual(actual_path, expected_path)
Ejemplo n.º 3
0
    def test_file_browser(self):
        file_browser = FileBrowser(self.test_folder)

        expected_structure = [
            {'label': 'directory_1', 'id': 'file_browser_test_folder/directory_1', 'children': [
                {'label': 'directory_1', 'id': 'file_browser_test_folder/directory_1/directory_1', 'children':[
                    {'label': 'file_1', 'id': 'file_browser_test_folder/directory_1/directory_1/file_1'}
                ]},
                {'label': 'file_1', 'id': 'file_browser_test_folder/directory_1/file_1'}
            ]},
            {'label': 'directory_2', 'id': 'file_browser_test_folder/directory_2', 'children':[]},
            {'label': 'file_1', 'id': 'file_browser_test_folder/file_1'},
            {'label': 'file_2', 'id': 'file_browser_test_folder/file_2'}
        ]

        actual_structure = file_browser.get_structure()

        self.assertEqual(actual_structure, expected_structure)