Exemplo n.º 1
0
def new_project():
    form = ProjectForm(request.form)
    if request.method == 'POST':
        form.repositories.choices = get_repo_list(g.user)
        if form.validate():
            project = Projects(name=form.name.data, author_id=g.user.id)
            db_session.add(project)
            db_session.commit()
            project.collaborators.append(g.user)
            repo_name_list = []
            for repo in form.repositories.data:
                repo_id, repo_name = repo.split('*')
                repo_name_list.append(repo_name)
                repository = Repositories(
                    name=repo_name, github_repo_id=repo_id, 
                    project_id=project.id)
                db_session.add(repository)
            db_session.commit()
            return redirect(url_for('add_labels', project_id=project.id))

    # this is for GET request
    form.repositories.choices = get_repo_list(g.user)
    return render_template(
        "new_project.html", form=form, view_url=url_for('new_project'))
Exemplo n.º 2
0
def edit_project(project_id):
    project = Projects.query.get(int(project_id))
    if request.method == 'GET':
        form = ProjectForm(obj=project)
        form.repositories.choices = get_repo_list(g.user)
        form.repositories.data = ['{0}*{1}'.format(repo.github_repo_id, repo.name)
                                  for repo in project.repositories]

        return render_template('new_project.html', form=form,
                               view_url=url_for('edit_project', project_id=project_id))
    else:
        # for POST method type
        form = ProjectForm(request.form)

        new_repo_id_name_dict = dict([repo.split('*') for repo in form.repositories.data])
        new_repos = new_repo_id_name_dict.values()

        project_repos = [repo.name for repo in project.repositories]

        obsolete_repos = set(project_repos) - set(new_repos)
        repos_to_be_added = set(new_repos) - set(project_repos)

        # remove obsolete repos
        for repo_name in obsolete_repos:
            repo_objects = Repositories.query.filter_by(name=repo_name, project_id=project.id)
            for repo in repo_objects:
                db_session.delete(repo)
        db_session.commit()

        # create new repos
        for repo_name in repos_to_be_added:
            repo_id = [id for (id, name) in new_repo_id_name_dict.items() if name == repo_name]
            repo_object = Repositories(name=repo_name, github_repo_id=repo_id[0], project_id=project.id)
            db_session.add(repo_object)
        db_session.commit()
        return redirect(url_for('add_labels', project_id=project_id))