Merge branch 'master' into remove-initial-update-sms-sender

This commit is contained in:
Rebecca Law
2017-11-15 12:33:38 +00:00
17 changed files with 178 additions and 53 deletions

View File

@@ -135,14 +135,16 @@ def create_or_update_free_sms_fragment_limit(service_id):
form = validate(req_args, create_or_update_free_sms_fragment_limit_schema)
financial_year_start = form.get('financial_year_start')
free_sms_fragment_limit = form.get('free_sms_fragment_limit')
update_free_sms_fragment_limit_data(service_id,
free_sms_fragment_limit=form.get('free_sms_fragment_limit'),
financial_year_start=form.get('financial_year_start'))
return jsonify(form), 201
def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, financial_year_start=None):
current_year = get_current_financial_year_start_year()
if financial_year_start is None or financial_year_start >= current_year:
dao_update_annual_billing_for_current_and_future_years(service_id, free_sms_fragment_limit)
else:
dao_create_or_update_annual_billing_for_year(service_id,
free_sms_fragment_limit, financial_year_start)
return jsonify(form), 201

View File

@@ -29,6 +29,7 @@ from app.models import (
ScheduledNotification,
ServiceEmailReplyTo,
Template,
TemplateHistory,
EMAIL_TYPE,
SMS_TYPE,
KEY_TYPE_NORMAL,
@@ -225,7 +226,7 @@ def get_notification_with_personalisation(service_id, notification_id, key_type)
if key_type:
filter_dict['key_type'] = key_type
return Notification.query.filter_by(**filter_dict).options(joinedload('template_history')).one()
return Notification.query.filter_by(**filter_dict).options(joinedload('template')).one()
@statsd(namespace="dao")
@@ -281,7 +282,7 @@ def get_notifications_for_service(
query = _filter_query(query, filter_dict)
if personalisation:
query = query.options(
joinedload('template_history')
joinedload('template')
)
return query.order_by(desc(Notification.created_at)).paginate(
@@ -305,7 +306,7 @@ def _filter_query(query, filter_dict=None):
# filter by template
template_types = multidict.getlist('template_type')
if template_types:
query = query.join(Template).filter(Template.template_type.in_(template_types))
query = query.join(TemplateHistory).filter(TemplateHistory.template_type.in_(template_types))
return query

View File

@@ -647,6 +647,19 @@ class TemplateHistory(db.Model):
nullable=False,
default=NORMAL)
template_redacted = db.relationship('TemplateRedacted', foreign_keys=[id],
primaryjoin='TemplateRedacted.template_id == TemplateHistory.id')
redact_personalisation = association_proxy('template_redacted', 'redact_personalisation')
def get_link(self):
return url_for(
"v2_template.get_template_by_id",
template_id=self.id,
version=self.version,
_external=True
)
def _as_utils_template(self):
return Template._as_utils_template(self)
@@ -911,9 +924,9 @@ class Notification(db.Model):
job_row_number = db.Column(db.Integer, nullable=True)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False)
service = db.relationship('Service')
template_id = db.Column(UUID(as_uuid=True), db.ForeignKey('templates.id'), index=True, unique=False)
template = db.relationship('Template')
template_id = db.Column(UUID(as_uuid=True), index=True, unique=False)
template_version = db.Column(db.Integer, nullable=False)
template = db.relationship('TemplateHistory')
api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), index=True, unique=False)
api_key = db.relationship('ApiKey')
key_type = db.Column(db.String, db.ForeignKey('key_types.name'), index=True, unique=False, nullable=False)
@@ -949,11 +962,6 @@ class Notification(db.Model):
client_reference = db.Column(db.String, index=True, nullable=True)
_personalisation = db.Column(db.String, nullable=True)
template_history = db.relationship('TemplateHistory', primaryjoin=and_(
foreign(template_id) == remote(TemplateHistory.id),
foreign(template_version) == remote(TemplateHistory.version)
))
scheduled_notification = db.relationship('ScheduledNotification', uselist=False)
client_reference = db.Column(db.String, index=True, nullable=True)
@@ -965,6 +973,14 @@ class Notification(db.Model):
created_by = db.relationship('User')
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True)
__table_args__ = (
db.ForeignKeyConstraint(
['template_id', 'template_version'],
['templates_history.id', 'templates_history.version'],
),
{}
)
@property
def personalisation(self):
if self._personalisation:
@@ -1167,8 +1183,7 @@ class NotificationHistory(db.Model, HistoryModel):
job_row_number = db.Column(db.Integer, nullable=True)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False)
service = db.relationship('Service')
template_id = db.Column(UUID(as_uuid=True), db.ForeignKey('templates.id'), index=True, unique=False)
template = db.relationship('Template')
template_id = db.Column(UUID(as_uuid=True), index=True, unique=False)
template_version = db.Column(db.Integer, nullable=False)
api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), index=True, unique=False)
api_key = db.relationship('ApiKey')
@@ -1198,6 +1213,14 @@ class NotificationHistory(db.Model, HistoryModel):
created_by = db.relationship('User')
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True)
__table_args__ = (
db.ForeignKeyConstraint(
['template_id', 'template_version'],
['templates_history.id', 'templates_history.version'],
),
{}
)
@classmethod
def from_original(cls, notification):
history = super().from_original(notification)

View File

@@ -471,7 +471,7 @@ class NotificationWithTemplateSchema(BaseSchema):
class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema):
template_history = fields.Nested(TemplateHistorySchema,
template_history = fields.Nested(TemplateHistorySchema, attribute="template",
only=['id', 'name', 'template_type', 'content', 'subject', 'version'],
dump_only=True)

View File

@@ -102,6 +102,7 @@ from app.schemas import (
detailed_service_schema
)
from app.utils import pagination_links
from app.billing.rest import update_free_sms_fragment_limit_data
service_blueprint = Blueprint('service', __name__)
@@ -203,6 +204,10 @@ def update_service(service_id):
if 'letter_contact_block' in req_json:
create_or_update_letter_contact(fetched_service.id, req_json['letter_contact_block'])
# bridging code between frontend is deployed and data has not been migrated yet. Can only update current year
if 'free_sms_fragment_limit' in req_json:
update_free_sms_fragment_limit_data(fetched_service.id, req_json['free_sms_fragment_limit'])
if service_going_live:
send_notification_to_service_users(
service_id=service_id,