From bd42bded0ac65fd5dfc235a0809aa819fb9c82e4 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Mon, 25 Apr 2022 13:05:26 +0100 Subject: [PATCH 1/4] Delete unused Marshmallow schema validation functions --- app/schemas.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/app/schemas.py b/app/schemas.py index 7b9e40144..095230e54 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -1,4 +1,4 @@ -from datetime import date, datetime, timedelta +from datetime import datetime, timedelta from uuid import UUID from flask_marshmallow.fields import fields @@ -40,16 +40,6 @@ def _validate_datetime_not_more_than_96_hours_in_future(dte, msg="Date cannot be raise ValidationError(msg) -def _validate_not_in_past(dte, msg="Date cannot be in the past"): - if dte < date.today(): - raise ValidationError(msg) - - -def _validate_datetime_not_in_future(dte, msg="Date cannot be in the future"): - if dte > datetime.utcnow(): - raise ValidationError(msg) - - def _validate_datetime_not_in_past(dte, msg="Date cannot be in the past"): if dte < datetime.utcnow(): raise ValidationError(msg) From ab199b6b059e8bfbbe91649baab35f886323e152 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Thu, 5 May 2022 11:37:46 +0100 Subject: [PATCH 2/4] Remove unused excluded fields from Marshmallow schemas We have a lot of cases in the schemas where we're excluding a field that doesn't actually exist on the schema anyway. This is often because a model has been deleted, and the schema has not been updated. These excluded fields have no effect at the moment, but Marshmallow 3 does raise an error if you try and exclude non-existent fields. There should be no change to what gets (de)serialized after this change. --- app/schemas.py | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/app/schemas.py b/app/schemas.py index 095230e54..c094d099a 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -105,8 +105,6 @@ class UserSchema(BaseSchema): "created_at", "email_access_validated_at", "updated_at", - "user_to_organisation", - "user_to_service", "verify_codes", ) strict = True @@ -147,7 +145,6 @@ class UserUpdateAttributeSchema(BaseSchema): 'platform_admin', 'state', 'updated_at', - 'user_to_service', 'verify_codes', ) strict = True @@ -198,7 +195,6 @@ class ProviderDetailsSchema(BaseSchema): class Meta(BaseSchema.Meta): model = models.ProviderDetails - exclude = ("provider_stats",) strict = True @@ -207,7 +203,6 @@ class ProviderDetailsHistorySchema(BaseSchema): class Meta(BaseSchema.Meta): model = models.ProviderDetailsHistory - exclude = ("provider_stats",) strict = True @@ -257,15 +252,11 @@ class ServiceSchema(BaseSchema, UUIDsAsStringsMixin): 'jobs', 'letter_contacts', 'letter_logo_filename', - 'old_id', 'reply_to_email_addresses', 'returned_letters', 'service_broadcast_provider_restriction', 'service_broadcast_settings', - 'service_notification_stats', - 'service_provider_stats', 'service_sms_senders', - 'template_statistics', 'templates', 'updated_at', 'users', @@ -317,20 +308,12 @@ class DetailedServiceSchema(BaseSchema): 'inbound_number', 'inbound_sms', 'jobs', - 'letter_contact_block', - 'letter_logo_filename', 'message_limit', - 'monthly_billing', 'permissions', 'rate_limit', - 'reply_to_email_address', 'reply_to_email_addresses', 'returned_letters', - 'service_notification_stats', - 'service_provider_stats', 'service_sms_senders', - 'sms_sender', - 'template_statistics', 'templates', 'users', 'version', @@ -358,7 +341,7 @@ class BaseTemplateSchema(BaseSchema): class Meta(BaseSchema.Meta): model = models.Template - exclude = ("service_id", "jobs", "service_letter_contact_id", "broadcast_messages") + exclude = ("service_id", "jobs", "service_letter_contact_id") strict = True @@ -520,7 +503,7 @@ class NotificationWithTemplateSchema(BaseSchema): class Meta(BaseSchema.Meta): model = models.Notification strict = True - exclude = ('_personalisation', 'scheduled_notification') + exclude = ('_personalisation',) template = fields.Nested( TemplateSchema, @@ -583,6 +566,9 @@ class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema): 'service', 'template_history', ) + # Overwrite the `NotificationWithTemplateSchema` base class to not exclude `_personalisation`, which + # isn't a defined field for this class + exclude = () @pre_dump def handle_personalisation_property(self, in_data): From 685959de0002cf6a3d2dfc36cd0baafa25a4b439 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Fri, 6 May 2022 09:19:16 +0100 Subject: [PATCH 3/4] Don't serialize broadcast_messages in TemplateHistorySchema In 0282a76 we excluded serializing `broadcast_messages` on some schemas. This also excludes it from the TemplateHistory schema. --- app/schemas.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/schemas.py b/app/schemas.py index c094d099a..79fccf604 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -407,6 +407,7 @@ class TemplateHistorySchema(BaseSchema): class Meta(BaseSchema.Meta): model = models.TemplateHistory + exclude = ('broadcast_messages',) class ApiKeySchema(BaseSchema): From dd213b8d5543230bf47a9de1c8c9eb0cb4a5cb6a Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Fri, 6 May 2022 09:49:31 +0100 Subject: [PATCH 4/4] Ensure UserUpdatePasswordSchema only loads _password We had the `only` defined in the Meta class, and this wasn't working - any extra fields were also being loaded. This moves it to the point where the class is instantiated, which now works. --- app/schemas.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/schemas.py b/app/schemas.py index 79fccf604..f19946eb3 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -180,7 +180,6 @@ class UserUpdatePasswordSchema(BaseSchema): class Meta(BaseSchema.Meta): model = models.User - only = ('password') strict = True @validates_schema(pass_original=True) @@ -716,7 +715,7 @@ class UnarchivedTemplateSchema(BaseSchema): # should not be used on its own for dumping - only for loading create_user_schema = UserSchema() user_update_schema_load_json = UserUpdateAttributeSchema(load_json=True, partial=True) -user_update_password_schema_load_json = UserUpdatePasswordSchema(load_json=True, partial=True) +user_update_password_schema_load_json = UserUpdatePasswordSchema(only=('_password',), load_json=True, partial=True) service_schema = ServiceSchema() detailed_service_schema = DetailedServiceSchema() template_schema = TemplateSchema()