def initialize(tutorial_version_id, user_id, gh_token): """ Initialize project and environment for the tutorial, fork or create main repo for the user. - check if TutorialVersion and User exists - check venv - create venv - same for the repo """ tutorial_version = TutorialVersion.objects.get(pk=tutorial_version_id) user = User.objects.get(pk=user_id) gh_user = user.get_profile().gh_login #initialize virtualenv with cd(settings.REMOTE_ENV_PATH): venv_name = "T{0}".format(tutorial_version_id) if not exists(os.path.join(settings.REMOTE_ENV_PATH, venv_name)): run('virtualenv {0}'.format(venv_name)) venv(venv_name, "pip install {0}".format(tutorial_version.env['pip_line'])) #create repo on GitHub api_client = GitHubApi(token=gh_token) repo_to_create = get_repo_name(tutorial_version) api_client.create_repository(repo_to_create) api_client.add_collaborator(gh_user, repo_to_create, settings.GITHUB_COLLABORATOR_USER) #initialize repo repo_name = "U{0}TV{1}".format(user_id, tutorial_version_id) repo_path = os.path.join(settings.REMOTE_REPO_PATH, repo_name) if not exists(repo_path): run("mkdir {0}".format(repo_path)) with cd(repo_path): run("git init .") run("git remote add origin [email protected]:{0}/{1}.git".format(gh_user, repo_to_create)) #add remote run("git config user.email \"[email protected]\"") run("git config user.name \"trybox\"") #if everything worked, we add the tutorial to the user. user_tutorial = UserTutorialVersion(user=user, tutorial_version=tutorial_version, repository=repo_to_create) user_tutorial.save()
def authorized(request): code = request.GET.get('code', None) if not code: return HttpResponseRedirect(reverse('home:index')) token = GitHubApi.fetch_token(request, code) if token: user = authenticate(gh_token=token) if user: login(request, user) if request.session.test_cookie_worked(): request.session.delete_test_cookie() return HttpResponseRedirect(request.session.get('next') or reverse('home:index')) return HttpResponseRedirect(reverse('home:index'))
def sign_in(request): request.session['next'] = request.GET.get('next', None) return HttpResponseRedirect(GitHubApi.get_authorize_url())