Merge pull request #1732 from alphagov/ken-hidden-in-json-response

Return `is_precompiled_letter` field as part of json for notification by id
This commit is contained in:
kentsanggds
2018-03-08 15:06:10 +00:00
committed by GitHub
17 changed files with 184 additions and 48 deletions

View File

@@ -149,7 +149,6 @@ class Config(object):
CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID = 'eb4d9930-87ab-4aef-9bce-786762687884'
SERVICE_NOW_LIVE_TEMPLATE_ID = '618185c6-3636-49cd-b7d2-6f6f5eb3bdde'
ORGANISATION_INVITATION_EMAIL_TEMPLATE_ID = '203566f0-d835-47c5-aa06-932439c86573'
PRECOMPILED_TEMPLATE_NAME = 'Pre-compiled PDF'
BROKER_URL = 'sqs://'
BROKER_TRANSPORT_OPTIONS = {

View File

@@ -83,6 +83,7 @@ def dao_get_template_usage(service_id, limit_days=None):
Template.id.label('template_id'),
Template.name,
Template.template_type,
Template.is_precompiled_letter,
notifications_aggregate_query.c.count
).join(
notifications_aggregate_query,

View File

@@ -522,6 +522,7 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
stat.month = result.month
stat.year = result.year
stat.count = result.count
stat.is_precompiled_letter = result.is_precompiled_letter
stats.append(stat)
month = get_london_month_from_utc_column(Notification.created_at)
@@ -533,6 +534,7 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
if fy_start < datetime.now() < fy_end:
today_results = db.session.query(
Notification.template_id,
Template.is_precompiled_letter,
Template.name,
Template.template_type,
extract('month', month).label('month'),
@@ -547,6 +549,7 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
Notification.key_type != KEY_TYPE_TEST
).group_by(
Notification.template_id,
Template.hidden,
Template.name,
Template.template_type,
month,
@@ -571,6 +574,7 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
new_stat.month = int(today_result.month)
new_stat.year = int(today_result.year)
new_stat.count = today_result.count
new_stat.is_precompiled_letter = today_result.is_precompiled_letter
stats.append(new_stat)
return stats

View File

@@ -37,6 +37,7 @@ def dao_get_template_usage_stats_by_service(service_id, year):
StatsTemplateUsageByMonth.template_id,
Template.name,
Template.template_type,
Template.is_precompiled_letter,
StatsTemplateUsageByMonth.month,
StatsTemplateUsageByMonth.year,
StatsTemplateUsageByMonth.count

View File

@@ -5,7 +5,11 @@ from sqlalchemy import asc, desc
from sqlalchemy.sql.expression import bindparam
from app import db
from app.models import (Template, TemplateHistory, TemplateRedacted)
from app.models import (
Template,
TemplateHistory,
TemplateRedacted
)
from app.dao.dao_utils import (
transactional,
version_class
@@ -135,6 +139,7 @@ def dao_get_templates_for_cache(cache):
query = db.session.query(Template.id.label('template_id'),
Template.template_type,
Template.name,
Template.is_precompiled_letter,
cache_subq.c.count.label('count')
).join(cache_subq,
Template.id == cache_subq.c.template_id

View File

@@ -76,7 +76,3 @@ def get_letter_pdf(notification):
file_content = obj.get()["Body"].read()
return file_content
def is_precompiled_letter(template):
return template.hidden and template.name == current_app.config['PRECOMPILED_TEMPLATE_NAME']

View File

@@ -6,6 +6,7 @@ from flask import url_for, current_app
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.dialects.postgresql import (
UUID,
JSON
@@ -641,6 +642,9 @@ class TemplateProcessTypes(db.Model):
name = db.Column(db.String(255), primary_key=True)
PRECOMPILED_TEMPLATE_NAME = 'Pre-compiled PDF'
class TemplateBase(db.Model):
__abstract__ = True
@@ -718,6 +722,14 @@ class TemplateBase(db.Model):
else:
return None
@hybrid_property
def is_precompiled_letter(self):
return self.hidden and self.name == PRECOMPILED_TEMPLATE_NAME and self.template_type == LETTER_TYPE
@is_precompiled_letter.setter
def is_precompiled_letter(self, value):
pass
def _as_utils_template(self):
if self.template_type == EMAIL_TYPE:
return PlainTextEmailTemplate(

View File

@@ -454,7 +454,16 @@ class NotificationWithTemplateSchema(BaseSchema):
template = fields.Nested(
TemplateSchema,
only=['id', 'version', 'name', 'template_type', 'content', 'subject', 'redact_personalisation'],
only=[
'id',
'version',
'name',
'template_type',
'content',
'subject',
'redact_personalisation',
'is_precompiled_letter'
],
dump_only=True
)
job = fields.Nested(JobSchema, only=["id", "original_file_name"], dump_only=True)

View File

@@ -536,7 +536,8 @@ def get_monthly_template_usage(service_id):
'type': i.template_type,
'month': i.month,
'year': i.year,
'count': i.count
'count': i.count,
'is_precompiled_letter': i.is_precompiled_letter
}
)

View File

@@ -20,7 +20,7 @@ from app.dao.templates_dao import (
dao_get_template_by_id)
from notifications_utils.template import SMSMessageTemplate
from app.dao.services_dao import dao_fetch_service_by_id
from app.letters.utils import get_letter_pdf, is_precompiled_letter
from app.letters.utils import get_letter_pdf
from app.models import SMS_TYPE
from app.notifications.validators import service_has_permission, check_reply_to
from app.schemas import (template_schema, template_history_schema)
@@ -196,7 +196,7 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
template = dao_get_template_by_id(notification.template_id)
if is_precompiled_letter(template):
if template.is_precompiled_letter:
try:

View File

@@ -47,7 +47,8 @@ def get_template_statistics_for_service_by_day(service_id):
'count': data.count,
'template_id': str(data.template_id),
'template_name': data.name,
'template_type': data.template_type
'template_type': data.template_type,
'is_precompiled_letter': data.is_precompiled_letter
}
return jsonify(data=[serialize(row) for row in stats])