Skip to content

Commit

Permalink
Merge pull request #1901 from GSA/notify-admin-1877
Browse files Browse the repository at this point in the history
set timezone automatically
  • Loading branch information
heyitsmebev authored Aug 28, 2024
2 parents 8f15a83 + d756b22 commit b20c8ec
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 38 deletions.
4 changes: 4 additions & 0 deletions app/assets/js/setTimezone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
document.addEventListener('DOMContentLoaded', function() {
var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
document.cookie = `timezone=${timeZone}; path=/`;
})
6 changes: 6 additions & 0 deletions app/main/views/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from app.formatters import format_date_numeric, format_datetime_numeric, get_time_left
from app.main import main
from app.main.views.user_profile import set_timezone
from app.statistics_utils import get_formatted_percentage
from app.utils import (
DELIVERED_STATUSES,
Expand All @@ -39,6 +40,7 @@ def old_service_dashboard(service_id):
@main.route("/services/<uuid:service_id>")
@user_has_permissions()
def service_dashboard(service_id):

if session.get("invited_user_id"):
session.pop("invited_user_id", None)
session["service_id"] = service_id
Expand Down Expand Up @@ -402,11 +404,13 @@ def get_dashboard_partials(service_id):


def get_dashboard_totals(statistics):

for msg_type in statistics.values():
msg_type["failed_percentage"] = get_formatted_percentage(
msg_type["failed"], msg_type["requested"]
)
msg_type["show_warning"] = float(msg_type["failed_percentage"]) > 3

return statistics


Expand Down Expand Up @@ -465,6 +469,8 @@ def get_months_for_financial_year(year, time_format="%B"):


def get_current_month_for_financial_year(year):
# Setting the timezone here because we need to set it somewhere.
set_timezone()
current_month = datetime.now().month
return current_month

Expand Down
2 changes: 2 additions & 0 deletions app/main/views/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
SetSenderForm,
get_placeholder_form_instance,
)
from app.main.views.user_profile import set_timezone
from app.models.user import Users
from app.s3_client.s3_csv_client import (
get_csv_metadata,
Expand Down Expand Up @@ -1075,6 +1076,7 @@ def get_spreadsheet_column_headings_from_template(template):


def get_recipient():
set_timezone()
if {"recipient", "placeholders"} - set(session.keys()):
return None

Expand Down
13 changes: 13 additions & 0 deletions app/main/views/user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
TwoFactorForm,
)
from app.models.user import User
from app.utils import hilite
from app.utils.user import user_is_gov_user, user_is_logged_in
from notifications_utils.url_safe_token import check_token

Expand Down Expand Up @@ -275,3 +276,15 @@ def user_profile_disable_platform_admin_view():
return render_template(
"views/user-profile/disable-platform-admin-view.html", form=form
)


def set_timezone():
# Cookie is set in dashboard.html on page load
try:
timezone = request.cookies.get("timezone", "US/Eastern")
current_app.logger.debug(hilite(f"User's timezone is {timezone}"))
serialized_user = current_user.serialize()
if serialized_user["preferred_timezone"] is not timezone:
current_user.update(preferred_timezone=timezone)
except Exception:
current_app.logger.exception(hilite("Can't get timezone"))
1 change: 1 addition & 0 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, _dict):
super().__init__(_dict)
self.permissions = _dict.get("permissions", {})
self._platform_admin = _dict["platform_admin"]
self.preferred_timezone = _dict.get("preferred_timezone", "America/New_York")

@classmethod
def from_id(cls, user_id):
Expand Down
4 changes: 4 additions & 0 deletions app/templates/views/dashboard/dashboard.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


{% extends "withnav_template.html" %}

{% from "components/table.html" import list_table, field, text_field, link_field, right_aligned_field_heading, hidden_field_heading, row_heading, notification_status_field, notification_carrier_field, notification_carrier_message_field %}
Expand All @@ -8,6 +10,8 @@
{% endblock %}

{% block maincolumn_content %}
<script type="text/javascript" src="{{ asset_url('js/setTimezone.js') }}"></script>


<div class="dashboard margin-bottom-2">

Expand Down
1 change: 1 addition & 0 deletions app/templates/views/send.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% from "components/file-upload.html" import file_upload %}
{% from "components/table.html" import list_table, text_field, index_field, index_field_heading %}
{% from "components/components/back-link/macro.njk" import usaBackLink %}
<script type="text/javascript" src="{{ asset_url('js/setTimezone.js') }}"></script>

{% block service_page_title %}
Upload a list of {{ 999|recipient_count_label(template.template_type) }}
Expand Down
9 changes: 2 additions & 7 deletions app/templates/views/user-profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@ <h1 class="font-body-2xl margin-bottom-3">User profile</h1>
{% endcall %}

{% call row() %}
{{ text_field('Preferred Timezone') }}
{{ text_field('Preferred timezone') }}
{{ optional_text_field(current_user.preferred_timezone) }}
{{ edit_field(
'Change',
url_for('.user_profile_preferred_timezone'),
suffix='preferred timezone'
)
}}
{{ text_field('Set automatically') }}
{% endcall %}

{% if current_user.platform_admin or session.get('disable_platform_admin_view') %}
Expand Down
9 changes: 8 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ const copyGtmHead = () => {
.pipe(dest(paths.dist + 'js/'));
};

// Task to copy `setTimezone.js`
const copySetTimezone = () => {
return src(paths.src + 'js/setTimezone.js')
.pipe(dest(paths.dist + 'js/'));
};


// Task to copy images
const copyImages = () => {
return src(paths.src + 'images/**/*', { encoding: false })
Expand All @@ -122,4 +129,4 @@ const copyAssets = async () => {
await uswds.copyAssets();
};

exports.default = series(styles, javascripts, copyGtmHead, copyImages, copyAssets);
exports.default = series(styles, javascripts, copyGtmHead, copySetTimezone, copyImages, copyAssets);
1 change: 0 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions tests/app/main/views/test_conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,55 +113,55 @@ def test_view_conversation(
[
(
"message-8",
"yesterday at 14:59 US/Eastern",
"yesterday at 14:59 America/New_York",
),
(
"message-7",
"yesterday at 14:59 US/Eastern",
"yesterday at 14:59 America/New_York",
),
(
"message-6",
"yesterday at 16:59 US/Eastern",
"yesterday at 16:59 America/New_York",
),
(
"message-5",
"yesterday at 18:59 US/Eastern",
"yesterday at 18:59 America/New_York",
),
(
"message-4",
"yesterday at 20:59 US/Eastern",
"yesterday at 20:59 America/New_York",
),
(
"message-3",
"yesterday at 22:59 US/Eastern",
"yesterday at 22:59 America/New_York",
),
(
"message-2",
"yesterday at 22:59 US/Eastern",
"yesterday at 22:59 America/New_York",
),
(
"message-1",
"yesterday at 23:00 US/Eastern",
"yesterday at 23:00 America/New_York",
),
(
expected_outbound_content,
"yesterday at 00:00 US/Eastern",
"yesterday at 00:00 America/New_York",
),
(
expected_outbound_content,
"yesterday at 00:00 US/Eastern",
"yesterday at 00:00 America/New_York",
),
(
expected_outbound_content,
"yesterday at 00:00 US/Eastern",
"yesterday at 00:00 America/New_York",
),
(
expected_outbound_content,
"yesterday at 00:00 US/Eastern",
"yesterday at 00:00 America/New_York",
),
(
expected_outbound_content,
"yesterday at 00:00 US/Eastern",
"yesterday at 00:00 America/New_York",
),
]
):
Expand Down
19 changes: 9 additions & 10 deletions tests/app/main/views/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,14 @@ def test_download_inbox(
)
assert response.get_data(as_text=True) == (
"Phone number,Message,Received\r\n"
"(202) 867-5300,message-1,07-01-2016 11:00 US/Eastern\r\n"
"(202) 867-5300,message-2,07-01-2016 10:59 US/Eastern\r\n"
"(202) 867-5300,message-3,07-01-2016 10:59 US/Eastern\r\n"
"(202) 867-5302,message-4,07-01-2016 08:59 US/Eastern\r\n"
"+33(0)1 12345678,message-5,07-01-2016 06:59 US/Eastern\r\n"
"(202) 555-0104,message-6,07-01-2016 04:59 US/Eastern\r\n"
"(202) 555-0104,message-7,07-01-2016 02:59 US/Eastern\r\n"
"+68212345,message-8,07-01-2016 02:59 US/Eastern\r\n"
"(202) 867-5300,message-1,07-01-2016 11:00 America/New_York\r\n"
"(202) 867-5300,message-2,07-01-2016 10:59 America/New_York\r\n"
"(202) 867-5300,message-3,07-01-2016 10:59 America/New_York\r\n"
"(202) 867-5302,message-4,07-01-2016 08:59 America/New_York\r\n"
"+33(0)1 12345678,message-5,07-01-2016 06:59 America/New_York\r\n"
"(202) 555-0104,message-6,07-01-2016 04:59 America/New_York\r\n"
"(202) 555-0104,message-7,07-01-2016 02:59 America/New_York\r\n"
"+68212345,message-8,07-01-2016 02:59 America/New_York\r\n"
)


Expand Down Expand Up @@ -849,7 +849,7 @@ def test_should_show_upcoming_jobs_on_dashboard(
assert normalize_spaces(page.select_one("main h2").text) == ("In the next few days")

assert normalize_spaces(page.select_one("a.banner-dashboard").text) == (
"2 files waiting to send " "- sending starts today at 06:09 US/Eastern"
"2 files waiting to send " "- sending starts today at 06:09 America/New_York"
)

assert page.select_one("a.banner-dashboard")["href"] == url_for(
Expand Down Expand Up @@ -1786,7 +1786,6 @@ def test_org_breadcrumbs_show_if_user_is_platform_admin(

client_request.login(platform_admin_user, service_one_json)
page = client_request.get("main.service_dashboard", service_id=SERVICE_ONE_ID)

assert page.select_one(".navigation-organization-link")["href"] == url_for(
"main.organization_dashboard",
org_id=ORGANISATION_ID,
Expand Down
2 changes: 1 addition & 1 deletion tests/app/main/views/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def test_should_show_scheduled_job(
)

assert normalize_spaces(page.select("main div p")[1].text) == (
"Example template - service one was scheduled on January 02, 2016 at 12:00 AM US/Eastern by Test User"
"Example template - service one was scheduled on January 02, 2016 at 12:00 AM America/New_York by Test User"
)

assert page.select("main p a")[0]["href"] == url_for(
Expand Down
4 changes: 2 additions & 2 deletions tests/app/main/views/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def test_caseworker_sees_template_page_if_template_is_deleted(
)
assert (
page.select("p.hint")[0].text.strip()
== "This template was deleted today at 15:00 US/Eastern."
== "This template was deleted today at 15:00 America/New_York."
)

mock_get_deleted_template.assert_called_with(SERVICE_ONE_ID, template_id, None)
Expand Down Expand Up @@ -1591,7 +1591,7 @@ def test_should_show_page_for_a_deleted_template(
)
assert (
page.select("p.hint")[0].text.strip()
== "This template was deleted today at 15:00 US/Eastern."
== "This template was deleted today at 15:00 America/New_York."
)
assert "Delete this template" not in page.select_one("main").text

Expand Down
6 changes: 3 additions & 3 deletions tests/app/main/views/uploads/test_upload_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_get_upload_hub_page(

assert normalize_spaces(uploads[0].text.strip()) == (
"some.csv "
"Sent 1 January 2016 at 06:09 US/Eastern "
"Sent 1 January 2016 at 06:09 America/New_York "
"0 pending 8 delivered 2 failed"
)
assert uploads[0].select_one("a.file-list-filename-large")["href"] == (
Expand Down Expand Up @@ -82,12 +82,12 @@ def test_uploads_page_shows_scheduled_jobs(
("File Status"),
(
"even_later.csv "
"Sending 1 January 2016 at 18:09 US/Eastern "
"Sending 1 January 2016 at 18:09 America/New_York "
"1 text message waiting to send"
),
(
"send_me_later.csv "
"Sending 1 January 2016 at 06:09 US/Eastern "
"Sending 1 January 2016 at 06:09 America/New_York "
"1 text message waiting to send"
),
]
Expand Down
1 change: 1 addition & 0 deletions tests/app/models/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_user(notify_admin):
"email_address": "[email protected]",
"mobile_number": "+12021231234",
"state": "pending",
"preferred_timezone": "America/Chicago",
"failed_login_count": 0,
"platform_admin": False,
}
Expand Down

0 comments on commit b20c8ec

Please sign in to comment.