mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-20 14:29:51 -04:00
Merge branch 'master' into celery-jobs
This commit is contained in:
@@ -71,7 +71,7 @@
|
||||
color: $text-colour;
|
||||
background-image: file-url('icon-important-2x.png');
|
||||
background-size: 34px 34px;
|
||||
background-position: 0 0px;
|
||||
background-position: 0 0;
|
||||
background-repeat: no-repeat;
|
||||
padding: 7px 0 5px 50px;
|
||||
}
|
||||
|
||||
@@ -7,9 +7,26 @@
|
||||
margin: 20px 0 10px 0;
|
||||
}
|
||||
|
||||
&-subject,
|
||||
&-from {
|
||||
margin: 10px 0;
|
||||
&-meta {
|
||||
|
||||
@include core-19;
|
||||
margin: 0;
|
||||
|
||||
td,
|
||||
th {
|
||||
@include core-19;
|
||||
border-bottom: 0;
|
||||
border-top: 1px solid $border-colour;
|
||||
}
|
||||
|
||||
th {
|
||||
color: $secondary-text-colour;
|
||||
}
|
||||
|
||||
td {
|
||||
width: 99%;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
&-from {
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
.sms-message-use-links {
|
||||
|
||||
@include copy-19;
|
||||
margin-top: 55px;
|
||||
margin-top: 52px;
|
||||
|
||||
a {
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ from flask import Blueprint
|
||||
main = Blueprint('main', __name__)
|
||||
|
||||
from app.main.views import (
|
||||
index, sign_in, sign_out, register, two_factor, verify, sms, add_service,
|
||||
index, sign_in, sign_out, register, two_factor, verify, send, add_service,
|
||||
code_not_received, jobs, dashboard, templates, service_settings, forgot_password,
|
||||
new_password, styleguide, user_profile, choose_service, api_keys, manage_users
|
||||
)
|
||||
|
||||
@@ -4,14 +4,14 @@ from app.utils import BrowsableItem
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
|
||||
def insert_service_template(name, content, service_id):
|
||||
def insert_service_template(name, type_, content, service_id, subject=None):
|
||||
return notifications_api_client.create_service_template(
|
||||
name, 'sms', content, service_id)
|
||||
name, type_, content, service_id, subject)
|
||||
|
||||
|
||||
def update_service_template(id_, name, content, service_id):
|
||||
def update_service_template(id_, name, type_, content, service_id, subject=None):
|
||||
return notifications_api_client.update_service_template(
|
||||
id_, name, 'sms', content, service_id)
|
||||
id_, name, type_, content, service_id)
|
||||
|
||||
|
||||
def get_service_templates(service_id):
|
||||
|
||||
@@ -7,7 +7,8 @@ from wtforms import (
|
||||
ValidationError,
|
||||
TextAreaField,
|
||||
FileField,
|
||||
RadioField
|
||||
RadioField,
|
||||
BooleanField
|
||||
)
|
||||
from wtforms.fields.html5 import EmailField, TelField
|
||||
from wtforms.validators import DataRequired, Email, Length, Regexp
|
||||
@@ -110,6 +111,7 @@ class TwoFactorForm(Form):
|
||||
super(TwoFactorForm, self).__init__(*args, **kwargs)
|
||||
|
||||
sms_code = sms_code()
|
||||
remember_me = BooleanField("Remember me")
|
||||
|
||||
def validate_sms_code(self, field):
|
||||
is_valid, reason = self.validate_code_func(field.data)
|
||||
@@ -188,7 +190,7 @@ class ConfirmPasswordForm(Form):
|
||||
raise ValidationError('Invalid password')
|
||||
|
||||
|
||||
class TemplateForm(Form):
|
||||
class SMSTemplateForm(Form):
|
||||
name = StringField(
|
||||
u'Template name',
|
||||
validators=[DataRequired(message="Template name cannot be empty")])
|
||||
@@ -198,6 +200,13 @@ class TemplateForm(Form):
|
||||
validators=[DataRequired(message="Template content cannot be empty")])
|
||||
|
||||
|
||||
class EmailTemplateForm(SMSTemplateForm):
|
||||
|
||||
subject = StringField(
|
||||
u'Subject',
|
||||
validators=[DataRequired(message="Subject cannot be empty")])
|
||||
|
||||
|
||||
class ForgotPasswordForm(Form):
|
||||
email_address = email_address()
|
||||
|
||||
|
||||
0
app/main/views/email.py
Normal file
0
app/main/views/email.py
Normal file
@@ -22,15 +22,3 @@ def register_from_invite():
|
||||
@login_required
|
||||
def verify_mobile():
|
||||
return render_template('views/verify-mobile.html')
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/send-email")
|
||||
@login_required
|
||||
def send_email(service_id):
|
||||
return render_template('views/send-email.html', service_id=service_id)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/check-email")
|
||||
@login_required
|
||||
def check_email(service_id):
|
||||
return render_template('views/check-email.html')
|
||||
|
||||
@@ -29,10 +29,11 @@ fake_users = [
|
||||
@main.route("/services/<service_id>/users")
|
||||
@login_required
|
||||
def manage_users(service_id):
|
||||
users = user_api_client.get_users_for_service(service_id=service_id)
|
||||
return render_template(
|
||||
'views/manage-users.html',
|
||||
service_id=service_id,
|
||||
users=fake_users,
|
||||
users=users,
|
||||
current_user=current_user,
|
||||
invited_users=[]
|
||||
)
|
||||
|
||||
@@ -28,15 +28,23 @@ from app.main.uploader import (
|
||||
s3download
|
||||
)
|
||||
from app.main.dao import templates_dao
|
||||
from app.main.dao import services_dao
|
||||
from app import job_api_client
|
||||
from app.utils import (
|
||||
validate_phone_number,
|
||||
InvalidPhoneError
|
||||
)
|
||||
from app.utils import validate_recipient, InvalidPhoneError, InvalidEmailError
|
||||
|
||||
first_column_header = {
|
||||
'email': 'email',
|
||||
'sms': 'phone'
|
||||
}
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send", methods=['GET'])
|
||||
def choose_sms_template(service_id):
|
||||
@main.route("/services/<service_id>/send/<template_type>", methods=['GET'])
|
||||
def choose_template(service_id, template_type):
|
||||
|
||||
services_dao.get_service_by_id_or_404(service_id)
|
||||
|
||||
if template_type not in ['email', 'sms']:
|
||||
abort(404)
|
||||
try:
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
except HTTPError as e:
|
||||
@@ -44,23 +52,20 @@ def choose_sms_template(service_id):
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
print("="*80)
|
||||
print(jobs)
|
||||
print(len(jobs))
|
||||
print(bool(len(jobs)))
|
||||
return render_template(
|
||||
'views/choose-sms-template.html',
|
||||
'views/choose-{}-template.html'.format(template_type),
|
||||
templates=[
|
||||
Template(template) for template in templates_dao.get_service_templates(service_id)['data']
|
||||
if template['template_type'] == template_type
|
||||
],
|
||||
has_jobs=len(jobs),
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send/<template_id>", methods=['GET', 'POST'])
|
||||
@main.route("/services/<service_id>/send/<int:template_id>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def send_sms(service_id, template_id):
|
||||
def send_messages(service_id, template_id):
|
||||
|
||||
form = CsvUploadForm()
|
||||
if form.validate_on_submit():
|
||||
@@ -70,48 +75,50 @@ def send_sms(service_id, template_id):
|
||||
upload_id = str(uuid.uuid4())
|
||||
s3upload(upload_id, service_id, filedata, current_app.config['AWS_REGION'])
|
||||
session['upload_data'] = {"template_id": template_id, "original_file_name": filedata['file_name']}
|
||||
return redirect(url_for('.check_sms',
|
||||
return redirect(url_for('.check_messages',
|
||||
service_id=service_id,
|
||||
upload_id=upload_id))
|
||||
except ValueError as e:
|
||||
flash('There was a problem uploading: {}'.format(csv_file.filename))
|
||||
flash(str(e))
|
||||
return redirect(url_for('.send_sms', service_id=service_id, template_id=template_id))
|
||||
return redirect(url_for('.send_messages', service_id=service_id, template_id=template_id))
|
||||
|
||||
service = services_dao.get_service_by_id_or_404(service_id)
|
||||
template = Template(
|
||||
templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
||||
)
|
||||
|
||||
return render_template(
|
||||
'views/send-sms.html',
|
||||
'views/send.html',
|
||||
template=template,
|
||||
column_headers=['phone'] + template.placeholders_as_markup,
|
||||
column_headers=[first_column_header[template.template_type]] + template.placeholders_as_markup,
|
||||
form=form,
|
||||
service=service,
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send/<template_id>.csv", methods=['GET'])
|
||||
@main.route("/services/<service_id>/send/<template_id>.csv", methods=['GET'])
|
||||
@login_required
|
||||
def get_example_csv(service_id, template_id):
|
||||
template = templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
||||
placeholders = list(Template(template).placeholders)
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
writer.writerow(['phone'] + placeholders)
|
||||
writer.writerow([first_column_header[template['template_type']]] + placeholders)
|
||||
writer.writerow([current_user.mobile_number] + ["test {}".format(header) for header in placeholders])
|
||||
|
||||
return(output.getvalue(), 200, {'Content-Type': 'text/csv; charset=utf-8'})
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send/<template_id>/to-self", methods=['GET'])
|
||||
@main.route("/services/<service_id>/send/<template_id>/to-self", methods=['GET'])
|
||||
@login_required
|
||||
def send_sms_to_self(service_id, template_id):
|
||||
def send_message_to_self(service_id, template_id):
|
||||
template = templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
||||
placeholders = list(Template(template).placeholders)
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
writer.writerow(['phone'] + placeholders)
|
||||
writer.writerow([first_column_header[template['template_type']]] + placeholders)
|
||||
writer.writerow([current_user.mobile_number] + ["test {}".format(header) for header in placeholders])
|
||||
filedata = {
|
||||
'file_name': 'Test run',
|
||||
@@ -121,35 +128,37 @@ def send_sms_to_self(service_id, template_id):
|
||||
s3upload(upload_id, service_id, filedata, current_app.config['AWS_REGION'])
|
||||
session['upload_data'] = {"template_id": template_id, "original_file_name": filedata['file_name']}
|
||||
|
||||
return redirect(url_for('.check_sms',
|
||||
return redirect(url_for('.check_messages',
|
||||
service_id=service_id,
|
||||
upload_id=upload_id))
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/check/<upload_id>",
|
||||
@main.route("/services/<service_id>/check/<upload_id>",
|
||||
methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def check_sms(service_id, upload_id):
|
||||
def check_messages(service_id, upload_id):
|
||||
|
||||
upload_data = session['upload_data']
|
||||
template_id = upload_data.get('template_id')
|
||||
|
||||
if request.method == 'GET':
|
||||
contents = s3download(service_id, upload_id)
|
||||
if not contents:
|
||||
flash('There was a problem reading your upload file')
|
||||
upload_data = session['upload_data']
|
||||
template_id = upload_data.get('template_id')
|
||||
raw_template = templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
||||
recipient_type = first_column_header[raw_template['template_type']]
|
||||
upload_result = _get_rows(contents, raw_template)
|
||||
session['upload_data']['notification_count'] = len(upload_result['rows'])
|
||||
template = Template(
|
||||
raw_template,
|
||||
values=upload_result['rows'][0] if upload_result['valid'] else {},
|
||||
drop_values={'phone'}
|
||||
drop_values={recipient_type}
|
||||
)
|
||||
return render_template(
|
||||
'views/check-sms.html',
|
||||
upload_result=upload_result,
|
||||
template=template,
|
||||
column_headers=['phone number'] + list(
|
||||
column_headers=[recipient_type] + list(
|
||||
template.placeholders if upload_result['valid'] else template.placeholders_as_markup
|
||||
),
|
||||
original_file_name=upload_data.get('original_file_name'),
|
||||
@@ -157,9 +166,7 @@ def check_sms(service_id, upload_id):
|
||||
form=CsvUploadForm()
|
||||
)
|
||||
elif request.method == 'POST':
|
||||
upload_data = session['upload_data']
|
||||
original_file_name = upload_data.get('original_file_name')
|
||||
template_id = upload_data.get('template_id')
|
||||
notification_count = upload_data.get('notification_count')
|
||||
session.pop('upload_data')
|
||||
try:
|
||||
@@ -171,9 +178,9 @@ def check_sms(service_id, upload_id):
|
||||
raise e
|
||||
|
||||
flash('We’ve started sending your messages', 'default_with_tick')
|
||||
return redirect(url_for('main.view_job',
|
||||
service_id=service_id,
|
||||
job_id=upload_id))
|
||||
return redirect(
|
||||
url_for('main.view_job', service_id=service_id, job_id=upload_id)
|
||||
)
|
||||
|
||||
|
||||
def _get_filedata(file):
|
||||
@@ -196,8 +203,12 @@ def _get_rows(contents, raw_template):
|
||||
for row in reader:
|
||||
rows.append(row)
|
||||
try:
|
||||
validate_phone_number(row['phone'])
|
||||
Template(raw_template, values=row, drop_values={'phone'}).replaced
|
||||
except (InvalidPhoneError, NeededByTemplateError, NoPlaceholderForDataError):
|
||||
recipient_column = first_column_header[raw_template['template_type']]
|
||||
validate_recipient(
|
||||
row[recipient_column],
|
||||
template_type=raw_template['template_type']
|
||||
)
|
||||
Template(raw_template, values=row, drop_values={recipient_column}).replaced
|
||||
except (InvalidEmailError, InvalidPhoneError, NeededByTemplateError, NoPlaceholderForDataError):
|
||||
valid = False
|
||||
return {"valid": valid, "rows": rows}
|
||||
@@ -7,10 +7,10 @@ from flask import (
|
||||
flash
|
||||
)
|
||||
|
||||
from flask.ext.login import current_user
|
||||
from flask.ext.login import (current_user, login_fresh, confirm_login)
|
||||
|
||||
from app.main import main
|
||||
from app.main.dao import users_dao
|
||||
from app.main.dao import (users_dao, services_dao)
|
||||
from app.main.forms import LoginForm
|
||||
|
||||
|
||||
@@ -18,11 +18,24 @@ from app.main.forms import LoginForm
|
||||
def sign_in():
|
||||
if current_user and current_user.is_authenticated():
|
||||
return redirect(url_for('main.choose_service'))
|
||||
|
||||
form = LoginForm()
|
||||
if form.validate_on_submit():
|
||||
user = users_dao.get_user_by_email(form.email_address.data)
|
||||
user = _get_and_verify_user(user, form.password.data)
|
||||
if user:
|
||||
# Remember me login
|
||||
if not login_fresh() and \
|
||||
not current_user.is_anonymous() and \
|
||||
current_user.id == user.id and \
|
||||
user.is_active():
|
||||
confirm_login()
|
||||
services = services_dao.get_services(user.id).get('data', [])
|
||||
if (len(services) == 1):
|
||||
return redirect(url_for('main.service_dashboard', service_id=services[0]['id']))
|
||||
else:
|
||||
return redirect(url_for('main.choose_service'))
|
||||
|
||||
session['user_details'] = {"email": user.email_address, "id": user.id}
|
||||
if user.state == 'pending':
|
||||
return redirect(url_for('.verify'))
|
||||
|
||||
@@ -6,8 +6,7 @@ from app.main import main
|
||||
|
||||
|
||||
@main.route('/sign-out', methods=(['GET']))
|
||||
@login_required
|
||||
def sign_out():
|
||||
session.clear()
|
||||
logout_user()
|
||||
return redirect(url_for('main.index'))
|
||||
return redirect(url_for('main.sign_in'))
|
||||
|
||||
@@ -5,45 +5,44 @@ from notifications_python_client.errors import HTTPError
|
||||
from utils.template import Template
|
||||
|
||||
from app.main import main
|
||||
from app.main.forms import TemplateForm
|
||||
from app.main.forms import SMSTemplateForm, EmailTemplateForm
|
||||
from app import job_api_client
|
||||
from app.main.dao.services_dao import get_service_by_id
|
||||
from app.main.dao.services_dao import get_service_by_id_or_404
|
||||
from app.main.dao import templates_dao as tdao
|
||||
from app.main.dao import services_dao as sdao
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates")
|
||||
form_objects = {
|
||||
'email': EmailTemplateForm,
|
||||
'sms': SMSTemplateForm
|
||||
}
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/add-<template_type>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def manage_service_templates(service_id):
|
||||
return redirect(url_for(
|
||||
'.choose_sms_template',
|
||||
service_id=service_id
|
||||
))
|
||||
def add_service_template(service_id, template_type):
|
||||
|
||||
service = sdao.get_service_by_id_or_404(service_id)
|
||||
|
||||
@main.route("/services/<service_id>/templates/add", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_service_template(service_id):
|
||||
try:
|
||||
service = sdao.get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
if template_type not in ['sms', 'email']:
|
||||
abort(404)
|
||||
|
||||
form = TemplateForm()
|
||||
form = form_objects[template_type]()
|
||||
|
||||
if form.validate_on_submit():
|
||||
tdao.insert_service_template(
|
||||
form.name.data, form.template_content.data, service_id)
|
||||
return redirect(url_for(
|
||||
'.choose_sms_template', service_id=service_id))
|
||||
form.name.data, template_type, form.template_content.data, service_id, form.subject.data or None
|
||||
)
|
||||
return redirect(
|
||||
url_for('.choose_template', service_id=service_id, template_type=template_type)
|
||||
)
|
||||
|
||||
return render_template(
|
||||
'views/edit-template.html',
|
||||
h1='Add a text message template',
|
||||
'views/edit-{}-template.html'.format(template_type),
|
||||
form=form,
|
||||
service_id=service_id)
|
||||
template_type=template_type,
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/<int:template_id>", methods=['GET', 'POST'])
|
||||
@@ -51,20 +50,26 @@ def add_service_template(service_id):
|
||||
def edit_service_template(service_id, template_id):
|
||||
template = tdao.get_service_template_or_404(service_id, template_id)['data']
|
||||
template['template_content'] = template['content']
|
||||
form = TemplateForm(**template)
|
||||
form = form_objects[template['template_type']](**template)
|
||||
|
||||
if form.validate_on_submit():
|
||||
tdao.update_service_template(
|
||||
template_id, form.name.data,
|
||||
form.template_content.data, service_id)
|
||||
return redirect(url_for('.choose_sms_template', service_id=service_id))
|
||||
template_id, form.name.data, template['template_type'],
|
||||
form.template_content.data, service_id
|
||||
)
|
||||
return redirect(url_for(
|
||||
'.choose_template',
|
||||
service_id=service_id,
|
||||
template_type=template['template_type']
|
||||
))
|
||||
|
||||
return render_template(
|
||||
'views/edit-template.html',
|
||||
h1='Edit template',
|
||||
'views/edit-{}-template.html'.format(template['template_type']),
|
||||
form=form,
|
||||
service_id=service_id,
|
||||
template_id=template_id)
|
||||
template_id=template_id,
|
||||
template_type=template['template_type']
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/<int:template_id>/delete", methods=['GET', 'POST'])
|
||||
@@ -74,13 +79,17 @@ def delete_service_template(service_id, template_id):
|
||||
|
||||
if request.method == 'POST':
|
||||
tdao.delete_service_template(service_id, template_id)
|
||||
return redirect(url_for('.manage_service_templates', service_id=service_id))
|
||||
return redirect(url_for(
|
||||
'.choose_template',
|
||||
service_id=service_id,
|
||||
template_type=template['template_type']
|
||||
))
|
||||
|
||||
template['template_content'] = template['content']
|
||||
form = TemplateForm(**template)
|
||||
form = form_objects[template['template_type']](**template)
|
||||
flash('Are you sure you want to delete ‘{}’?'.format(form.name.data), 'delete')
|
||||
return render_template(
|
||||
'views/edit-template.html',
|
||||
'views/edit-{}-template.html'.format(template['template_type']),
|
||||
h1='Edit template',
|
||||
form=form,
|
||||
service_id=service_id,
|
||||
|
||||
@@ -12,7 +12,10 @@ from app.main.forms import TwoFactorForm
|
||||
@main.route('/two-factor', methods=['GET', 'POST'])
|
||||
def two_factor():
|
||||
# TODO handle user_email not in session
|
||||
user_id = session['user_details']['id']
|
||||
try:
|
||||
user_id = session['user_details']['id']
|
||||
except KeyError:
|
||||
return redirect('main.sign_in')
|
||||
|
||||
def _check_code(code):
|
||||
return users_dao.check_verify_code(user_id, code, "sms")
|
||||
@@ -27,7 +30,7 @@ def two_factor():
|
||||
if 'password' in session['user_details']:
|
||||
user.set_password(session['user_details']['password'])
|
||||
users_dao.update_user(user)
|
||||
login_user(user)
|
||||
login_user(user, remember=form.remember_me.data if form.remember_me.data else False)
|
||||
finally:
|
||||
del session['user_details']
|
||||
if (len(services) == 1):
|
||||
|
||||
@@ -70,7 +70,7 @@ class NotificationsAdminAPIClient(NotificationsAPIClient):
|
||||
endpoint = "/service/{0}".format(service_id)
|
||||
return self.put(endpoint, data)
|
||||
|
||||
def create_service_template(self, name, type_, content, service_id):
|
||||
def create_service_template(self, name, type_, content, service_id, subject=None):
|
||||
"""
|
||||
Create a service template.
|
||||
"""
|
||||
@@ -80,10 +80,14 @@ class NotificationsAdminAPIClient(NotificationsAPIClient):
|
||||
"content": content,
|
||||
"service": service_id
|
||||
}
|
||||
if subject:
|
||||
data.update({
|
||||
'subject': subject
|
||||
})
|
||||
endpoint = "/service/{0}/template".format(service_id)
|
||||
return self.post(endpoint, data)
|
||||
|
||||
def update_service_template(self, id_, name, type_, content, service_id):
|
||||
def update_service_template(self, id_, name, type_, content, service_id, subject=None):
|
||||
"""
|
||||
Update a service template.
|
||||
"""
|
||||
@@ -94,8 +98,12 @@ class NotificationsAdminAPIClient(NotificationsAPIClient):
|
||||
'content': content,
|
||||
'service': service_id
|
||||
}
|
||||
if subject:
|
||||
data.update({
|
||||
'subject': subject
|
||||
})
|
||||
endpoint = "/service/{0}/template/{1}".format(service_id, id_)
|
||||
return self.put(endpoint, data)
|
||||
return self.post(endpoint, data)
|
||||
|
||||
def get_service_template(self, service_id, template_id, *params):
|
||||
"""
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from notifications_python_client.notifications import BaseAPIClient
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
from flask.ext.login import UserMixin
|
||||
from flask.ext.login import (UserMixin, login_fresh)
|
||||
|
||||
|
||||
class UserApiClient(BaseAPIClient):
|
||||
@@ -81,6 +81,11 @@ class UserApiClient(BaseAPIClient):
|
||||
return False, 'Code not found'
|
||||
raise e
|
||||
|
||||
def get_users_for_service(self, service_id):
|
||||
endpoint = '/service/{}/users'.format(service_id)
|
||||
resp = self.get(endpoint)
|
||||
return resp['data']
|
||||
|
||||
|
||||
class User(UserMixin):
|
||||
def __init__(self, fields, max_failed_login_count=3):
|
||||
@@ -100,6 +105,12 @@ class User(UserMixin):
|
||||
def is_active(self):
|
||||
return self.state == 'active'
|
||||
|
||||
def is_authenticated(self):
|
||||
# To handle remember me token renewal
|
||||
if not login_fresh():
|
||||
return False
|
||||
return super(User, self).is_authenticated()
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
|
||||
30
app/templates/components/checkbox.html
Normal file
30
app/templates/components/checkbox.html
Normal file
@@ -0,0 +1,30 @@
|
||||
{% macro checkbox(
|
||||
field,
|
||||
hint=False,
|
||||
help_link=None,
|
||||
help_link_text=None,
|
||||
width='2-3',
|
||||
suffix=None
|
||||
) %}
|
||||
<label class="form-checkbox" for="{{ field.name }}">
|
||||
{{ field.label }}
|
||||
{% if hint %}
|
||||
<span class="form-hint">
|
||||
{{ hint }}
|
||||
</span>
|
||||
{% endif %}
|
||||
{% if field.errors %}
|
||||
<span class="error-message">
|
||||
{{ field.errors[0] }}
|
||||
</span>
|
||||
{% endif %}
|
||||
{{ field(**{
|
||||
'class': 'form-control form-control-{} textbox-highlight-textbox'.format(width) if highlight_tags else 'form-control form-control-{} {}'.format(width, 'textbox-right-aligned' if suffix else ''),
|
||||
'data-module': 'highlight-tags' if highlight_tags else ''})}}
|
||||
{% if help_link and help_link_text %}
|
||||
<p class="textbox-help-link">
|
||||
<a href='{{ help_link }}'>{{ help_link_text }}</a>
|
||||
</p>
|
||||
{% endif %}
|
||||
</label>
|
||||
{% endmacro %}
|
||||
@@ -9,29 +9,27 @@
|
||||
</h3>
|
||||
{% endif %}
|
||||
<div class="email-message">
|
||||
{% if from_name and from_address %}
|
||||
<div class="email-message-from">
|
||||
<div class="grid-row">
|
||||
<div class="column-one-eighth">
|
||||
<span class="form-hint">From</span>
|
||||
</div>
|
||||
<div class="column-seven-eighths">
|
||||
{{ from_name }} <{{ from_address }}>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if subject %}
|
||||
<div class="email-message-subject">
|
||||
<div class="grid-row">
|
||||
<div class="column-one-eighth">
|
||||
<span class="form-hint">Subject</span>
|
||||
</div>
|
||||
<div class="column-seven-eighths">
|
||||
{{ subject }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% if from_name or subject %}
|
||||
<table class="email-message-meta">
|
||||
<tbody>
|
||||
{% if from_name and from_address %}
|
||||
<tr>
|
||||
<th>From</th>
|
||||
<td>
|
||||
{{ from_name }} <{{ from_address }}>
|
||||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% if subject %}
|
||||
<tr class="email-message-meta">
|
||||
<th>Subject</th>
|
||||
<td>
|
||||
{{ subject }}
|
||||
</td>
|
||||
</div>
|
||||
{% endif %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endif %}
|
||||
<div class="email-message-body">
|
||||
{{ body|nl2br }}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
<a href="{{ url_for('.service_dashboard', service_id=service_id) }}">{{ session.get('service_name', 'Service') }}</a>
|
||||
</h2>
|
||||
<ul>
|
||||
<li><a href="{{ url_for('.choose_sms_template', service_id=service_id) }}">Send text messages</a></li>
|
||||
<li><a href="{{ url_for('.send_email', service_id=service_id) }}">Send emails</a></li>
|
||||
<li><a href="{{ url_for('.choose_template', service_id=service_id, template_type='sms') }}">Send text messages</a></li>
|
||||
<li><a href="{{ url_for('.choose_template', service_id=service_id, template_type='email') }}">Send emails</a></li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><a href="{{ url_for('.manage_users', service_id=service_id) }}">Manage team</a></li>
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
<form method="post" enctype="multipart/form-data">
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
|
||||
<input type="submit" class="button" value="{{ "Send {} text message{}".format(upload_result.rows|count, '' if upload_result.rows|count == 1 else 's') }}" />
|
||||
<a href="{{url_for('.send_sms', service_id=service_id, template_id=template.id)}}" class="page-footer-back-link">Back</a>
|
||||
<a href="{{url_for('.send_messages', service_id=service_id, template_id=template.id)}}" class="page-footer-back-link">Back</a>
|
||||
</form>
|
||||
{% else %}
|
||||
{{file_upload(form.file, button_text='Upload a CSV file')}}
|
||||
|
||||
36
app/templates/views/choose-email-template.html
Normal file
36
app/templates/views/choose-email-template.html
Normal file
@@ -0,0 +1,36 @@
|
||||
{% extends "withnav_template.html" %}
|
||||
{% from "components/email-message.html" import email_message %}
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
{% from "components/textbox.html" import textbox %}
|
||||
|
||||
{% block page_title %}
|
||||
Send emails – GOV.UK Notify
|
||||
{% endblock %}
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<h1 class="heading-large">Send emails</h1>
|
||||
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
|
||||
{% if templates %}
|
||||
<div class="grid-row">
|
||||
{% for template in templates %}
|
||||
<div class="column-two-thirds">
|
||||
{{ email_message(template.subject, template.formatted_as_markup, name=template.name) }}
|
||||
</div>
|
||||
<div class="column-one-third">
|
||||
<div class="sms-message-use-links">
|
||||
<a href="{{ url_for(".edit_service_template", service_id=service_id, template_id=template.id) }}">Edit template</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<p>
|
||||
<a href="{{ url_for('.add_service_template', service_id=service_id, template_type='email') }}" class="button">Add a new template</a>
|
||||
</p>
|
||||
|
||||
</form>
|
||||
{% endblock %}
|
||||
@@ -30,8 +30,8 @@
|
||||
</div>
|
||||
<div class="column-one-third">
|
||||
<div class="sms-message-use-links">
|
||||
<a href="{{ url_for(".send_sms", service_id=service_id, template_id=template.id) }}">Add recipients</a>
|
||||
<a href="{{ url_for(".send_sms_to_self", service_id=service_id, template_id=template.id) }}">Send yourself a test</a>
|
||||
<a href="{{ url_for(".send_messages", service_id=service_id, template_id=template.id) }}">Add recipients</a>
|
||||
<a href="{{ url_for(".send_message_to_self", service_id=service_id, template_id=template.id) }}">Send yourself a test</a>
|
||||
<a href="{{ url_for(".edit_service_template", service_id=service_id, template_id=template.id) }}">Edit template</a>
|
||||
</div>
|
||||
</div>
|
||||
@@ -40,7 +40,7 @@
|
||||
{% endif %}
|
||||
|
||||
<p>
|
||||
<a href="{{ url_for('.add_service_template', service_id=service_id) }}" class="button">Add a new template</a>
|
||||
<a href="{{ url_for('.add_service_template', service_id=service_id, template_type='sms') }}" class="button">Add a new template</a>
|
||||
</p>
|
||||
|
||||
</form>
|
||||
|
||||
@@ -8,12 +8,15 @@
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<h1 class="heading-large">{{ h1 }}</h1>
|
||||
<h1 class="heading-large">Edit email template</h1>
|
||||
|
||||
<form method="post">
|
||||
<div class="grid-row">
|
||||
<div class="column-two-thirds">
|
||||
{{ textbox(form.name, width='1-1') }}
|
||||
{% if 'email' == template_type %}
|
||||
{{ textbox(form.subject, width='1-1') }}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid-row">
|
||||
@@ -31,7 +34,7 @@
|
||||
'Save',
|
||||
delete_link=url_for('.delete_service_template', service_id=service_id, template_id=template_id) if template_id or None,
|
||||
delete_link_text='Delete this template',
|
||||
back_link=url_for('.choose_sms_template', service_id=service_id),
|
||||
back_link=url_for('.choose_template', template_type=template_type, service_id=service_id),
|
||||
back_link_text='Cancel'
|
||||
) }}
|
||||
</form>
|
||||
43
app/templates/views/edit-sms-template.html
Normal file
43
app/templates/views/edit-sms-template.html
Normal file
@@ -0,0 +1,43 @@
|
||||
{% extends "withnav_template.html" %}
|
||||
{% from "components/textbox.html" import textbox %}
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
|
||||
{% block page_title %}
|
||||
{{ h1 }} – GOV.UK Notify
|
||||
{% endblock %}
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<h1 class="heading-large">Edit text message template</h1>
|
||||
|
||||
<form method="post">
|
||||
<div class="grid-row">
|
||||
<div class="column-two-thirds">
|
||||
{{ textbox(form.name, width='1-1') }}
|
||||
{% if 'email' == template_type %}
|
||||
{{ textbox(form.subject, width='1-1') }}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid-row">
|
||||
<div class="column-two-thirds">
|
||||
{{ textbox(form.template_content, highlight_tags=True, width='1-1') }}
|
||||
</div>
|
||||
<div class="column-one-third">
|
||||
<label for='template_content' class='edit-template-placeholder-hint'>
|
||||
Add placeholders using double brackets, eg Your thing
|
||||
is due on ((date))
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
{{ page_footer(
|
||||
'Save',
|
||||
delete_link=url_for('.delete_service_template', service_id=service_id, template_id=template_id) if template_id or None,
|
||||
delete_link_text='Delete this template',
|
||||
back_link=url_for('.choose_template', service_id=service_id, template_type=template_type),
|
||||
back_link_text='Cancel'
|
||||
) }}
|
||||
</form>
|
||||
|
||||
|
||||
{% endblock %}
|
||||
@@ -26,13 +26,13 @@ Manage users – GOV.UK Notify
|
||||
users, caption='Active', **table_options
|
||||
) %}
|
||||
{% call field() %}
|
||||
{{ current_user.name }}
|
||||
{{ item.name }}
|
||||
{% endcall %}
|
||||
{{ boolean_field(item.permission_send_messages) }}
|
||||
{{ boolean_field(item.permission_manage_service) }}
|
||||
{{ boolean_field(item.permission_manage_api_keys) }}
|
||||
{% call field(align='right') %}
|
||||
<a href="{{ url_for('.edit_user', service_id=service_id, user_id=0)}}">Change</a>
|
||||
<a href="{{ url_for('.edit_user', service_id=service_id, user_id=item.id)}}">Change</a>
|
||||
{% endcall %}
|
||||
{% endcall %}
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
{% extends "withnav_template.html" %}
|
||||
|
||||
{% block page_title %}
|
||||
Send email – GOV.UK Notify
|
||||
{% endblock %}
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<h1 class="heading-large">Send email</h1>
|
||||
|
||||
<p>This page will be where we construct email messages</p>
|
||||
|
||||
<p>
|
||||
<a class="button" href="check-email" role="button">Continue</a>
|
||||
</p>
|
||||
|
||||
{% endblock %}
|
||||
@@ -1,5 +1,6 @@
|
||||
{% extends "withnav_template.html" %}
|
||||
{% from "components/sms-message.html" import sms_message %}
|
||||
{% from "components/email-message.html" import email_message %}
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
{% from "components/file-upload.html" import file_upload %}
|
||||
{% from "components/table.html" import list_table, field %}
|
||||
@@ -14,14 +15,22 @@
|
||||
|
||||
<div class="grid-row">
|
||||
<div class="column-two-thirds">
|
||||
|
||||
{{ sms_message(template.formatted_as_markup) }}
|
||||
|
||||
{% if 'sms' == template.template_type %}
|
||||
{{ sms_message(template.formatted_as_markup) }}
|
||||
{% elif 'email' == template.template_type %}
|
||||
{{ email_message(
|
||||
template.subject,
|
||||
template.formatted_as_markup,
|
||||
from_address='{}@notifications.service.gov.uk'.format(service.email_from),
|
||||
from_name=service.name
|
||||
) }}
|
||||
{% endif %}
|
||||
{{ banner(
|
||||
'You can upload real data, but we’ll only send to your mobile number until you <a href="{}">request to go live</a>'|safe,
|
||||
'You can upload real data, but we’ll only send to your mobile number until you <a href="{}">request to go live</a>'.format(
|
||||
url_for('.service_request_to_go_live', service_id=service_id)
|
||||
)|safe,
|
||||
'info'
|
||||
)}}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
</ol>
|
||||
""".format(
|
||||
url_for(".add_service_template", service_id=service_id),
|
||||
url_for(".choose_sms_template", service_id=service_id)
|
||||
url_for(".choose_template", service_id=service_id, template_type="sms")
|
||||
)|safe,
|
||||
subhead='Get started',
|
||||
type="tip"
|
||||
@@ -46,7 +46,7 @@
|
||||
"""
|
||||
<a href='{}'>Send yourself a text message</a>
|
||||
""".format(
|
||||
url_for(".choose_sms_template", service_id=service_id)
|
||||
url_for(".choose_template", service_id=service_id, template_type="sms")
|
||||
)|safe,
|
||||
subhead='Next step',
|
||||
type="tip"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{% extends "withoutnav_template.html" %}
|
||||
{% from "components/textbox.html" import textbox %}
|
||||
{% from "components/checkbox.html" import checkbox %}
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
|
||||
{% block page_title %}
|
||||
@@ -22,6 +23,7 @@
|
||||
help_link=url_for('.verification_code_not_received'),
|
||||
help_link_text='I haven’t received a text message'
|
||||
) }}
|
||||
{{ checkbox(form.remember_me) }}
|
||||
{{ page_footer(
|
||||
"Continue"
|
||||
) }}
|
||||
|
||||
20
app/utils.py
20
app/utils.py
@@ -1,3 +1,5 @@
|
||||
import re
|
||||
|
||||
from functools import wraps
|
||||
from flask import abort
|
||||
|
||||
@@ -28,6 +30,11 @@ class BrowsableItem(object):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidEmailError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
|
||||
class InvalidPhoneError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
@@ -74,6 +81,19 @@ def format_phone_number(number):
|
||||
return '+447{}{}{}'.format(*re.findall('...', number))
|
||||
|
||||
|
||||
def validate_email_address(email_address):
|
||||
if re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email_address):
|
||||
return
|
||||
raise InvalidEmailError('Not a valid email address')
|
||||
|
||||
|
||||
def validate_recipient(recipient, template_type):
|
||||
return {
|
||||
'email': validate_email_address,
|
||||
'sms': validate_phone_number
|
||||
}[template_type](recipient)
|
||||
|
||||
|
||||
def user_has_permissions(*permissions):
|
||||
def wrap(func):
|
||||
@wraps(func)
|
||||
|
||||
Reference in New Issue
Block a user