Use service model to look up service attributes

This is better than just keying into the JSON because it means you get
an exception straight away when looking up a key that doesn’t exist
(which via mocking you could ordinarily miss).
This commit is contained in:
Chris Hill-Scott
2018-07-20 08:42:01 +01:00
parent 036923c382
commit 1304561a70
14 changed files with 147 additions and 77 deletions

View File

@@ -98,7 +98,7 @@ def create_api_key(service_id):
(KEY_TYPE_TEST, 'Test pretends to send messages'),
]
disabled_options, option_hints = [], {}
if current_service['restricted']:
if current_service.trial_mode:
disabled_options = [KEY_TYPE_NORMAL]
option_hints[KEY_TYPE_NORMAL] = Markup(
'Not available because your service is in '
@@ -148,14 +148,14 @@ def revoke_api_key(service_id, key_id):
def get_apis():
callback_api = None
inbound_api = None
if current_service['service_callback_api']:
if current_service.service_callback_api:
callback_api = service_api_client.get_service_callback_api(
current_service['id'],
current_service.id,
current_service.get('service_callback_api')[0]
)
if current_service['inbound_api']:
if current_service.inbound_api:
inbound_api = service_api_client.get_service_inbound_api(
current_service['id'],
current_service.id,
current_service.get('inbound_api')[0]
)
@@ -172,7 +172,7 @@ def check_token_against_dummy_bearer(token):
@main.route("/services/<service_id>/api/callbacks", methods=['GET'])
@login_required
def api_callbacks(service_id):
if 'inbound_sms' not in current_service['permissions']:
if not current_service.has_permission('inbound_sms'):
return redirect(url_for('.delivery_status_callback', service_id=service_id))
delivery_status_callback, received_text_messages_callback = get_apis()
@@ -186,10 +186,10 @@ def api_callbacks(service_id):
def get_delivery_status_callback_details():
if current_service['service_callback_api']:
if current_service.service_callback_api:
return service_api_client.get_service_callback_api(
current_service['id'],
current_service.get('service_callback_api')[0]
current_service.id,
current_service.service_callback_api[0]
)
@@ -248,9 +248,9 @@ def delivery_status_callback(service_id):
def get_received_text_messages_callback():
if current_service['inbound_api']:
if current_service.inbound_api:
return service_api_client.get_service_inbound_api(
current_service['id'],
current_service.id,
current_service.get('inbound_api')[0]
)
@@ -258,7 +258,7 @@ def get_received_text_messages_callback():
@main.route("/services/<service_id>/api/callbacks/received-text-messages-callback", methods=['GET', 'POST'])
@login_required
def received_text_messages_callback(service_id):
if 'inbound_sms' not in current_service['permissions']:
if not current_service.has_permission('inbound_sms'):
return redirect(url_for('.api_integration', service_id=service_id))
received_text_messages_callback = get_received_text_messages_callback()

View File

@@ -238,7 +238,7 @@ def inbox_download(service_id):
def get_inbox_partials(service_id):
page = int(request.args.get('page', 1))
if 'inbound_sms' not in current_service['permissions']:
if not current_service.has_permission('inbound_sms'):
abort(403)
inbound_messages_data = service_api_client.get_most_recent_inbound_sms(service_id, page=page)

View File

@@ -104,8 +104,8 @@ def feedback(ticket_type):
user_name = form.name.data or None
if current_service:
service_string = 'Service: "{name}"\n{url}\n'.format(
name=current_service['name'],
url=url_for('main.service_dashboard', service_id=current_service['id'], _external=True)
name=current_service.name,
url=url_for('main.service_dashboard', service_id=current_service.id, _external=True)
)
else:
service_string = ''

View File

@@ -157,7 +157,7 @@ def view_job_updates(service_id, job_id):
return jsonify(**get_job_partials(
job,
service_api_client.get_service_template(
service_id=current_service['id'],
service_id=current_service.id,
template_id=job['template'],
version=job['template_version']
)['data'],
@@ -179,7 +179,7 @@ def view_notifications(service_id, message_type=None):
search_form=SearchNotificationsForm(to=request.form.get('to', '')),
download_link=url_for(
'.download_notifications_csv',
service_id=current_service['id'],
service_id=current_service.id,
message_type=message_type,
status=request.args.get('status')
)
@@ -244,7 +244,7 @@ def get_notifications(service_id, message_type, status_override=None):
if message_type:
download_link = url_for(
'.view_notifications_csv',
service_id=current_service['id'],
service_id=current_service.id,
message_type=message_type,
status=request.args.get('status')
)
@@ -382,7 +382,7 @@ def get_job_partials(job, template):
percentage_complete=(job['notifications_requested'] / job['notification_count'] * 100),
download_link=url_for(
'.view_job_csv',
service_id=current_service['id'],
service_id=current_service.id,
job_id=job['id'],
status=request.args.get('status')
),

View File

@@ -188,6 +188,6 @@ def download_notifications_csv(service_id):
'Content-Disposition': 'inline; filename="{} - {} - {} report.csv"'.format(
format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")),
filter_args['message_type'][0],
current_service['name'])
current_service.name)
}
)

View File

@@ -116,7 +116,7 @@ def send_messages(service_id, template_id):
session['sender_id'] = None
db_template = service_api_client.get_service_template(service_id, template_id)['data']
if email_or_sms_not_enabled(db_template['template_type'], current_service['permissions']):
if email_or_sms_not_enabled(db_template['template_type'], current_service.permissions):
return redirect(url_for(
'.action_blocked',
service_id=service_id,
@@ -294,7 +294,7 @@ def send_test(service_id, template_id):
if db_template['template_type'] == 'letter':
session['sender_id'] = None
if email_or_sms_not_enabled(db_template['template_type'], current_service['permissions']):
if email_or_sms_not_enabled(db_template['template_type'], current_service.permissions):
return redirect(url_for(
'.action_blocked',
service_id=service_id,
@@ -511,7 +511,7 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
users = user_api_client.get_users_for_service(service_id=service_id)
statistics = service_api_client.get_service_statistics(service_id, today_only=True)
remaining_messages = (current_service['message_limit'] - sum(stat['requested'] for stat in statistics.values()))
remaining_messages = (current_service.message_limit - sum(stat['requested'] for stat in statistics.values()))
contents = s3download(service_id, upload_id)
@@ -549,7 +549,7 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
max_errors_shown=50,
whitelist=itertools.chain.from_iterable(
[user.name, user.mobile_number, user.email_address] for user in users
) if current_service['restricted'] else None,
) if current_service.trial_mode else None,
remaining_messages=remaining_messages,
international_sms=current_service.has_permission('international_sms'),
)
@@ -585,7 +585,7 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
back_link=back_link,
help=get_help_argument(),
trying_to_send_letters_in_trial_mode=all((
current_service['restricted'],
current_service.trial_mode,
template.template_type == 'letter',
not request.args.get('from_test'),
)),
@@ -873,7 +873,7 @@ def send_notification(service_id, template_id):
)
except HTTPError as exception:
current_app.logger.info('Service {} could not send notification: "{}"'.format(
current_service['id'],
current_service.id,
exception.message
))
return _check_notification(service_id, template_id, exception)

View File

@@ -62,8 +62,8 @@ def service_settings(service_id):
letter_branding_organisations = email_branding_client.get_letter_email_branding()
organisation = organisations_client.get_service_organisation(service_id).get('name', None)
if current_service['email_branding']:
email_branding = email_branding_client.get_email_branding(current_service['email_branding'])['email_branding']
if current_service.email_branding:
email_branding = email_branding_client.get_email_branding(current_service.email_branding)['email_branding']
else:
email_branding = None
@@ -102,7 +102,7 @@ def service_settings(service_id):
default_sms_sender=default_sms_sender,
sms_sender_count=sms_sender_count,
free_sms_fragment_limit=free_sms_fragment_limit,
prefix_sms=current_service['prefix_sms'],
prefix_sms=current_service.prefix_sms,
organisation=organisation,
)
@@ -114,11 +114,11 @@ def service_name_change(service_id):
form = RenameServiceForm()
if request.method == 'GET':
form.name.data = current_service['name']
form.name.data = current_service.name
if form.validate_on_submit():
if form.name.data == current_service['name']:
if form.name.data == current_service.name:
return redirect(url_for('.service_settings', service_id=service_id))
unique_name = service_api_client.is_service_name_unique(service_id, form.name.data, email_safe(form.name.data))
@@ -149,7 +149,7 @@ def service_name_change_confirm(service_id):
if form.validate_on_submit():
try:
service_api_client.update_service(
current_service['id'],
current_service.id,
name=session['service_name_change'],
email_from=email_safe(session['service_name_change'])
)
@@ -201,7 +201,7 @@ def submit_request_to_go_live(service_id):
if form.validate_on_submit():
zendesk_client.create_ticket(
subject='Request to go live - {}'.format(current_service['name']),
subject='Request to go live - {}'.format(current_service.name),
message=(
'Service: {}\n'
'{}\n'
@@ -212,9 +212,9 @@ def submit_request_to_go_live(service_id):
'\nPeak volume: {}'
'\nFeatures: {}'
).format(
current_service['name'],
url_for('main.service_dashboard', service_id=current_service['id'], _external=True),
current_service['organisation_type'],
current_service.name,
url_for('main.service_dashboard', service_id=current_service.id, _external=True),
current_service.organisation_type,
AgreementInfo.from_current_user().as_human_readable,
formatted_list(filter(None, (
'email' if form.channel_email.data else None,
@@ -246,11 +246,11 @@ def submit_request_to_go_live(service_id):
@user_is_platform_admin
def service_switch_live(service_id):
service_api_client.update_service(
current_service['id'],
current_service.id,
# TODO This limit should be set depending on the agreement signed by
# with Notify.
message_limit=250000 if current_service['restricted'] else 50,
restricted=(not current_service['restricted'])
message_limit=250000 if current_service.trial_mode else 50,
restricted=(not current_service.trial_mode)
)
return redirect(url_for('.service_settings', service_id=service_id))
@@ -261,7 +261,7 @@ def service_switch_live(service_id):
def service_switch_research_mode(service_id):
service_api_client.update_service_with_properties(
service_id,
{"research_mode": not current_service['research_mode']}
{"research_mode": not current_service.research_mode}
)
return redirect(url_for('.service_settings', service_id=service_id))
@@ -271,14 +271,14 @@ def switch_service_permissions(service_id, permission, sms_sender=None):
force_service_permission(
service_id,
permission,
on=permission not in current_service['permissions'],
on=permission not in current_service.permissions,
sms_sender=sms_sender
)
def force_service_permission(service_id, permission, on=False, sms_sender=None):
permissions, permission = set(current_service['permissions']), {permission}
permissions, permission = set(current_service.permissions), {permission}
update_service_permissions(
service_id,
@@ -343,7 +343,7 @@ def service_switch_can_upload_document(service_id):
if form.validate_on_submit():
service_api_client.update_service(
current_service['id'],
current_service.id,
contact_link=form.url.data
)
switch_service_permissions(service_id, 'upload_document')
@@ -400,10 +400,10 @@ def service_set_contact_link(service_id):
if form.validate_on_submit():
service_api_client.update_service(
current_service['id'],
current_service.id,
contact_link=form.url.data
)
return redirect(url_for('.service_settings', service_id=current_service['id']))
return redirect(url_for('.service_settings', service_id=current_service.id))
return render_template('views/service-settings/contact_link.html', form=form)
@@ -443,7 +443,7 @@ def service_add_email_reply_to(service_id):
first_email_address = reply_to_email_address_count == 0
if form.validate_on_submit():
service_api_client.add_reply_to_email_address(
current_service['id'],
current_service.id,
email_address=form.email_address.data,
is_default=first_email_address if first_email_address else form.is_default.data
)
@@ -474,7 +474,7 @@ def service_edit_email_reply_to(service_id, reply_to_email_id):
form.is_default.data = reply_to_email_address['is_default']
if form.validate_on_submit():
service_api_client.update_reply_to_email_address(
current_service['id'],
current_service.id,
reply_to_email_id=reply_to_email_id,
email_address=form.email_address.data,
is_default=True if reply_to_email_address['is_default'] else form.is_default.data
@@ -493,7 +493,7 @@ def service_edit_email_reply_to(service_id, reply_to_email_id):
@user_has_permissions('manage_service')
def service_delete_email_reply_to(service_id, reply_to_email_id):
service_api_client.delete_reply_to_email_address(
service_id=current_service['id'],
service_id=current_service.id,
reply_to_email_id=reply_to_email_id,
)
return redirect(url_for('.service_email_reply_to', service_id=service_id))
@@ -514,12 +514,12 @@ def service_set_inbound_number(service_id):
)
if form.validate_on_submit():
service_api_client.add_sms_sender(
current_service['id'],
current_service.id,
sms_sender=form.inbound_number.data,
is_default=True,
inbound_number_id=form.inbound_number.data
)
switch_service_permissions(current_service['id'], 'inbound_sms')
switch_service_permissions(current_service.id, 'inbound_sms')
return redirect(url_for('.service_settings', service_id=service_id))
return render_template(
'views/service-settings/set-inbound-number.html',
@@ -544,14 +544,14 @@ def service_set_sms(service_id):
def service_set_sms_prefix(service_id):
form = SMSPrefixForm(enabled=(
'on' if current_service['prefix_sms'] else 'off'
'on' if current_service.prefix_sms else 'off'
))
form.enabled.label.text = 'Start all text messages with {}:'.format(current_service['name'])
form.enabled.label.text = 'Start all text messages with {}:'.format(current_service.name)
if form.validate_on_submit():
service_api_client.update_service(
current_service['id'],
current_service.id,
prefix_sms=(form.enabled.data == 'on')
)
return redirect(url_for('.service_settings', service_id=service_id))
@@ -682,7 +682,7 @@ def service_add_letter_contact(service_id):
first_contact_block = letter_contact_blocks_count == 0
if form.validate_on_submit():
service_api_client.add_letter_contact(
current_service['id'],
current_service.id,
contact_block=form.letter_contact_block.data.replace('\r', '') or None,
is_default=first_contact_block if first_contact_block else form.is_default.data
)
@@ -707,7 +707,7 @@ def service_edit_letter_contact(service_id, letter_contact_id):
form.is_default.data = letter_contact_block['is_default']
if form.validate_on_submit():
service_api_client.update_letter_contact(
current_service['id'],
current_service.id,
letter_contact_id=letter_contact_id,
contact_block=form.letter_contact_block.data.replace('\r', '') or None,
is_default=True if letter_contact_block['is_default'] else form.is_default.data
@@ -753,7 +753,7 @@ def service_add_sms_sender(service_id):
first_sms_sender = sms_sender_count == 0
if form.validate_on_submit():
service_api_client.add_sms_sender(
current_service['id'],
current_service.id,
sms_sender=form.sms_sender.data.replace('\r', '') or None,
is_default=first_sms_sender if first_sms_sender else form.is_default.data
)
@@ -786,7 +786,7 @@ def service_edit_sms_sender(service_id, sms_sender_id):
if form.validate_on_submit():
service_api_client.update_sms_sender(
current_service['id'],
current_service.id,
sms_sender_id=sms_sender_id,
sms_sender=sms_sender['sms_sender'] if is_inbound_number else form.sms_sender.data.replace('\r', ''),
is_default=True if sms_sender['is_default'] else form.is_default.data
@@ -812,7 +812,7 @@ def service_edit_sms_sender(service_id, sms_sender_id):
@user_has_permissions('manage_service')
def service_delete_sms_sender(service_id, sms_sender_id):
service_api_client.delete_sms_sender(
service_id=current_service['id'],
service_id=current_service.id,
sms_sender_id=sms_sender_id,
)
return redirect(url_for('.service_sms_senders', service_id=service_id))
@@ -823,13 +823,13 @@ def service_delete_sms_sender(service_id, sms_sender_id):
@user_has_permissions('manage_service')
def service_set_letter_contact_block(service_id):
if 'letter' not in current_service['permissions']:
if not current_service.has_permission('letter'):
abort(403)
form = ServiceLetterContactBlockForm(letter_contact_block=current_service['letter_contact_block'])
form = ServiceLetterContactBlockForm(letter_contact_block=current_service.letter_contact_block)
if form.validate_on_submit():
service_api_client.update_service(
current_service['id'],
current_service.id,
letter_contact_block=form.letter_contact_block.data.replace('\r', '') or None
)
if request.args.get('from_template'):
@@ -906,7 +906,7 @@ def service_set_email_branding(service_id):
)
return redirect(url_for('.service_settings', service_id=service_id))
form.branding_style.data = current_service['email_branding'] or 'None'
form.branding_style.data = current_service.email_branding or 'None'
return render_template(
'views/service-settings/set-email-branding.html',
@@ -971,12 +971,12 @@ def link_service_to_organisation(service_id):
def branding_request(service_id):
form = BrandingOptionsEmail(
options=current_service['branding']
options=current_service.branding
)
if form.validate_on_submit():
zendesk_client.create_ticket(
subject='Email branding request - {}'.format(current_service['name']),
subject='Email branding request - {}'.format(current_service.name),
message=(
'Organisation: {}\n'
'Service: {}\n'
@@ -985,8 +985,8 @@ def branding_request(service_id):
'\nBranding requested: {}'
).format(
AgreementInfo.from_current_user().as_info_for_branding_request,
current_service['name'],
url_for('main.service_dashboard', service_id=current_service['id'], _external=True),
current_service.name,
url_for('main.service_dashboard', service_id=current_service.id, _external=True),
branding_options_dict[form.options.data],
),
ticket_type=zendesk_client.TYPE_QUESTION,

View File

@@ -128,7 +128,7 @@ def choose_template(service_id, template_type='all'):
}) > 1
template_nav_items = [
(label, key, url_for('.choose_template', service_id=current_service['id'], template_type=key), '')
(label, key, url_for('.choose_template', service_id=current_service.id, template_type=key), '')
for label, key in filter(None, [
('All', 'all'),
('Text message', 'sms'),
@@ -237,7 +237,7 @@ def add_template_by_type(service_id):
template_id=blank_letter['data']['id'],
))
if email_or_sms_not_enabled(form.template_type.data, current_service['permissions']):
if email_or_sms_not_enabled(form.template_type.data, current_service.permissions):
return redirect(url_for(
'.action_blocked',
service_id=service_id,
@@ -325,7 +325,7 @@ def add_service_template(service_id, template_type):
if template_type not in ['sms', 'email', 'letter']:
abort(404)
if 'letter' not in current_service['permissions'] and template_type == 'letter':
if not current_service.has_permission('letter') and template_type == 'letter':
abort(403)
form = form_objects[template_type]()
@@ -355,7 +355,7 @@ def add_service_template(service_id, template_type):
url_for('.view_template', service_id=service_id, template_id=new_template['data']['id'])
)
if email_or_sms_not_enabled(template_type, current_service['permissions']):
if email_or_sms_not_enabled(template_type, current_service.permissions):
return redirect(url_for(
'.action_blocked',
service_id=service_id,
@@ -444,7 +444,7 @@ def edit_service_template(service_id, template_id):
db_template = service_api_client.get_service_template(service_id, template_id)['data']
if email_or_sms_not_enabled(db_template['template_type'], current_service['permissions']):
if email_or_sms_not_enabled(db_template['template_type'], current_service.permissions):
return redirect(url_for(
'.action_blocked',
service_id=service_id,