2017-01-27 12:21:28 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
2017-01-27 15:57:25 +00:00
|
|
|
import pytz
|
2016-06-28 15:17:36 +01:00
|
|
|
from flask import url_for
|
2016-12-09 15:56:25 +00:00
|
|
|
from notifications_utils.template import SMSMessageTemplate, PlainTextEmailTemplate
|
2016-06-28 15:17:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def pagination_links(pagination, endpoint, **kwargs):
|
|
|
|
|
if 'page' in kwargs:
|
|
|
|
|
kwargs.pop('page', None)
|
2016-09-21 16:54:02 +01:00
|
|
|
links = {}
|
2016-06-28 15:17:36 +01:00
|
|
|
if pagination.has_prev:
|
|
|
|
|
links['prev'] = url_for(endpoint, page=pagination.prev_num, **kwargs)
|
|
|
|
|
if pagination.has_next:
|
|
|
|
|
links['next'] = url_for(endpoint, page=pagination.next_num, **kwargs)
|
|
|
|
|
links['last'] = url_for(endpoint, page=pagination.pages, **kwargs)
|
|
|
|
|
return links
|
2016-10-13 11:59:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def url_with_token(data, url, config):
|
|
|
|
|
from notifications_utils.url_safe_token import generate_token
|
|
|
|
|
token = generate_token(data, config['SECRET_KEY'], config['DANGEROUS_SALT'])
|
|
|
|
|
base_url = config['ADMIN_BASE_URL'] + url
|
|
|
|
|
return base_url + token
|
2016-12-09 15:56:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_template_instance(template, values):
|
2017-01-27 16:39:01 +00:00
|
|
|
from app.models import SMS_TYPE, EMAIL_TYPE
|
2016-12-09 15:56:25 +00:00
|
|
|
return {
|
|
|
|
|
SMS_TYPE: SMSMessageTemplate, EMAIL_TYPE: PlainTextEmailTemplate
|
|
|
|
|
}[template['template_type']](template, values)
|
2017-01-27 12:21:28 +00:00
|
|
|
|
|
|
|
|
|
2017-01-27 15:57:25 +00:00
|
|
|
def get_london_midnight_in_utc(date):
|
|
|
|
|
"""
|
|
|
|
|
This function converts date to midnight as BST (British Standard Time) to UTC,
|
|
|
|
|
the tzinfo is lastly removed from the datetime because the database stores the timestamps without timezone.
|
|
|
|
|
:param date: the day to calculate the London midnight in UTC for
|
|
|
|
|
:return: the datetime of London midnight in UTC, for example 2016-06-17 = 2016-06-17 23:00:00
|
|
|
|
|
"""
|
|
|
|
|
return pytz.timezone('Europe/London').localize(datetime.combine(date, datetime.min.time())).astimezone(
|
|
|
|
|
pytz.UTC).replace(
|
|
|
|
|
tzinfo=None)
|
2017-01-27 12:21:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_midnight_for_day_before(date):
|
|
|
|
|
day_before = date - timedelta(1)
|
2017-01-27 15:57:25 +00:00
|
|
|
return get_london_midnight_in_utc(day_before)
|