The update of SQLAlchemy 1.4.10 has caused some conflicts in our code. This PR fixes most of those conflicts.

- sqlalchemy.sql.expression.case must include an else statement.
- clearly define list of columns for inbound_sms_history insert, getting the list from InboundSmsHistory.__table__.c was causing data type errors.
- remove relationships when not needed, the foreign key relationship is established in the creation of the column. This will get rid of the warnings referenced here: http://sqlalche.me/e/14/qzyx.
- update queries now that he user relationship in ServiceUser db model has been removed.
- move the check that a template is archived to the view instead of the dao method. The check was clearing the session before the version history could be done.

Deleting notifications in the night tasks still needs to be
investigated. The raw sql is causing an error.
This commit is contained in:
Rebecca Law
2021-04-26 11:50:17 +01:00
parent e1a626855d
commit 1b070d69a1
8 changed files with 24 additions and 50 deletions

View File

@@ -150,9 +150,12 @@ def fetch_letter_line_items_for_all_services(start_date, end_date):
[(FactBilling.postage.in_(INTERNATIONAL_POSTAGE_TYPES), "international")], else_=FactBilling.postage [(FactBilling.postage.in_(INTERNATIONAL_POSTAGE_TYPES), "international")], else_=FactBilling.postage
).label("postage") ).label("postage")
postage_order = case(((formatted_postage == "second", 1), postage_order = case(
(formatted_postage == "first", 2), (formatted_postage == "second", 1),
(formatted_postage == "international", 3))) (formatted_postage == "first", 2),
(formatted_postage == "international", 3),
else_=0 # assumes never get 0 as a result
)
query = db.session.query( query = db.session.query(
Organisation.name.label("organisation_name"), Organisation.name.label("organisation_name"),

View File

@@ -71,7 +71,13 @@ def dao_count_inbound_sms_for_service(service_id, limit_days):
def _insert_inbound_sms_history(subquery, query_limit=10000): def _insert_inbound_sms_history(subquery, query_limit=10000):
offset = 0 offset = 0
inbound_sms_query = db.session.query( inbound_sms_query = db.session.query(
*[x.name for x in InboundSmsHistory.__table__.c] InboundSms.id,
InboundSms.created_at,
InboundSms.service_id,
InboundSms.notify_number,
InboundSms.provider_date,
InboundSms.provider_reference,
InboundSms.provider
).filter(InboundSms.id.in_(subquery)) ).filter(InboundSms.id.in_(subquery))
inbound_sms_count = inbound_sms_query.count() inbound_sms_count = inbound_sms_query.count()

View File

@@ -9,9 +9,13 @@ def dao_get_service_user(user_id, service_id):
def dao_get_active_service_users(service_id): def dao_get_active_service_users(service_id):
query = ServiceUser.query.join(ServiceUser.user).filter( query = db.session.query(
ServiceUser.service_id == service_id, ServiceUser
User.state == 'active' ).join(
User, User.id == ServiceUser.user_id
).filter(
User.state == 'active',
ServiceUser.service_id == service_id
) )
return query.all() return query.all()

View File

@@ -43,9 +43,6 @@ def dao_create_template(template):
VersionOptions(Template, history_class=TemplateHistory) VersionOptions(Template, history_class=TemplateHistory)
) )
def dao_update_template(template): def dao_update_template(template):
if template.archived:
template.folder = None
db.session.add(template) db.session.add(template)

View File

@@ -199,8 +199,6 @@ class ServiceUser(db.Model):
UniqueConstraint('user_id', 'service_id', name='uix_user_to_service'), UniqueConstraint('user_id', 'service_id', name='uix_user_to_service'),
) )
user = db.relationship('User')
user_to_organisation = db.Table( user_to_organisation = db.Table(
'user_to_organisation', 'user_to_organisation',
@@ -671,7 +669,6 @@ class ServicePermission(db.Model):
primary_key=True, index=True, nullable=False) primary_key=True, index=True, nullable=False)
permission = db.Column(db.String(255), db.ForeignKey('service_permission_types.name'), permission = db.Column(db.String(255), db.ForeignKey('service_permission_types.name'),
index=True, primary_key=True, nullable=False) index=True, primary_key=True, nullable=False)
service = db.relationship("Service")
created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False)
service_permission_types = db.relationship( service_permission_types = db.relationship(

View File

@@ -145,7 +145,8 @@ def update_template(service_id, template_id):
raise InvalidRequest(errors, status_code=400) raise InvalidRequest(errors, status_code=400)
update_dict = template_schema.load(updated_template).data update_dict = template_schema.load(updated_template).data
if update_dict.archived:
update_dict.folder = None
dao_update_template(update_dict) dao_update_template(update_dict)
return jsonify(data=template_schema.dump(update_dict).data), 200 return jsonify(data=template_schema.dump(update_dict).data), 200

View File

@@ -405,7 +405,7 @@ def set_permissions(user_id, service_id):
# TODO fix security hole, how do we verify that the user # TODO fix security hole, how do we verify that the user
# who is making this request has permission to make the request. # who is making this request has permission to make the request.
service_user = dao_get_service_user(user_id, service_id) service_user = dao_get_service_user(user_id, service_id)
user = service_user.user user = get_user_by_id(user_id)
service = dao_fetch_service_by_id(service_id=service_id) service = dao_fetch_service_by_id(service_id=service_id)
data = request.get_json() data = request.get_json()

View File

@@ -13,12 +13,7 @@ from app.dao.templates_dao import (
dao_update_template, dao_update_template,
dao_update_template_reply_to, dao_update_template_reply_to,
) )
from app.models import ( from app.models import Template, TemplateHistory, TemplateRedacted
Template,
TemplateFolder,
TemplateHistory,
TemplateRedacted,
)
from tests.app.db import create_letter_contact, create_template from tests.app.db import create_letter_contact, create_template
@@ -95,35 +90,6 @@ def test_update_template(sample_service, sample_user):
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'new name' assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'new name'
def test_update_template_in_a_folder_to_archived(sample_service, sample_user):
template_data = {
'name': 'Sample Template',
'template_type': "sms",
'content': "Template content",
'service': sample_service,
'created_by': sample_user
}
template = Template(**template_data)
template_folder_data = {
'name': 'My Folder',
'service_id': sample_service.id,
}
template_folder = TemplateFolder(**template_folder_data)
template.folder = template_folder
dao_create_template(template)
template.archived = True
dao_update_template(template)
template_folder = TemplateFolder.query.one()
archived_template = Template.query.one()
assert template_folder
assert not archived_template.folder
def test_dao_update_template_reply_to_none_to_some(sample_service, sample_user): def test_dao_update_template_reply_to_none_to_some(sample_service, sample_user):
letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA') letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA')