create, edit and use email branding instead of organisation

notable things that have been kept until migration is complete:

* passing in `organisation` to update_service will update email branding
* both `/email-branding` and `/organisation` hit the same code
* service endpoints still return organisation as well as email branding
This commit is contained in:
Leo Hemsted
2018-02-05 12:02:35 +00:00
parent cea52929d3
commit 2f79da8702
18 changed files with 281 additions and 279 deletions

View File

@@ -96,7 +96,7 @@ def register_blueprint(application):
from app.template_statistics.rest import template_statistics as template_statistics_blueprint
from app.events.rest import events as events_blueprint
from app.provider_details.rest import provider_details as provider_details_blueprint
from app.organisation.rest import organisation_blueprint
from app.email_branding.rest import email_branding_blueprint
from app.dvla_organisation.rest import dvla_organisation_blueprint
from app.delivery.rest import delivery_blueprint
from app.inbound_number.rest import inbound_number_blueprint
@@ -159,8 +159,10 @@ def register_blueprint(application):
provider_details_blueprint.before_request(requires_admin_auth)
application.register_blueprint(provider_details_blueprint, url_prefix='/provider-details')
organisation_blueprint.before_request(requires_admin_auth)
application.register_blueprint(organisation_blueprint, url_prefix='/organisation')
email_branding_blueprint.before_request(requires_admin_auth)
application.register_blueprint(email_branding_blueprint, url_prefix='/email-branding')
# TODO: remove this route after admin is updated to refer to email branding
application.register_blueprint(email_branding_blueprint, url_prefix='/organisation')
dvla_organisation_blueprint.before_request(requires_admin_auth)
application.register_blueprint(dvla_organisation_blueprint, url_prefix='/dvla_organisations')

View File

@@ -0,0 +1,23 @@
from app import db
from app.dao.dao_utils import transactional
from app.models import EmailBranding
def dao_get_email_branding_options():
return EmailBranding.query.all()
def dao_get_email_branding_by_id(email_branding_id):
return EmailBranding.query.filter_by(id=email_branding_id).one()
@transactional
def dao_create_email_branding(email_branding):
db.session.add(email_branding)
@transactional
def dao_update_email_branding(email_branding, **kwargs):
for key, value in kwargs.items():
setattr(email_branding, key, value)
db.session.add(email_branding)

View File

@@ -1,23 +0,0 @@
from app import db
from app.dao.dao_utils import transactional
from app.models import Organisation
def dao_get_organisations():
return Organisation.query.all()
def dao_get_organisation_by_id(org_id):
return Organisation.query.filter_by(id=org_id).one()
@transactional
def dao_create_organisation(organisation):
db.session.add(organisation)
@transactional
def dao_update_organisation(organisation, **kwargs):
for key, value in kwargs.items():
setattr(organisation, key, value)
db.session.add(organisation)

View File

@@ -195,17 +195,17 @@ def get_logo_url(base_url, logo_file):
def get_html_email_options(service):
govuk_banner = service.branding not in (BRANDING_ORG, BRANDING_ORG_BANNER)
brand_banner = service.branding == BRANDING_ORG_BANNER
if service.organisation and service.branding != BRANDING_GOVUK:
if service.branding != BRANDING_GOVUK and service.email_branding:
logo_url = get_logo_url(
current_app.config['ADMIN_BASE_URL'],
service.organisation.logo
) if service.organisation.logo else None
service.email_branding.logo
) if service.email_branding.logo else None
branding = {
'brand_colour': service.organisation.colour,
'brand_colour': service.email_branding.colour,
'brand_logo': logo_url,
'brand_name': service.organisation.name,
'brand_name': service.email_branding.name,
}
else:
branding = {}

View File

@@ -1,6 +1,6 @@
post_create_organisation_schema = {
post_create_email_branding_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST schema for getting organisation",
"description": "POST schema for getting email_branding",
"type": "object",
"properties": {
"colour": {"type": ["string", "null"]},
@@ -10,9 +10,9 @@ post_create_organisation_schema = {
"required": []
}
post_update_organisation_schema = {
post_update_email_branding_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST schema for getting organisation",
"description": "POST schema for getting email_branding",
"type": "object",
"properties": {
"colour": {"type": ["string", "null"]},

View File

@@ -0,0 +1,54 @@
from flask import Blueprint, jsonify, request
from app.dao.email_branding_dao import (
dao_create_email_branding,
dao_get_email_branding_options,
dao_get_email_branding_by_id,
dao_update_email_branding
)
from app.errors import register_errors
from app.models import EmailBranding
from app.email_branding.email_branding_schema import (
post_create_email_branding_schema,
post_update_email_branding_schema
)
from app.schema_validation import validate
email_branding_blueprint = Blueprint('email_branding', __name__)
register_errors(email_branding_blueprint)
@email_branding_blueprint.route('', methods=['GET'])
def get_email_branding_options():
email_branding_options = [o.serialize() for o in dao_get_email_branding_options()]
return jsonify(email_branding=email_branding_options)
@email_branding_blueprint.route('/<uuid:email_branding_id>', methods=['GET'])
def get_email_branding_by_id(email_branding_id):
email_branding = dao_get_email_branding_by_id(email_branding_id)
return jsonify(email_branding=email_branding.serialize())
@email_branding_blueprint.route('', methods=['POST'])
def create_email_branding():
data = request.get_json()
validate(data, post_create_email_branding_schema)
email_branding = EmailBranding(**data)
dao_create_email_branding(email_branding)
return jsonify(data=email_branding.serialize()), 201
@email_branding_blueprint.route('/<uuid:email_branding_id>', methods=['POST'])
def update_email_branding(email_branding_id):
data = request.get_json()
validate(data, post_update_email_branding_schema)
fetched_email_branding = dao_get_email_branding_by_id(email_branding_id)
dao_update_email_branding(fetched_email_branding, **data)
return jsonify(data=fetched_email_branding.serialize()), 200

View File

@@ -148,6 +148,7 @@ class BrandingTypes(db.Model):
name = db.Column(db.String(255), primary_key=True)
# TODO: remove this model after admin is updated to refer to email branding
class Organisation(db.Model):
__tablename__ = 'organisation'
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)

View File

@@ -1,51 +0,0 @@
from flask import Blueprint, jsonify, request
from app.dao.organisations_dao import (
dao_create_organisation,
dao_get_organisations,
dao_get_organisation_by_id,
dao_update_organisation
)
from app.errors import register_errors
from app.models import Organisation
from app.organisation.organisation_schema import post_create_organisation_schema, post_update_organisation_schema
from app.schema_validation import validate
organisation_blueprint = Blueprint('organisation', __name__)
register_errors(organisation_blueprint)
@organisation_blueprint.route('', methods=['GET'])
def get_organisations():
organisations = [o.serialize() for o in dao_get_organisations()]
return jsonify(organisations=organisations)
@organisation_blueprint.route('/<uuid:org_id>', methods=['GET'])
def get_organisation_by_id(org_id):
organisation = dao_get_organisation_by_id(org_id)
return jsonify(organisation=organisation.serialize())
@organisation_blueprint.route('', methods=['POST'])
def create_organisation():
data = request.get_json()
validate(data, post_create_organisation_schema)
organisation = Organisation(**data)
dao_create_organisation(organisation)
return jsonify(data=organisation.serialize()), 201
@organisation_blueprint.route('/<uuid:organisation_id>', methods=['POST'])
def update_organisation(organisation_id):
data = request.get_json()
validate(data, post_update_organisation_schema)
fetched_organisation = dao_get_organisation_by_id(organisation_id)
dao_update_organisation(fetched_organisation, **data)
return jsonify(data=fetched_organisation.serialize()), 200

View File

@@ -199,11 +199,12 @@ class ProviderDetailsHistorySchema(BaseSchema):
class ServiceSchema(BaseSchema):
created_by = field_for(models.Service, 'created_by', required=True)
organisation = field_for(models.Service, 'organisation')
organisation_type = field_for(models.Service, 'organisation_type')
branding = field_for(models.Service, 'branding')
dvla_organisation = field_for(models.Service, 'dvla_organisation')
permissions = fields.Method("service_permissions")
# TODO: remove this variable after admin is updated to refer to email branding
organisation = field_for(models.Service, 'organisation')
email_branding = field_for(models.Service, 'email_branding')
override_flag = False
reply_to_email_address = fields.Method(method_name="get_reply_to_email_address")
@@ -284,11 +285,13 @@ class DetailedServiceSchema(BaseSchema):
'template_statistics',
'service_provider_stats',
'service_notification_stats',
# TODO: remove this field after admin is updated to refer to email branding
'organisation',
'email_branding',
'service_sms_senders',
'monthly_billing',
'reply_to_email_addresses',
'letter_contact_block', # new exclude from here
'letter_contact_block',
'message_limit',
'email_from',
'inbound_api',

View File

@@ -184,10 +184,15 @@ def update_service(service_id):
if org_type:
service.crown = org_type == 'central'
# TODO: remove this block after admin is updated to refer to email branding
if 'organisation' in req_json:
org_id = req_json['organisation']
service.email_branding = None if not org_id else EmailBranding.query.get(org_id)
if 'email_branding' in req_json:
email_branding_id = req_json['email_branding']
service.email_branding = None if not email_branding_id else EmailBranding.query.get(email_branding_id)
dao_update_service(service)
if service_going_live: