Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate variables in CSV do not exceed daily limit #278

Merged
merged 13 commits into from
Mar 11, 2024

Conversation

whabanks
Copy link
Contributor

@whabanks whabanks commented Feb 26, 2024

Summary | Résumé

This PR adds validation to the RecipientCSV class to ensure that variables inserted do not exceed the 612 character limit, when sending SMS. A new property, rows_with_combined_variable_content_too_long, was added to check that the total message length does not exceed 612 when both the variable content and template content is combined.

whabanks and others added 6 commits February 26, 2024 13:10
- Added has_message_too_long property to the Row class
- Added message_to_long variable to the Cell class with accompanying property content_length_error to identify a cell with the aformentioned error
- Added a check to the RecipientCSV class that checks if a variable in a CSV exceeds the message limit of 612 when added to the template content
- Raise an execption when length is exceeded
- Bubble exception message up to the UI for rendering
- Removed message_too_long_error from the Cell class
@whabanks whabanks marked this pull request as ready for review March 4, 2024 19:08
if variable:
variable_length += sum(1 if char in SanitiseSMS.ALLOWED_CHARACTERS else 2 for char in str(variable))
total_length = variable_length + (len(self.template.content) if self.template else 0)
if total_length > 612:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content size might change again in the future if we restrict to even less than 4 fragments. Would it be easy to make this a variable to be configured?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see we have the SMS_CHAR_COUNT_LIMIT constant, is that something we could leverage here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, we should use that here for sure.

rows_too_long = []
if self.rows_as_list:
for row in self.rows_as_list:
if row.personalisation and self.template and self.template.placeholders:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to check for all 3 vs row.personalisation?

Copy link
Contributor Author

@whabanks whabanks Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

row.personalisation includes the variable for the phone number from the CSV, causing those characters to be counted toward the limit. Calling as_dict_with_keys(template.placeholders) ensures we're only ever counting the variables input by the user.

We could probably get away without dropping the placeholder check. In some areas of the code, template is not always passed in to new instances of RecipientCSV.

total_length = variable_length + (len(self.template.content) if self.template else 0)
if total_length > SMS_CHAR_COUNT_LIMIT:
rows_too_long.append(row)
return (row for row in rows_too_long)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we doing this?

Copy link
Contributor Author

@whabanks whabanks Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention is to return a generator rather than a list to remain consistent with the other properties that return rows meeting certain criteria. E.g. rows_with_missing_data.

That said this should be refactored to yield the rows instead of storing them, as that's the whole point of using generators.🤦🏻‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, let me know if this makes more sense!

@@ -276,6 +276,25 @@ def rows_with_missing_data(self):
def rows_with_message_too_long(self):
return self._filter_rows("message_too_long")

@property
def rows_with_combined_variable_content_too_long(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this function being called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whabanks and others added 2 commits March 7, 2024 11:12
- Updated rows_with_combined_variable_content_too_long make use of generators properly
- Removed None check
@whabanks whabanks requested a review from jzbahrai March 7, 2024 18:02
@whabanks whabanks merged commit 7385bbc into main Mar 11, 2024
4 checks passed
@whabanks whabanks deleted the fix/enforce-sms-csv-limits branch March 11, 2024 14:51
whabanks added a commit that referenced this pull request Apr 26, 2024
whabanks added a commit that referenced this pull request Apr 26, 2024
whabanks added a commit that referenced this pull request May 3, 2024
whabanks added a commit that referenced this pull request May 21, 2024
* Reinstate PR #278 which added additional length checks to bulk SMS sends (#293)"

This reverts commit 0146718.

* Reinstate "Exclude content length errors from recipient_error prop (#290)" (#292)"

This reverts commit cd287cf.

* Remove unused parameter

* Fix params

* Bump utils version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants