mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 10:53:28 -05:00
Refactored register_errorhandlers so that it handles HTTPError
Remove most cases where we catch HTTPError
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
from flask import url_for, abort, current_app
|
||||
from flask import url_for, current_app
|
||||
from app import notifications_api_client
|
||||
from app.utils import BrowsableItem
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
|
||||
def insert_new_service(service_name, user_id):
|
||||
@@ -26,15 +25,7 @@ def get_service_by_id(id_):
|
||||
|
||||
|
||||
def get_service_by_id_or_404(id_):
|
||||
try:
|
||||
return notifications_api_client.get_service(id_)['data']
|
||||
except KeyError:
|
||||
abort(404)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
return notifications_api_client.get_service(id_)['data']
|
||||
|
||||
|
||||
def get_services(user_id=None):
|
||||
|
||||
@@ -22,10 +22,7 @@ def get_service_template_or_404(service_id, template_id):
|
||||
try:
|
||||
return notifications_api_client.get_service_template(service_id, template_id)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
abort(e.status_code)
|
||||
|
||||
|
||||
def delete_service_template(service_id, template_id):
|
||||
|
||||
@@ -16,29 +16,19 @@ from app import job_api_client
|
||||
@main.route("/services/<service_id>/dashboard")
|
||||
@login_required
|
||||
def service_dashboard(service_id):
|
||||
try:
|
||||
templates = templates_dao.get_service_templates(service_id)['data']
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
try:
|
||||
service = get_service_by_id(service_id)
|
||||
session['service_name'] = service['data']['name']
|
||||
session['service_id'] = service['data']['id']
|
||||
templates = templates_dao.get_service_templates(service_id)['data']
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
|
||||
service = get_service_by_id(service_id)
|
||||
session['service_name'] = service['data']['name']
|
||||
session['service_id'] = service['data']['id']
|
||||
|
||||
if session.get('invited_user'):
|
||||
session.pop('invited_user', None)
|
||||
service_name = service['data']['name']
|
||||
message = 'You have sucessfully accepted your invitation and been added to {}'.format(service_name)
|
||||
flash(message, 'default_with_tick')
|
||||
|
||||
if session.get('invited_user'):
|
||||
session.pop('invited_user', None)
|
||||
service_name = service['data']['name']
|
||||
message = 'You have sucessfully accepted your invitation and been added to {}'.format(service_name)
|
||||
flash(message, 'default_with_tick')
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
return render_template(
|
||||
'views/service_dashboard.html',
|
||||
jobs=list(reversed(jobs))[:5],
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from flask import (
|
||||
render_template,
|
||||
)
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
from app.main import main
|
||||
from app.main.forms import ForgotPasswordForm
|
||||
@@ -12,11 +11,8 @@ from app import user_api_client
|
||||
def forgot_password():
|
||||
form = ForgotPasswordForm()
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
user_api_client.send_reset_password_url(form.email_address.data)
|
||||
except HTTPError as e:
|
||||
if e.status_code != 404:
|
||||
raise e
|
||||
user_api_client.send_reset_password_url(form.email_address.data)
|
||||
|
||||
return render_template('views/password-reset-sent.html')
|
||||
|
||||
return render_template('views/forgot-password.html', form=form)
|
||||
|
||||
@@ -2,11 +2,9 @@ from flask import (
|
||||
redirect,
|
||||
url_for,
|
||||
session,
|
||||
abort,
|
||||
render_template
|
||||
)
|
||||
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
from app.main import main
|
||||
from app.main.dao.services_dao import get_service_by_id_or_404
|
||||
@@ -18,32 +16,23 @@ from app import (
|
||||
|
||||
@main.route("/invitation/<token>")
|
||||
def accept_invite(token):
|
||||
invited_user = invite_api_client.check_token(token)
|
||||
if invited_user.status == 'cancelled':
|
||||
from_user = user_api_client.get_user(invited_user.from_user)
|
||||
service = get_service_by_id_or_404(invited_user.service)
|
||||
return render_template('views/cancelled-invitation.html',
|
||||
from_user=from_user.name,
|
||||
service_name=service['name'])
|
||||
|
||||
try:
|
||||
existing_user = user_api_client.get_user_by_email(invited_user.email_address)
|
||||
session['invited_user'] = invited_user.serialize()
|
||||
|
||||
invited_user = invite_api_client.check_token(token)
|
||||
if invited_user.status == 'cancelled':
|
||||
from_user = user_api_client.get_user(invited_user.from_user)
|
||||
service = get_service_by_id_or_404(invited_user.service)
|
||||
return render_template('views/cancelled-invitation.html',
|
||||
from_user=from_user.name,
|
||||
service_name=service['name'])
|
||||
if existing_user:
|
||||
|
||||
existing_user = user_api_client.get_user_by_email(invited_user.email_address)
|
||||
session['invited_user'] = invited_user.serialize()
|
||||
|
||||
if existing_user:
|
||||
|
||||
user_api_client.add_user_to_service(invited_user.service,
|
||||
existing_user.id,
|
||||
invited_user.permissions)
|
||||
invite_api_client.accept_invite(invited_user.service, invited_user.id)
|
||||
return redirect(url_for('main.service_dashboard', service_id=invited_user.service))
|
||||
else:
|
||||
return redirect(url_for('main.register_from_invite'))
|
||||
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
user_api_client.add_user_to_service(invited_user.service,
|
||||
existing_user.id,
|
||||
invited_user.permissions)
|
||||
invite_api_client.accept_invite(invited_user.service, invited_user.id)
|
||||
return redirect(url_for('main.service_dashboard', service_id=invited_user.service))
|
||||
else:
|
||||
return redirect(url_for('main.register_from_invite'))
|
||||
|
||||
@@ -20,89 +20,71 @@ from app.main.dao import services_dao
|
||||
@main.route("/services/<service_id>/jobs")
|
||||
@login_required
|
||||
def view_jobs(service_id):
|
||||
try:
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
return render_template(
|
||||
'views/jobs/jobs.html',
|
||||
jobs=jobs,
|
||||
service_id=service_id
|
||||
)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
return render_template(
|
||||
'views/jobs/jobs.html',
|
||||
jobs=jobs,
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/jobs/<job_id>")
|
||||
@login_required
|
||||
def view_job(service_id, job_id):
|
||||
service = services_dao.get_service_by_id_or_404(service_id)
|
||||
try:
|
||||
job = job_api_client.get_job(service_id, job_id)['data']
|
||||
template = templates_dao.get_service_template_or_404(service_id, job['template'])['data']
|
||||
notifications = notification_api_client.get_notifications_for_service(service_id, job_id)
|
||||
finished = job['status'] == 'finished'
|
||||
return render_template(
|
||||
'views/jobs/job.html',
|
||||
notifications=notifications['notifications'],
|
||||
counts={
|
||||
'queued': 0 if finished else job['notification_count'],
|
||||
'sent': job['notification_count'] if finished else 0,
|
||||
'failed': 0,
|
||||
'cost': u'£0.00'
|
||||
},
|
||||
uploaded_at=job['created_at'],
|
||||
finished_at=job['updated_at'] if finished else None,
|
||||
uploaded_file_name=job['original_file_name'],
|
||||
template=Template(
|
||||
template,
|
||||
prefix=service['name'] if template['template_type'] == 'sms' else ''
|
||||
),
|
||||
service_id=service_id,
|
||||
service=service,
|
||||
job_id=job_id
|
||||
)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
job = job_api_client.get_job(service_id, job_id)['data']
|
||||
template = templates_dao.get_service_template_or_404(service_id, job['template'])['data']
|
||||
notifications = notification_api_client.get_notifications_for_service(service_id, job_id)
|
||||
finished = job['status'] == 'finished'
|
||||
return render_template(
|
||||
'views/jobs/job.html',
|
||||
notifications=notifications['notifications'],
|
||||
counts={
|
||||
'queued': 0 if finished else job['notification_count'],
|
||||
'sent': job['notification_count'] if finished else 0,
|
||||
'failed': 0,
|
||||
'cost': u'£0.00'
|
||||
},
|
||||
uploaded_at=job['created_at'],
|
||||
finished_at=job['updated_at'] if finished else None,
|
||||
uploaded_file_name=job['original_file_name'],
|
||||
template=Template(
|
||||
template,
|
||||
prefix=service['name'] if template['template_type'] == 'sms' else ''
|
||||
),
|
||||
service_id=service_id,
|
||||
service=service,
|
||||
job_id=job_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/jobs/<job_id>.json")
|
||||
@login_required
|
||||
def view_job_updates(service_id, job_id):
|
||||
service = services_dao.get_service_by_id_or_404(service_id)
|
||||
try:
|
||||
job = job_api_client.get_job(service_id, job_id)['data']
|
||||
notifications = notification_api_client.get_notifications_for_service(service_id, job_id)
|
||||
finished = job['status'] == 'finished'
|
||||
return jsonify(**{
|
||||
'counts': render_template(
|
||||
'partials/jobs/count.html',
|
||||
counts={
|
||||
'queued': 0 if finished else job['notification_count'],
|
||||
'sent': job['notification_count'] if finished else 0,
|
||||
'failed': 0,
|
||||
'cost': u'£0.00'
|
||||
}
|
||||
),
|
||||
'notifications': render_template(
|
||||
'partials/jobs/notifications.html',
|
||||
notifications=notifications['notifications']
|
||||
),
|
||||
'status': render_template(
|
||||
'partials/jobs/status.html',
|
||||
uploaded_at=job['created_at'],
|
||||
finished_at=job['updated_at'] if finished else None
|
||||
),
|
||||
})
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
job = job_api_client.get_job(service_id, job_id)['data']
|
||||
notifications = notification_api_client.get_notifications_for_service(service_id, job_id)
|
||||
finished = job['status'] == 'finished'
|
||||
return jsonify(**{
|
||||
'counts': render_template(
|
||||
'partials/jobs/count.html',
|
||||
counts={
|
||||
'queued': 0 if finished else job['notification_count'],
|
||||
'sent': job['notification_count'] if finished else 0,
|
||||
'failed': 0,
|
||||
'cost': u'£0.00'
|
||||
}
|
||||
),
|
||||
'notifications': render_template(
|
||||
'partials/jobs/notifications.html',
|
||||
notifications=notifications['notifications']
|
||||
),
|
||||
'status': render_template(
|
||||
'partials/jobs/status.html',
|
||||
uploaded_at=job['created_at'],
|
||||
finished_at=job['updated_at'] if finished else None
|
||||
),
|
||||
})
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/jobs/<job_id>/notification/<string:notification_id>")
|
||||
|
||||
@@ -11,9 +11,6 @@ from flask_login import (
|
||||
current_user
|
||||
)
|
||||
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from app import user_api_client
|
||||
|
||||
from app.main import main
|
||||
from app.main.forms import (
|
||||
InviteUserForm,
|
||||
|
||||
@@ -11,8 +11,6 @@ from flask import (
|
||||
|
||||
from flask.ext.login import current_user
|
||||
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
from app.main import main
|
||||
from app.main.dao import users_dao
|
||||
from app.main.forms import (
|
||||
@@ -56,17 +54,10 @@ def register_from_invite():
|
||||
|
||||
def _do_registration(form, service=None):
|
||||
if users_dao.is_email_unique(form.email_address.data):
|
||||
try:
|
||||
user = user_api_client.register_user(form.name.data,
|
||||
form.email_address.data,
|
||||
form.mobile_number.data,
|
||||
form.password.data)
|
||||
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
user = user_api_client.register_user(form.name.data,
|
||||
form.email_address.data,
|
||||
form.mobile_number.data,
|
||||
form.password.data)
|
||||
|
||||
# TODO possibly there should be some exception handling
|
||||
# for sending sms and email codes.
|
||||
|
||||
@@ -15,7 +15,6 @@ from flask import (
|
||||
)
|
||||
|
||||
from flask_login import login_required, current_user
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from utils.template import Template
|
||||
from utils.recipients import RecipientCSV, first_column_heading
|
||||
|
||||
@@ -80,13 +79,8 @@ def choose_template(service_id, template_type):
|
||||
|
||||
if template_type not in ['email', 'sms']:
|
||||
abort(404)
|
||||
try:
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
|
||||
return render_template(
|
||||
'views/choose-template.html',
|
||||
templates=[
|
||||
|
||||
@@ -13,8 +13,6 @@ from flask_login import (
|
||||
current_user
|
||||
)
|
||||
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
from app.main.dao.services_dao import (
|
||||
get_service_by_id,
|
||||
delete_service,
|
||||
@@ -31,13 +29,8 @@ from app.main.forms import ConfirmPasswordForm, ServiceNameForm
|
||||
@login_required
|
||||
@user_has_permissions('manage_settings')
|
||||
def service_settings(service_id):
|
||||
try:
|
||||
service = get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = get_service_by_id(service_id)['data']
|
||||
|
||||
return render_template(
|
||||
'views/service-settings.html',
|
||||
service=service,
|
||||
@@ -49,13 +42,7 @@ def service_settings(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('manage_settings')
|
||||
def service_name_change(service_id):
|
||||
try:
|
||||
service = get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = get_service_by_id(service_id)['data']
|
||||
|
||||
form = ServiceNameForm()
|
||||
|
||||
@@ -74,13 +61,7 @@ def service_name_change(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('manage_settings')
|
||||
def service_name_change_confirm(service_id):
|
||||
try:
|
||||
service = get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = get_service_by_id(service_id)['data']
|
||||
|
||||
# Validate password for form
|
||||
def _check_password(pwd):
|
||||
@@ -104,13 +85,7 @@ def service_name_change_confirm(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('manage_settings')
|
||||
def service_request_to_go_live(service_id):
|
||||
try:
|
||||
service = get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = get_service_by_id(service_id)['data']
|
||||
if request.method == 'GET':
|
||||
return render_template(
|
||||
'views/service-settings/request-to-go-live.html',
|
||||
@@ -127,13 +102,7 @@ def service_request_to_go_live(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('manage_settings')
|
||||
def service_status_change(service_id):
|
||||
try:
|
||||
service = get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = get_service_by_id(service_id)['data']
|
||||
|
||||
if request.method == 'GET':
|
||||
return render_template(
|
||||
@@ -149,13 +118,7 @@ def service_status_change(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('manage_settings')
|
||||
def service_status_change_confirm(service_id):
|
||||
try:
|
||||
service = get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = get_service_by_id(service_id)['data']
|
||||
|
||||
# Validate password for form
|
||||
def _check_password(pwd):
|
||||
@@ -178,13 +141,7 @@ def service_status_change_confirm(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('manage_settings')
|
||||
def service_delete(service_id):
|
||||
try:
|
||||
service = get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = get_service_by_id(service_id)['data']
|
||||
|
||||
if request.method == 'GET':
|
||||
return render_template(
|
||||
@@ -200,13 +157,7 @@ def service_delete(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('manage_settings')
|
||||
def service_delete_confirm(service_id):
|
||||
try:
|
||||
service = get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = get_service_by_id(service_id)['data']
|
||||
|
||||
# Validate password for form
|
||||
def _check_password(pwd):
|
||||
@@ -214,13 +165,7 @@ def service_delete_confirm(service_id):
|
||||
form = ConfirmPasswordForm(_check_password)
|
||||
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
service = delete_service(service_id)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
service = delete_service(service_id)
|
||||
return redirect(url_for('.choose_service'))
|
||||
|
||||
return render_template(
|
||||
|
||||
@@ -2,12 +2,9 @@ from flask import (
|
||||
render_template,
|
||||
redirect,
|
||||
session,
|
||||
url_for,
|
||||
abort
|
||||
url_for
|
||||
)
|
||||
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
from flask_login import login_user
|
||||
|
||||
from app.main import main
|
||||
@@ -31,11 +28,6 @@ def verify():
|
||||
activated_user = users_dao.activate_user(user)
|
||||
login_user(activated_user)
|
||||
return redirect(url_for('main.add_service', first='first'))
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
finally:
|
||||
del session['user_details']
|
||||
|
||||
|
||||
Reference in New Issue
Block a user