Merge pull request #206 from dod-ccpo/multiple-environments

Allow a user to add multiple environments when creating a project
This commit is contained in:
richard-dds
2018-08-23 16:15:00 -04:00
committed by GitHub
10 changed files with 89 additions and 21 deletions

View File

@@ -10,3 +10,9 @@ class Environments(object):
db.session.commit()
return environment
@classmethod
def create_many(cls, project, names):
for name in names:
environment = Environment(project=project, name=name)
db.session.add(environment)
db.session.commit()

View File

@@ -1,11 +1,13 @@
from atst.database import db
from atst.models.project import Project
from atst.domain.environments import Environments
class Projects(object):
@classmethod
def create(cls, workspace, name, description):
def create(cls, workspace, name, description, environment_names):
project = Project(workspace=workspace, name=name, description=description)
Environments.create_many(project, environment_names)
db.session.add(project)
db.session.commit()

View File

@@ -1,9 +1,26 @@
from flask_wtf import Form
from wtforms.fields import StringField, TextAreaField
from wtforms.fields import StringField, TextAreaField, FieldList
from wtforms.validators import Required
from atst.forms.validators import ListItemRequired
class NewProjectForm(Form):
name = StringField(label="Project Name")
description = TextAreaField(label="Description")
environment_name = StringField(label="Environment Name")
EMPTY_ENVIRONMENT_NAMES = ["", None]
name = StringField(label="Project Name", validators=[Required()])
description = TextAreaField(label="Description", validators=[Required()])
environment_names = FieldList(
StringField(label="Environment Name"),
validators=[ListItemRequired(message="Provide at least one environment name.")],
)
@property
def data(self):
_data = super(Form, self).data
_data["environment_names"] = [
n
for n in _data["environment_names"]
if n not in self.EMPTY_ENVIRONMENT_NAMES
]
return _data

View File

@@ -51,3 +51,14 @@ def Alphabet(message="Please enter only letters."):
raise ValidationError(message)
return _alphabet
def ListItemRequired(message="Please provide at least one.", empty_values=("", None)):
def _list_item_required(form, field):
non_empty_values = [v for v in field.data if v not in empty_values]
if len(non_empty_values) == 0:
raise ValidationError(message)
return _list_item_required

View File

@@ -9,7 +9,6 @@ from flask import (
from atst.domain.workspaces import Workspaces
from atst.domain.projects import Projects
from atst.domain.environments import Environments
from atst.forms.new_project import NewProjectForm
bp = Blueprint("workspaces", __name__)
@@ -67,10 +66,16 @@ def update_project(workspace_id):
if form.validate():
project_data = form.data
project = Projects.create(
workspace, project_data["name"], project_data["description"]
Projects.create(
workspace,
project_data["name"],
project_data["description"],
project_data["environment_names"],
)
Environments.create(project, project_data["environment_name"])
return redirect(
url_for("workspaces.workspace_projects", workspace_id=workspace.id)
)
else:
return render_template(
"workspace_project_new.html", workspace=workspace, form=form
)