Update route to catch error when app name uniqueness is violated and display a error message
This commit is contained in:
parent
22dd5d7b85
commit
ffbf612290
@ -2,6 +2,7 @@ from flask import redirect, render_template, request as http_request, url_for, g
|
||||
|
||||
from .blueprint import applications_bp
|
||||
from atst.domain.applications import Applications
|
||||
from atst.domain.exceptions import AlreadyExistsError
|
||||
from atst.domain.portfolios import Portfolios
|
||||
from atst.forms.application import NameAndDescriptionForm, EnvironmentsForm
|
||||
from atst.domain.authz.decorator import user_can_access_decorator as user_can
|
||||
@ -37,6 +38,31 @@ def render_new_application_form(
|
||||
return render_template(template, **render_args)
|
||||
|
||||
|
||||
def update_application(form, application_id=None, portfolio_id=None):
|
||||
if form.validate():
|
||||
application = None
|
||||
try:
|
||||
if application_id:
|
||||
application = Applications.get(application_id)
|
||||
application = Applications.update(application, form.data)
|
||||
flash("application_updated", application_name=application.name)
|
||||
else:
|
||||
portfolio = Portfolios.get_for_update(portfolio_id)
|
||||
application = Applications.create(
|
||||
g.current_user, portfolio, **form.data
|
||||
)
|
||||
flash("application_created", application_name=application.name)
|
||||
|
||||
return application
|
||||
|
||||
except AlreadyExistsError:
|
||||
flash("application_name_error", name=form.data["name"])
|
||||
return False
|
||||
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
@applications_bp.route("/portfolios/<portfolio_id>/applications/new")
|
||||
@applications_bp.route("/applications/<application_id>/new/step_1")
|
||||
@user_can(Permissions.CREATE_APPLICATION, message="view create new application form")
|
||||
@ -64,17 +90,9 @@ def create_or_update_new_application_step_1(portfolio_id=None, application_id=No
|
||||
form = get_new_application_form(
|
||||
{**http_request.form}, NameAndDescriptionForm, application_id
|
||||
)
|
||||
application = update_application(form, application_id, portfolio_id)
|
||||
|
||||
if form.validate():
|
||||
application = None
|
||||
if application_id:
|
||||
application = Applications.get(application_id)
|
||||
application = Applications.update(application, form.data)
|
||||
flash("application_updated", application_name=application.name)
|
||||
else:
|
||||
portfolio = Portfolios.get_for_update(portfolio_id)
|
||||
application = Applications.create(g.current_user, portfolio, **form.data)
|
||||
flash("application_created", application_name=application.name)
|
||||
if application:
|
||||
return redirect(
|
||||
url_for(
|
||||
"applications.update_new_application_step_2",
|
||||
|
@ -64,6 +64,11 @@ MESSAGES = {
|
||||
"message_template": "You have successfully updated the permissions for {{ user_name }}",
|
||||
"category": "success",
|
||||
},
|
||||
"application_name_error": {
|
||||
"title_template": "",
|
||||
"message_template": """{{ 'flash.application.name_error.message' | translate({ 'name': name }) }}""",
|
||||
"category": "error",
|
||||
},
|
||||
"ccpo_user_added": {
|
||||
"title_template": translate("flash.success"),
|
||||
"message_template": "You have successfully given {{ user_name }} CCPO permissions.",
|
||||
|
@ -70,6 +70,24 @@ def test_post_name_and_description_for_update(client, session, user_session):
|
||||
assert application.description == "This is only a test"
|
||||
|
||||
|
||||
def test_post_name_and_description_enforces_unique_name(client, user_session, session):
|
||||
portfolio = PortfolioFactory.create()
|
||||
name = "Test Application"
|
||||
application = ApplicationFactory.create(portfolio=portfolio, name=name)
|
||||
user_session(portfolio.owner)
|
||||
|
||||
session.begin_nested()
|
||||
response = client.post(
|
||||
url_for(
|
||||
"applications.create_new_application_step_1", portfolio_id=portfolio.id
|
||||
),
|
||||
data={"name": name, "description": "This is only a test"},
|
||||
)
|
||||
session.rollback()
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_get_environments(client, user_session):
|
||||
application = ApplicationFactory.create()
|
||||
user_session(application.portfolio.owner)
|
||||
|
@ -114,6 +114,8 @@ flash:
|
||||
message: '{application_name} has been successfully created. You may continue on to provision environments and assign team members now, or come back and complete these tasks at a later time.'
|
||||
updated: 'You have successfully updated the {application_name} application.'
|
||||
deleted: 'You have successfully deleted the {application_name} application. To view the retained activity log, visit the portfolio administration page.'
|
||||
name_error:
|
||||
message: 'The application name {name} has already been used in this portfolio. Please enter a unique name.'
|
||||
delete_member_success: 'You have successfully deleted {member_name} from the portfolio.'
|
||||
deleted_member: Portfolio member deleted
|
||||
environment_added: 'The environment "{env_name}" has been added to the application.'
|
||||
|
Loading…
x
Reference in New Issue
Block a user