Update route to handle error when env names are duplicated
This commit is contained in:
parent
812caf5d7d
commit
63a5d9274b
@ -1,4 +1,10 @@
|
|||||||
from flask import redirect, render_template, request as http_request, url_for, g
|
from flask import (
|
||||||
|
redirect,
|
||||||
|
render_template,
|
||||||
|
request as http_request,
|
||||||
|
url_for,
|
||||||
|
g,
|
||||||
|
)
|
||||||
|
|
||||||
from .blueprint import applications_bp
|
from .blueprint import applications_bp
|
||||||
from atst.domain.exceptions import AlreadyExistsError
|
from atst.domain.exceptions import AlreadyExistsError
|
||||||
@ -245,16 +251,36 @@ def handle_update_member(application_id, application_role_id, form_data):
|
|||||||
# TODO: flash error message
|
# TODO: flash error message
|
||||||
|
|
||||||
|
|
||||||
|
def handle_update_environment(form, application=None, environment=None):
|
||||||
|
if form.validate():
|
||||||
|
try:
|
||||||
|
if environment:
|
||||||
|
environment = Environments.update(
|
||||||
|
environment=environment, name=form.name.data
|
||||||
|
)
|
||||||
|
flash("application_environments_updated")
|
||||||
|
else:
|
||||||
|
environment = Environments.create(
|
||||||
|
g.current_user, application=application, name=form.name.data
|
||||||
|
)
|
||||||
|
flash("environment_added", environment_name=form.name.data)
|
||||||
|
|
||||||
|
return environment
|
||||||
|
|
||||||
|
except AlreadyExistsError:
|
||||||
|
flash("application_environments_name_error", name=form.name.data)
|
||||||
|
return False
|
||||||
|
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@applications_bp.route("/applications/<application_id>/settings")
|
@applications_bp.route("/applications/<application_id>/settings")
|
||||||
@user_can(Permissions.VIEW_APPLICATION, message="view application edit form")
|
@user_can(Permissions.VIEW_APPLICATION, message="view application edit form")
|
||||||
def settings(application_id):
|
def settings(application_id):
|
||||||
application = Applications.get(application_id)
|
application = Applications.get(application_id)
|
||||||
|
|
||||||
return render_settings_page(
|
return render_settings_page(application=application,)
|
||||||
application=application,
|
|
||||||
active_toggler=http_request.args.get("active_toggler"),
|
|
||||||
active_toggler_section=http_request.args.get("active_toggler_section"),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@applications_bp.route("/environments/<environment_id>/edit", methods=["POST"])
|
@applications_bp.route("/environments/<environment_id>/edit", methods=["POST"])
|
||||||
@ -264,31 +290,21 @@ def update_environment(environment_id):
|
|||||||
application = environment.application
|
application = environment.application
|
||||||
|
|
||||||
env_form = EditEnvironmentForm(obj=environment, formdata=http_request.form)
|
env_form = EditEnvironmentForm(obj=environment, formdata=http_request.form)
|
||||||
|
updated_environment = handle_update_environment(
|
||||||
|
form=env_form, application=application, environment=environment
|
||||||
|
)
|
||||||
|
|
||||||
if env_form.validate():
|
if updated_environment:
|
||||||
Environments.update(environment=environment, name=env_form.name.data)
|
|
||||||
|
|
||||||
flash("application_environments_updated")
|
|
||||||
|
|
||||||
return redirect(
|
return redirect(
|
||||||
url_for(
|
url_for(
|
||||||
"applications.settings",
|
"applications.settings",
|
||||||
application_id=application.id,
|
application_id=application.id,
|
||||||
fragment="application-environments",
|
fragment="application-environments",
|
||||||
_anchor="application-environments",
|
_anchor="application-environments",
|
||||||
active_toggler=environment.id,
|
|
||||||
active_toggler_section="edit",
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return (
|
return (render_settings_page(application=application, show_flash=True), 400)
|
||||||
render_settings_page(
|
|
||||||
application=application,
|
|
||||||
active_toggler=environment.id,
|
|
||||||
active_toggler_section="edit",
|
|
||||||
),
|
|
||||||
400,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@applications_bp.route(
|
@applications_bp.route(
|
||||||
@ -298,14 +314,9 @@ def update_environment(environment_id):
|
|||||||
def new_environment(application_id):
|
def new_environment(application_id):
|
||||||
application = Applications.get(application_id)
|
application = Applications.get(application_id)
|
||||||
env_form = EditEnvironmentForm(formdata=http_request.form)
|
env_form = EditEnvironmentForm(formdata=http_request.form)
|
||||||
|
environment = handle_update_environment(form=env_form, application=application)
|
||||||
|
|
||||||
if env_form.validate():
|
if environment:
|
||||||
Environments.create(
|
|
||||||
g.current_user, application=application, name=env_form.name.data
|
|
||||||
)
|
|
||||||
|
|
||||||
flash("environment_added", environment_name=env_form.data["name"])
|
|
||||||
|
|
||||||
return redirect(
|
return redirect(
|
||||||
url_for(
|
url_for(
|
||||||
"applications.settings",
|
"applications.settings",
|
||||||
@ -315,7 +326,7 @@ def new_environment(application_id):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return (render_settings_page(application=application), 400)
|
return (render_settings_page(application=application, show_flash=True), 400)
|
||||||
|
|
||||||
|
|
||||||
@applications_bp.route("/applications/<application_id>/edit", methods=["POST"])
|
@applications_bp.route("/applications/<application_id>/edit", methods=["POST"])
|
||||||
|
@ -29,6 +29,11 @@ MESSAGES = {
|
|||||||
""",
|
""",
|
||||||
"category": "success",
|
"category": "success",
|
||||||
},
|
},
|
||||||
|
"application_environments_name_error": {
|
||||||
|
"title_template": "",
|
||||||
|
"message_template": """{{ 'flash.application.env_name_error.message' | translate({ 'name': name }) }}""",
|
||||||
|
"category": "error",
|
||||||
|
},
|
||||||
"application_environments_updated": {
|
"application_environments_updated": {
|
||||||
"title_template": "Application environments updated",
|
"title_template": "Application environments updated",
|
||||||
"message_template": "Application environments have been updated",
|
"message_template": "Application environments have been updated",
|
||||||
|
@ -13,6 +13,9 @@
|
|||||||
|
|
||||||
{% block application_content %}
|
{% block application_content %}
|
||||||
|
|
||||||
|
{% if show_flash -%}
|
||||||
|
{% include "fragments/flash.html" %}
|
||||||
|
{%- endif %}
|
||||||
<h3>{{ 'portfolios.applications.settings.name_description' | translate }}</h3>
|
<h3>{{ 'portfolios.applications.settings.name_description' | translate }}</h3>
|
||||||
|
|
||||||
{% if user_can(permissions.EDIT_APPLICATION) %}
|
{% if user_can(permissions.EDIT_APPLICATION) %}
|
||||||
|
@ -52,8 +52,6 @@ def test_updating_application_environments_success(client, user_session):
|
|||||||
_external=True,
|
_external=True,
|
||||||
fragment="application-environments",
|
fragment="application-environments",
|
||||||
_anchor="application-environments",
|
_anchor="application-environments",
|
||||||
active_toggler=environment.id,
|
|
||||||
active_toggler_section="edit",
|
|
||||||
)
|
)
|
||||||
assert environment.name == "new name a"
|
assert environment.name == "new name a"
|
||||||
|
|
||||||
@ -78,6 +76,24 @@ def test_update_environment_failure(client, user_session):
|
|||||||
assert environment.name == "original name"
|
assert environment.name == "original name"
|
||||||
|
|
||||||
|
|
||||||
|
def test_enforces_unique_env_name(client, user_session, session):
|
||||||
|
application = ApplicationFactory.create()
|
||||||
|
user = application.portfolio.owner
|
||||||
|
name = "New Environment"
|
||||||
|
environment = EnvironmentFactory.create(application=application, name=name)
|
||||||
|
form_data = {"name": name}
|
||||||
|
user_session(user)
|
||||||
|
|
||||||
|
session.begin_nested()
|
||||||
|
response = client.post(
|
||||||
|
url_for("applications.new_environment", application_id=application.id),
|
||||||
|
data=form_data,
|
||||||
|
)
|
||||||
|
session.rollback()
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
def test_application_settings(client, user_session):
|
def test_application_settings(client, user_session):
|
||||||
portfolio = PortfolioFactory.create()
|
portfolio = PortfolioFactory.create()
|
||||||
application = Applications.create(
|
application = Applications.create(
|
||||||
|
@ -116,6 +116,8 @@ flash:
|
|||||||
deleted: 'You have successfully deleted the {application_name} application. To view the retained activity log, visit the portfolio administration page.'
|
deleted: 'You have successfully deleted the {application_name} application. To view the retained activity log, visit the portfolio administration page.'
|
||||||
name_error:
|
name_error:
|
||||||
message: 'The application name {name} has already been used in this portfolio. Please enter a unique name.'
|
message: 'The application name {name} has already been used in this portfolio. Please enter a unique name.'
|
||||||
|
env_name_error:
|
||||||
|
message: 'The environment name {name} has already been used in this application. Please enter a unique name.'
|
||||||
delete_member_success: 'You have successfully deleted {member_name} from the portfolio.'
|
delete_member_success: 'You have successfully deleted {member_name} from the portfolio.'
|
||||||
deleted_member: Portfolio member deleted
|
deleted_member: Portfolio member deleted
|
||||||
environment_added: 'The environment "{env_name}" has been added to the application.'
|
environment_added: 'The environment "{env_name}" has been added to the application.'
|
||||||
@ -199,7 +201,7 @@ forms:
|
|||||||
</ul>
|
</ul>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
defense_component:
|
defense_component:
|
||||||
label: "Select DoD component(s) funding your Portfolio:"
|
label: "Select DoD component(s) funding your Portfolio:"
|
||||||
choices:
|
choices:
|
||||||
air_force: Air Force
|
air_force: Air Force
|
||||||
@ -211,7 +213,7 @@ forms:
|
|||||||
help_text: |
|
help_text: |
|
||||||
<p>
|
<p>
|
||||||
Select the DOD component(s) that will fund all Applications within this Portfolio.
|
Select the DOD component(s) that will fund all Applications within this Portfolio.
|
||||||
In JEDI, multiple DoD organizations can fund the same Portfolio.<br/>
|
In JEDI, multiple DoD organizations can fund the same Portfolio.<br/>
|
||||||
Select all that apply.<br/>
|
Select all that apply.<br/>
|
||||||
</p>
|
</p>
|
||||||
attachment:
|
attachment:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user