Skip to content

Commit

Permalink
Incorporate breaking utils changes
Browse files Browse the repository at this point in the history
  • Loading branch information
quis committed Dec 14, 2016
1 parent ae356fc commit dc63de0
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 100 deletions.
10 changes: 4 additions & 6 deletions app/main/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from app import convert_to_boolean
from flask_login import (login_required, current_user)


from notifications_utils.renderers import HTMLEmail
from notifications_utils.template import HTMLEmailTemplate


@main.route('/')
Expand Down Expand Up @@ -57,9 +56,7 @@ def terms():

@main.route('/_email')
def email_template():
return HTMLEmail(
govuk_banner=convert_to_boolean(request.args.get('govuk_banner', True))
)(
return str(HTMLEmailTemplate({'subject': 'foo', 'content': (
'Lorem Ipsum is simply dummy text of the printing and typesetting '
'industry.\n\nLorem Ipsum has been the industry’s standard dummy '
'text ever since the 1500s, when an unknown printer took a galley '
Expand Down Expand Up @@ -96,7 +93,8 @@ def email_template():
'This is an example of an email sent using GOV.UK Notify.'
'\n\n'
'https://www.notifications.service.gov.uk'
)
)}, govuk_banner=convert_to_boolean(request.args.get('govuk_banner', True))
))


@main.route('/documentation')
Expand Down
9 changes: 4 additions & 5 deletions app/main/views/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
)
from flask_login import login_required
from werkzeug.datastructures import MultiDict
from notifications_utils.template import Template

from app import (
job_api_client,
Expand All @@ -31,7 +30,8 @@
generate_previous_dict,
user_has_permissions,
generate_notifications_csv,
get_help_argument
get_help_argument,
get_template,
)
from app.statistics_utils import add_rate_to_job

Expand Down Expand Up @@ -108,14 +108,13 @@ def view_job(service_id, job_id):
'views/jobs/job.html',
finished=(total_notifications == processed_notifications),
uploaded_file_name=job['original_file_name'],
template=Template(
template=get_template(
service_api_client.get_service_template(
service_id=service_id,
template_id=job['template'],
version=job['template_version']
)['data'],
prefix=current_service['name'],
sms_sender=current_service['sms_sender']
current_service,
),
status=request.args.get('status', ''),
updates_url=url_for(
Expand Down
44 changes: 20 additions & 24 deletions app/main/views/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

from flask_login import login_required, current_user
from notifications_utils.columns import Columns
from notifications_utils.template import Template
from notifications_utils.recipients import RecipientCSV, first_column_headings, validate_and_format_phone_number
from notifications_utils.renderers import EmailPreview, SMSPreview, LetterPDFLink

from app.main import main
from app.main.forms import CsvUploadForm, ChooseTimeForm, get_next_days_until, get_furthest_possible_scheduled_time
Expand All @@ -29,7 +27,7 @@
s3download
)
from app import job_api_client, service_api_client, current_service, user_api_client
from app.utils import user_has_permissions, get_errors_for_csv, Spreadsheet, get_help_argument, get_renderer
from app.utils import user_has_permissions, get_errors_for_csv, Spreadsheet, get_help_argument, get_template


def get_page_headings(template_type):
Expand Down Expand Up @@ -89,10 +87,8 @@ def choose_template(service_id, template_type):
return render_template(
'views/templates/choose.html',
templates=[
Template(
template,
renderer=get_renderer(template_type, current_service, show_recipient=False)
) for template in service_api_client.get_service_templates(service_id)['data']
get_template(template, current_service)
for template in service_api_client.get_service_templates(service_id)['data']
if template['template_type'] == template_type
],
template_type=template_type,
Expand All @@ -104,10 +100,12 @@ def choose_template(service_id, template_type):
@login_required
@user_has_permissions('send_texts', 'send_emails', 'send_letters')
def send_messages(service_id, template_id):
template = Template(
service_api_client.get_service_template(service_id, template_id)['data']

template = get_template(
service_api_client.get_service_template(service_id, template_id)['data'],
current_service,
show_recipient=True
)
template.renderer = get_renderer(template.template_type, current_service, show_recipient=True)

form = CsvUploadForm()
if form.validate_on_submit():
Expand Down Expand Up @@ -145,7 +143,9 @@ def send_messages(service_id, template_id):
@login_required
@user_has_permissions('send_texts', 'send_emails', 'send_letters', 'manage_templates', any_=True)
def get_example_csv(service_id, template_id):
template = Template(service_api_client.get_service_template(service_id, template_id)['data'])
template = get_template(
service_api_client.get_service_template(service_id, template_id)['data'], current_service
)
return Spreadsheet.from_rows([
first_column_headings[template.template_type] + list(template.placeholders),
get_example_csv_rows(template)
Expand All @@ -162,14 +162,12 @@ def send_test(service_id, template_id):

file_name = current_app.config['TEST_MESSAGE_FILENAME']

template = Template(
template = get_template(
service_api_client.get_service_template(service_id, template_id)['data'],
prefix=current_service['name'],
sms_sender=current_service['sms_sender']
current_service,
show_recipient=True
)

template.renderer = get_renderer(template.template_type, current_service, show_recipient=True)

if len(template.placeholders) == 0 or request.method == 'POST':
upload_id = s3upload(
service_id,
Expand Down Expand Up @@ -209,10 +207,8 @@ def send_test(service_id, template_id):
def send_from_api(service_id, template_id):
return render_template(
'views/send-from-api.html',
template=Template(
service_api_client.get_service_template(service_id, template_id)['data'],
prefix=current_service['name'],
sms_sender=current_service['sms_sender']
template=get_template(
service_api_client.get_service_template(service_id, template_id)['data'], current_service
)
)

Expand All @@ -234,15 +230,15 @@ def check_messages(service_id, template_type, upload_id):
if not contents:
flash('There was a problem reading your upload file')

template = Template(
template = get_template(
service_api_client.get_service_template(
service_id,
session['upload_data'].get('template_id')
)['data']
)['data'],
current_service,
show_recipient=True
)

template.renderer = get_renderer(template_type, current_service, show_recipient=True)

recipients = RecipientCSV(
contents,
template_type=template.template_type,
Expand Down
59 changes: 24 additions & 35 deletions app/main/views/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
from dateutil.parser import parse
from wand.image import Image

from notifications_utils.template import Template
from notifications_utils.template import LetterPreviewTemplate
from notifications_utils.recipients import first_column_headings
from notifications_utils.renderers import LetterPreview
from notifications_python_client.errors import HTTPError

from app.main import main
from app.utils import user_has_permissions, get_renderer
from app.utils import user_has_permissions, get_template
from app.main.forms import SMSTemplateForm, EmailTemplateForm, LetterTemplateForm
from app.main.views.send import get_example_csv_rows
from app import service_api_client, current_service, template_statistics_client
Expand Down Expand Up @@ -43,27 +42,25 @@
admin_override=True, any_=True
)
def view_template(service_id, template_id):
template = Template(
service_api_client.get_service_template(service_id, template_id)['data']
)
template.renderer = get_renderer(
template.template_type, current_service, show_recipient=False, expand_emails=True
)
return render_template(
'views/templates/template.html',
template=template
template=get_template(
service_api_client.get_service_template(service_id, template_id)['data'],
current_service,
expand_emails=True
)
)


@main.route("/services/<service_id>/templates/<template_id>.pdf")
@login_required
@user_has_permissions('view_activity', admin_override=True)
def view_letter_template_as_pdf(service_id, template_id):
template = Template(
service_api_client.get_service_template(service_id, template_id)['data'],
renderer=LetterPreview()
)
return render_pdf(HTML(string=template.rendered))
return render_pdf(HTML(string=str(
LetterPreviewTemplate(
service_api_client.get_service_template(service_id, template_id)['data'],
)
)))


@main.route("/services/<service_id>/templates/<template_id>.png")
Expand Down Expand Up @@ -92,15 +89,13 @@ def view_letter_template_as_image(service_id, template_id):
any_=True
)
def view_template_version(service_id, template_id, version):
template = Template(
service_api_client.get_service_template(service_id, template_id, version)['data']
)
template.renderer = get_renderer(
template.template_type, current_service, show_recipient=False, expand_emails=True
)
return render_template(
'views/templates/template_history.html',
template=template
template=get_template(
service_api_client.get_service_template(service_id, template_id, version)['data'],
current_service,
expand_emails=True
)
)


Expand Down Expand Up @@ -154,14 +149,14 @@ def edit_service_template(service_id, template_id):

if form.validate_on_submit():
subject = form.subject.data if hasattr(form, 'subject') else None
new_template = Template({
new_template = get_template({
'name': form.name.data,
'content': form.template_content.data,
'subject': subject,
'template_type': template['template_type'],
'id': template['id']
})
template_change = Template(template).compare_to(new_template)
}, current_service)
template_change = get_template(template, current_service).compare_to(new_template)
if template_change.has_different_placeholders and not request.form.get('confirm'):
return render_template(
'views/templates/breaking-change.html',
Expand Down Expand Up @@ -260,18 +255,12 @@ def delete_service_template(service_id, template_id):
any_=True
)
def view_template_versions(service_id, template_id):

versions = []
for template in service_api_client.get_service_template_versions(service_id, template_id)['data']:
template = Template(template)
template.renderer = get_renderer(
template.template_type, current_service, show_recipient=False, expand_emails=True
)
versions.append(template)

return render_template(
'views/templates/choose_history.html',
versions=versions
versions=[
get_template(template, current_service, expand_emails=True)
for template in service_api_client.get_service_template_versions(service_id, template_id)['data']
]
)


Expand Down
2 changes: 1 addition & 1 deletion app/templates/views/check.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ <h1 class="heading-large">

{% endif %}

{{ template.rendered }}
{{ template|string }}

{% if errors %}
{% if request.args.from_test %}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/views/jobs/job.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1 class="heading-large">
{{ uploaded_file_name }}
</h1>

{{ template.rendered }}
{{ template|string }}

{{ ajax_block(partials, updates_url, 'status', finished=finished) }}
{{ ajax_block(partials, updates_url, 'counts', finished=finished) }}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/views/send-from-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h1 class="heading-large">
API info
</h1>

{{ template.rendered }}
{{ template|string }}

<div class="bottom-gutter">
{{ api_key(template.id, name="Template ID", thing='template ID') }}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/views/send-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h1 class="heading-large">Example text message</h1>
<h1 class="heading-large">Send yourself a test</h1>
{% endif %}

{{ template.rendered }}
{{ template|string }}

<form method="post">
{% call(item, row_number) list_table(
Expand Down
2 changes: 1 addition & 1 deletion app/templates/views/send.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<h1 class="heading-large">Upload recipients</h1>

{{ template.rendered }}
{{ template|string }}

<div class="page-footer bottom-gutter">
{{file_upload(
Expand Down
2 changes: 1 addition & 1 deletion app/templates/views/templates/_template.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="column-two-thirds">
{{ template.rendered }}
{{ template|string }}
</div>
<div class="column-one-third">
{% if template._template.archived %}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/views/templates/_template_history.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h2 class="message-name">{{ template.name }}</h2>
</div>

<div class="column-two-thirds">
{{ template.rendered }}
{{ template|string }}
</div>

<div class="column-one-third">
Expand Down
28 changes: 19 additions & 9 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from flask import (abort, current_app, session, request, redirect, url_for)
from flask_login import current_user

from notifications_utils.renderers import SMSPreview, EmailPreview, LetterPDFLink
from notifications_utils.template import (
SMSPreviewTemplate,
EmailPreviewTemplate,
LetterPDFLinkTemplate,
)

import pyexcel
import pyexcel.ext.io
Expand Down Expand Up @@ -220,18 +224,24 @@ def is_gov_user(email_address):
return bool(re.search(email_regex, email_address.lower()))


def get_renderer(template_type, service, show_recipient, expand_emails=False):
return {
'email': EmailPreview(
def get_template(template, service, show_recipient=False, expand_emails=False):
if 'email' == template['template_type']:
return EmailPreviewTemplate(
template,
from_name=service['name'],
from_address='{}@notifications.service.gov.uk'.format(service['email_from']),
expanded=expand_emails,
show_recipient=show_recipient
),
'sms': SMSPreview(
)
if 'sms' == template['template_type']:
return SMSPreviewTemplate(
template,
prefix=service['name'],
sender=service['sms_sender'],
show_recipient=show_recipient
),
'letter': LetterPDFLink(service['id']),
}[template_type]
)
if 'letter' == template['template_type']:
return LetterPDFLinkTemplate(
template,
service_id=service['id'],
)
Loading

0 comments on commit dc63de0

Please sign in to comment.