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

Add intent to change date format #93

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,31 @@ def handle_time_format_change(self, message: Message):
self.speak_dialog("time_format_changed",
{"scale": str(new_setting)}, private=True)

@intent_handler(IntentBuilder("ChangeDate").require("change")
.require("date").one_of("mdy", "dmy", "ymd").build())
def handle_date_format_change(self, message: Message):
"""
Handle a request to set date format to DMY, MDY, or YMD format
:param message: Message associated with request
"""
new_setting = "YMD" if message.data.get("ymd") else \
"MDY" if message.data.get("mdy") else \
"DMY" if message.data.get("dmy") else None
if not new_setting:
raise RuntimeError("Missing required date format vocab")

current_setting = get_user_prefs(message)["units"]["date"]
if new_setting == current_setting:
self.speak_dialog("date_format_already_set",
{"format": message.data.get(new_setting.lower())},
private=True)
else:
updated_prefs = {"units": {"date": new_setting}}
self.update_profile(updated_prefs, message)
self.speak_dialog("date_format_changed",
{"format": message.data.get(new_setting.lower())},
private=True)

@intent_handler(IntentBuilder("SetHesitation").one_of("permit", "deny")
.require("hesitation").build())
def handle_speak_hesitation(self, message: Message):
Expand Down
1 change: 1 addition & 0 deletions locale/en-us/dialog/date_format_already_set.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Date format is already set to {{format}}.
1 change: 1 addition & 0 deletions locale/en-us/dialog/date_format_changed.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Date format changed to {{format}}.
1 change: 1 addition & 0 deletions locale/en-us/vocab/date.voc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
date
1 change: 1 addition & 0 deletions locale/en-us/vocab/dmy.voc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
day month year
1 change: 1 addition & 0 deletions locale/en-us/vocab/mdy.voc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
month day year
1 change: 1 addition & 0 deletions locale/en-us/vocab/ymd.voc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
year month day
7 changes: 7 additions & 0 deletions test/test_intents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ en-us:
- full
- change to military time:
- full
ChangeDate:
- use year month day date format:
- ymd
- change date format to month day year:
- mdy
- change to day month year date format:
- dmy
SetHesitation:
- enable hesitation:
- permit
Expand Down
7 changes: 7 additions & 0 deletions test/test_resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ vocab:
- 'timezone'
- 'units'
- 'username'
- 'date'
- 'dmy'
- 'mdy'
- 'ymd'

# dialog is .dialog file basenames (case-sensitive)
dialog:
Expand Down Expand Up @@ -132,6 +136,8 @@ dialog:
- 'word_dot'
- 'word_at'
- 'word_email_title'
- 'date_format_already_set'
- 'date_format_changed'
# regex entities, not necessarily filenames
regex:
- 'rx_language'
Expand All @@ -153,6 +159,7 @@ intents:
adapt:
- ChangeUnits
- ChangeTime
- ChangeDate
- SetHesitation
- Transcription
- SpeakSpeed
Expand Down
38 changes: 38 additions & 0 deletions test/test_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,44 @@ def test_handle_time_format_change(self):
self.assertEqual(
test_message.context["user_profiles"][0]["units"]["time"], 12)

def test_handle_date_format_change(self):
test_profile = self.user_config
test_profile["user"]["username"] = "test_user"
test_profile["units"]["date"] = "MDY"
test_message = Message("test", {"mdy": "month day year"},
{"username": "test_user",
"user_profiles": [test_profile]})
# MDY -> MDY
self.skill.handle_date_format_change(test_message)
self.skill.speak_dialog.assert_called_once_with(
"date_format_already_set", {"format": "month day year"},
private=True)
self.assertEqual(
test_message.context["user_profiles"][0]["units"]["date"], "MDY")
# MDY -> YMD
test_message.data = {"ymd": "year month day"}
self.skill.handle_date_format_change(test_message)
self.skill.speak_dialog.assert_called_with("date_format_changed",
{"format": "year month day"},
private=True)
self.assertEqual(
test_message.context["user_profiles"][0]["units"]["date"], "YMD")
# YMD -> YMD
self.skill.handle_date_format_change(test_message)
self.skill.speak_dialog.assert_called_with("date_format_already_set",
{"format": "year month day"},
private=True)
self.assertEqual(
test_message.context["user_profiles"][0]["units"]["date"], "YMD")
# YMD -> DMY
test_message.data = {"dmy": "day month year"}
self.skill.handle_date_format_change(test_message)
self.skill.speak_dialog.assert_called_with("date_format_changed",
{"format": "day month year"},
private=True)
self.assertEqual(
test_message.context["user_profiles"][0]["units"]["date"], "DMY")

def test_handle_speech_hesitation(self):
test_profile = self.user_config
test_profile["user"]["username"] = "test_user"
Expand Down