2016-02-22 17:17:18 +00:00
|
|
|
|
import re
|
|
|
|
|
|
|
2016-02-19 16:38:04 +00:00
|
|
|
|
from functools import wraps
|
2016-02-29 14:57:07 +00:00
|
|
|
|
from flask import (abort, session)
|
2016-02-19 16:38:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-01-18 11:15:14 +00:00
|
|
|
|
class BrowsableItem(object):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Maps for the template browse-list.
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, item, *args, **kwargs):
|
|
|
|
|
|
self._item = item
|
|
|
|
|
|
super(BrowsableItem, self).__init__()
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def title(self):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def link(self):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def hint(self):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def destructive(self):
|
|
|
|
|
|
pass
|
2016-02-01 16:57:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-03-17 10:46:47 +00:00
|
|
|
|
def user_has_permissions(*permissions, admin_override=False, or_=False):
|
2016-02-19 16:38:04 +00:00
|
|
|
|
def wrap(func):
|
|
|
|
|
|
@wraps(func)
|
|
|
|
|
|
def wrap_func(*args, **kwargs):
|
|
|
|
|
|
from flask_login import current_user
|
2016-03-17 10:46:47 +00:00
|
|
|
|
if current_user and admin_override and current_user.platform_admin:
|
|
|
|
|
|
return func(*args, **kwargs)
|
2016-03-18 10:49:22 +00:00
|
|
|
|
from flask import request
|
|
|
|
|
|
if current_user and current_user.has_permissions(permissions, service_id=request.view_args.get('service_id', None), admin_override=admin_override, or_=or_):
|
2016-02-29 14:57:07 +00:00
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
else:
|
2016-02-19 16:38:04 +00:00
|
|
|
|
abort(403)
|
|
|
|
|
|
return wrap_func
|
|
|
|
|
|
return wrap
|
2016-03-07 18:47:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_errors_for_csv(recipients, template_type):
|
|
|
|
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
|
|
|
|
missing_column_headers = list(recipients.missing_column_headers)
|
|
|
|
|
|
|
|
|
|
|
|
if len(missing_column_headers) == 1:
|
|
|
|
|
|
errors.append("add a column called ‘{}’".format("".join(missing_column_headers)))
|
|
|
|
|
|
elif len(missing_column_headers) == 2:
|
|
|
|
|
|
errors.append("add 2 columns, ‘{}’".format("’ and ‘".join(missing_column_headers)))
|
|
|
|
|
|
elif len(missing_column_headers) > 2:
|
|
|
|
|
|
errors.append(
|
|
|
|
|
|
"add columns called ‘{}’, and ‘{}’".format(
|
|
|
|
|
|
"’, ‘".join(missing_column_headers[0:-1]),
|
|
|
|
|
|
missing_column_headers[-1]
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if recipients.rows_with_bad_recipients:
|
|
|
|
|
|
number_of_bad_recipients = len(list(recipients.rows_with_bad_recipients))
|
|
|
|
|
|
if 'sms' == template_type:
|
|
|
|
|
|
if 1 == number_of_bad_recipients:
|
|
|
|
|
|
errors.append("fix 1 phone number")
|
|
|
|
|
|
else:
|
|
|
|
|
|
errors.append("fix {} phone numbers".format(number_of_bad_recipients))
|
|
|
|
|
|
elif 'email' == template_type:
|
|
|
|
|
|
if 1 == number_of_bad_recipients:
|
|
|
|
|
|
errors.append("fix 1 email address")
|
|
|
|
|
|
else:
|
|
|
|
|
|
errors.append("fix {} email addresses".format(number_of_bad_recipients))
|
|
|
|
|
|
|
|
|
|
|
|
if recipients.rows_with_missing_data:
|
|
|
|
|
|
number_of_rows_with_missing_data = len(list(recipients.rows_with_missing_data))
|
|
|
|
|
|
if 1 == number_of_rows_with_missing_data:
|
|
|
|
|
|
errors.append("fill in 1 empty cell")
|
|
|
|
|
|
else:
|
|
|
|
|
|
errors.append("fill in {} empty cells".format(number_of_rows_with_missing_data))
|
|
|
|
|
|
|
|
|
|
|
|
return errors
|