Skip to content

Commit

Permalink
Merge branch 'main' into chore/python-3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels authored Dec 4, 2024
2 parents 0d9dab0 + 10033ac commit 783630d
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 526 deletions.
2 changes: 1 addition & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def get_locale():
application.jinja_env.globals["show_tou_prompt"] = show_tou_prompt
application.jinja_env.globals["parse_ua"] = parse
application.jinja_env.globals["events_key"] = EVENTS_KEY
application.jinja_env.globals["now"] = datetime.now
application.jinja_env.globals["now"] = datetime.utcnow

# Initialize Salesforce Account list
if application.config["FF_SALESFORCE_CONTACT"]:
Expand Down
40 changes: 6 additions & 34 deletions app/main/views/send.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import itertools
import json
from datetime import datetime, timezone
from string import ascii_uppercase
from zipfile import BadZipFile

Expand Down Expand Up @@ -54,7 +53,6 @@
)
from app.main.views.dashboard import aggregate_notifications_stats
from app.models.user import Users
from app.notify_client.notification_counts_client import notification_counts_client
from app.s3_client.s3_csv_client import (
copy_bulk_send_file_to_uploads,
list_bulk_send_uploads,
Expand Down Expand Up @@ -651,8 +649,8 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_

sms_fragments_sent_today = daily_sms_fragment_count(service_id)
emails_sent_today = daily_email_count(service_id)
remaining_sms_message_fragments_today = current_service.sms_daily_limit - sms_fragments_sent_today
remaining_email_messages_today = current_service.message_limit - emails_sent_today
remaining_sms_message_fragments = current_service.sms_daily_limit - sms_fragments_sent_today
remaining_email_messages = current_service.message_limit - emails_sent_today

contents = s3download(service_id, upload_id)

Expand All @@ -661,7 +659,7 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
email_reply_to = None
sms_sender = None
recipients_remaining_messages = (
remaining_email_messages_today if db_template["template_type"] == "email" else remaining_sms_message_fragments_today
remaining_email_messages if db_template["template_type"] == "email" else remaining_sms_message_fragments
)

if db_template["template_type"] == "email":
Expand Down Expand Up @@ -745,8 +743,8 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_
original_file_name=request.args.get("original_file_name", ""),
upload_id=upload_id,
form=CsvUploadForm(),
remaining_messages=remaining_email_messages_today,
remaining_sms_message_fragments=remaining_sms_message_fragments_today,
remaining_messages=remaining_email_messages,
remaining_sms_message_fragments=remaining_sms_message_fragments,
sms_parts_to_send=sms_parts_to_send,
is_sms_parts_estimated=is_sms_parts_estimated,
choose_time_form=choose_time_form,
Expand Down Expand Up @@ -785,29 +783,7 @@ def check_messages(service_id, template_id, upload_id, row_index=2):
data["original_file_name"] = SanitiseASCII.encode(data.get("original_file_name", ""))
data["sms_parts_requested"] = data["stats_daily"]["sms"]["requested"]
data["sms_parts_remaining"] = current_service.sms_daily_limit - daily_sms_fragment_count(service_id)

if current_app.config["FF_ANNUAL_LIMIT"]:
# Override the remaining messages counts with the remaining annual counts, if the latter are lower
stats_ytd = notification_counts_client.get_all_notification_counts_for_year(service_id, datetime.now(timezone.utc).year)
remaining_sms_this_year = current_service.sms_annual_limit - stats_ytd["sms"]
remaining_email_this_year = current_service.email_annual_limit - stats_ytd["email"]

# Show annual limit validation over the daily one (even if both are true)
data["send_exceeds_annual_limit"] = False
data["send_exceeds_daily_limit"] = False
if data["template"].template_type == "email":
if remaining_email_this_year < data["count_of_recipients"]:
data["recipients_remaining_messages"] = remaining_email_this_year
data["send_exceeds_annual_limit"] = True
else:
if remaining_sms_this_year < data["count_of_recipients"]:
data["recipients_remaining_messages"] = remaining_sms_this_year
data["send_exceeds_annual_limit"] = True
else:
data["send_exceeds_daily_limit"] = data["recipients"].sms_fragment_count > data["sms_parts_remaining"]

else:
data["send_exceeds_daily_limit"] = data["recipients"].sms_fragment_count > data["sms_parts_remaining"]
data["send_exceeds_daily_limit"] = data["recipients"].sms_fragment_count > data["sms_parts_remaining"]

if (
data["recipients"].too_many_rows
Expand All @@ -828,10 +804,6 @@ def check_messages(service_id, template_id, upload_id, row_index=2):
if data["send_exceeds_daily_limit"]:
return render_template("views/check/column-errors.html", **data)

if current_app.config["FF_ANNUAL_LIMIT"]:
if data["send_exceeds_annual_limit"]:
return render_template("views/check/column-errors.html", **data)

metadata_kwargs = {
"notification_count": data["count_of_recipients"],
"template_id": str(template_id),
Expand Down
75 changes: 0 additions & 75 deletions app/notify_client/notification_counts_client.py

This file was deleted.

46 changes: 0 additions & 46 deletions app/notify_client/template_statistics_api_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from itertools import groupby

from app.notify_client import NotifyAdminAPIClient
from app.utils import DELIVERED_STATUSES, FAILURE_STATUSES


class TemplateStatisticsApiClient(NotifyAdminAPIClient):
Expand All @@ -20,46 +17,3 @@ def get_template_statistics_for_template(self, service_id, template_id):


template_statistics_client = TemplateStatisticsApiClient()


class TemplateStatistics:
def __init__(self, stats):
self.stats = stats

def as_aggregates(self):
template_statistics = self._filter_out_cancelled_stats(self.stats)
notifications = {
template_type: {status: 0 for status in ("requested", "delivered", "failed")} for template_type in ["sms", "email"]
}
for stat in template_statistics:
notifications[stat["template_type"]]["requested"] += stat["count"]
if stat["status"] in DELIVERED_STATUSES:
notifications[stat["template_type"]]["delivered"] += stat["count"]
elif stat["status"] in FAILURE_STATUSES:
notifications[stat["template_type"]]["failed"] += stat["count"]

return notifications

def as_template_usage(self, sort_key="count"):
template_statistics = self._filter_out_cancelled_stats(self.stats)
templates = []
for k, v in groupby(
sorted(template_statistics, key=lambda x: x["template_id"]),
key=lambda x: x["template_id"],
):
template_stats = list(v)

templates.append(
{
"template_id": k,
"template_name": template_stats[0]["template_name"],
"template_type": template_stats[0]["template_type"],
"is_precompiled_letter": template_stats[0]["is_precompiled_letter"],
"count": sum(s["count"] for s in template_stats),
}
)

return sorted(templates, key=lambda x: x[sort_key], reverse=True)

def _filter_out_cancelled_stats(self, template_statistics):
return [s for s in template_statistics if s["status"] != "cancelled"]
22 changes: 1 addition & 21 deletions app/templates/partials/check/too-many-email-messages.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
{% from "components/banner.html" import banner_wrapper %}
{% from "components/links.html" import content_link %}

<p>
{%- if current_service.trial_mode %}
{{ _("Your service is in trial mode. To send more messages, <a href='{}'>request to go live</a>").format(url_for('main.request_to_go_live', service_id=current_service.id)) }}
{% else %}
{% if send_exceeds_annual_limit %}

<p data-testid="exceeds-annual">{{ _('<strong>{}</strong> can only send <strong>{}</strong> more email messages until annual limit resets'.format(current_service.name, recipients_remaining_messages)) }}</p>
<p>
{{ _('To send some of these messages now, edit the spreadsheet to <strong>{}</strong> recipients maximum. '.format(recipients_remaining_messages)) }}
{{ _('To send to recipients you removed, wait until <strong>April 1, {}</strong> or contact them some other way.'.format(now().year)) }}
</p>

{% elif send_exceeds_daily_limit or recipients.more_rows_than_can_send %}
{% call banner_wrapper(type='dangerous') %}
{{ _("To request a daily limit above {} emails, {}").format(current_service.message_limit, content_link(_("contact us"), url_for('main.contact'), is_external_link=true)) }}
{% endcall %}

<h2 class="heading-medium">{{ _('You cannot send all these email messages today') }}</h2>
<p data-testid="exceeds-daily">
{{ _("You can try sending these messages after {} Eastern Time. Check {}.").format(time_to_reset[current_lang],
content_link(_("your current local time"), _('https://nrc.canada.ca/en/web-clock/'), is_external_link=true))}}
</p>
{% endif %}

{{ _("To request a daily limit above {} emails, {}").format(current_service.message_limit, content_link(_("contact us"), url_for('main.contact'), is_external_link=true)) }}
{%- endif -%}
</p>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% from "components/links.html" import content_link %}

<p data-testid="exceeds-daily">
<p>
{%- if current_service.trial_mode %}
{{ _("Your service is in trial mode. To send more messages, <a href='{}'>request to go live</a>").format(url_for('main.request_to_go_live', service_id=current_service.id)) }}
{% else %}
Expand Down
19 changes: 14 additions & 5 deletions app/templates/views/check/column-errors.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
{% set prefix_txt = _('a column called') %}
{% set prefix_plural_txt = _('columns called') %}

{% if send_exceeds_annual_limit %}
{% set page_title = _('These messages exceed the annual limit') %}
{% elif send_exceeds_daily_limit and (sms_parts_remaining <= 0) %}
{% if send_exceeds_daily_limit and (sms_parts_remaining <= 0) %}
{% set page_title = _('These messages exceed your daily limit') %}
{% elif send_exceeds_daily_limit or recipients.more_rows_than_can_send %}
{% set page_title = _('These messages exceed your daily limit') %}
Expand Down Expand Up @@ -170,9 +168,20 @@ <h2 class="heading-medium">{{ _('You cannot send all these text messages today')
{% call banner_wrapper(type='dangerous') %}
{% include "partials/check/too-many-email-messages.html" %}
{% endcall %}
{% elif recipients.more_rows_than_can_send or send_exceeds_annual_limit %}
{% include "partials/check/too-many-email-messages.html" %}
{% elif recipients.more_rows_than_can_send %}
{% call banner_wrapper(type='dangerous') %}
{% include "partials/check/too-many-email-messages.html" %}
{% endcall %}
<h2 class="heading-medium">{{ _('You cannot send all these email messages today') }}</h2>
<p>
{{ _("You can try sending these messages after {} Eastern Time. Check {}.").format(time_to_reset[current_lang],
content_link(_("your current local time"), _('https://nrc.canada.ca/en/web-clock/'), is_external_link=true))}}
</p>


{% endif %}


</div>

{% if not send_exceeds_daily_limit %}
Expand Down
6 changes: 1 addition & 5 deletions app/translations/csv/fr.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2027,8 +2027,4 @@
"Annual usage","Utilisation annuelle"
"resets at 7pm Eastern Time","Réinitialisation à 19 h, heure de l’Est"
"Visit usage report","Consulter le rapport d’utilisation"
"Month by month totals","Totaux mensuels"
"<strong>{}</strong> can only send <strong>{}</strong> more email messages until annual limit resets","FR: <strong>{}</strong> can only send <strong>{}</strong> more email messages until annual limit resets"
"To send some of these messages now, edit the spreadsheet to <strong>{}</strong> recipients maximum. ","FR: To send some of these messages now, edit the spreadsheet to <strong>{}</strong> recipients maximum. "
"To send to recipients you removed, wait until <strong>April 1, {}</strong> or contact them some other way.","FR: To send to recipients you removed, wait until <strong>April 1, {}</strong> or contact them some other way"
"These messages exceed the annual limit","FR: These messages exceed the annual limit"
"Month by month totals","Totaux mensuels"
1 change: 0 additions & 1 deletion gunicorn_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
# Start timer for total running time
start_time = time.time()


def on_starting(server):
server.log.info("Starting Notifications Admin")

Expand Down
Loading

0 comments on commit 783630d

Please sign in to comment.