-
-
Notifications
You must be signed in to change notification settings - Fork 9
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 polling station intent #33
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
brooklinevoiceapp/mycity/intents/polling_stations_intent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
""" Intent for responding to polling station requests """ | ||
import logging | ||
from mycity.mycity_response_data_model import MyCityResponseDataModel | ||
from mycity.intents import intent_constants | ||
from mycity.utils.address_utils import set_address_in_session | ||
from mycity.utils.brookline_arcgis_api_utils import get_polling_locations_json | ||
|
||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
# User facing strings | ||
CARD_TITLE_POLLING = "Polling Locations" | ||
OUTPUT_SPEECH_TEMPLATE = "The {} polling station in {} is located at {}. " | ||
|
||
# Strings used in parsing json data returned by server | ||
FEATURES_PATH = "features" | ||
ATTRIBUTES_PATH = "attributes" | ||
NAME_PATH = "NAME" | ||
PRECINCT_PATH = "PRECINCT" | ||
ADDR_PATH = "FULLADD" | ||
|
||
# Request data model strings | ||
NUMBER_LOCATIONS_SLOT_NAME = "number_requests" | ||
MAX_LOCATIONS = 10 | ||
DEFAULT_LOCATIONS = 3 | ||
|
||
def get_polling_location_info(mycity_request): | ||
""" | ||
Generates a response to a polling location request | ||
|
||
:param mycity_request: MyCityRequestDataModel containing the user request | ||
:return: MyCityResponseDataModel containing the speech to return to the user | ||
""" | ||
LOGGER.debug('Getting polling location information') | ||
|
||
response = MyCityResponseDataModel() | ||
set_address_in_session(mycity_request) | ||
current_address = \ | ||
mycity_request.session_attributes.get(intent_constants.CURRENT_ADDRESS_KEY) | ||
if current_address is None: | ||
# Delegate to the Alexa interaction model for getting the user address | ||
LOGGER.debug('Requesting user address.') | ||
response.dialog_directive = "Delegate" | ||
else: | ||
response.output_speech = _get_output_speech_for_address(current_address, mycity_request) | ||
response.card_title = CARD_TITLE_POLLING | ||
response.should_end_session = True | ||
|
||
return response | ||
|
||
|
||
def _get_output_speech_for_address(address, mycity_request): | ||
""" | ||
Creates output speech for polling locations near the provided address | ||
|
||
:param address: Current address | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
:return: Output speech string | ||
""" | ||
number_locations = _number_of_locations(mycity_request) | ||
response = get_polling_locations_json(address) | ||
try: | ||
results = response[FEATURES_PATH] | ||
output_speech = "" | ||
for result in results[:number_locations]: | ||
output_speech += _build_speech_from_result(result) | ||
except (IndexError, KeyError): | ||
LOGGER.error("Error extracting polling station response.") | ||
return intent_constants.NO_RESULTS_RESPONSE | ||
|
||
if not output_speech: | ||
return intent_constants.NO_RESULTS_RESPONSE | ||
|
||
return output_speech | ||
|
||
|
||
def _number_of_locations(mycity_request): | ||
""" | ||
Returns number of locations from the request if available or a default value | ||
:param mycity_request: MyCityRequestDataModel object | ||
:return: Number of polling station requests to return from this intent | ||
""" | ||
if NUMBER_LOCATIONS_SLOT_NAME in \ | ||
mycity_request.intent_variables and \ | ||
"value" in mycity_request.intent_variables[ | ||
NUMBER_LOCATIONS_SLOT_NAME]: | ||
return min( | ||
int(mycity_request.intent_variables[NUMBER_LOCATIONS_SLOT_NAME]["value"]), | ||
MAX_LOCATIONS) | ||
|
||
return DEFAULT_LOCATIONS | ||
|
||
|
||
def _build_speech_from_result(result): | ||
""" | ||
Builds a speech string from a given polling location result | ||
:param result: JSON object of a single polling location result | ||
:return: Speech string representing this result | ||
""" | ||
|
||
try: | ||
attributes = result[ATTRIBUTES_PATH] | ||
name = attributes[NAME_PATH] | ||
precinct = attributes[PRECINCT_PATH] | ||
address = attributes[ADDR_PATH] | ||
except KeyError: | ||
LOGGER.error("Polling station response json did not contain the expected attributes.") | ||
raise KeyError | ||
|
||
return OUTPUT_SPEECH_TEMPLATE.format(name, precinct, address) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
brooklinevoiceapp/mycity/test/integration_tests/test_polling_stations_intent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" Integration tests for PollingStationIntent """ | ||
import mycity.test.test_constants as test_constants | ||
import mycity.test.integration_tests.intent_base_case as base_case | ||
import mycity.test.integration_tests.intent_test_mixins as mix_ins | ||
import mycity.intents.polling_stations_intent as polling_intent | ||
import mycity.intents.intent_constants as intent_constants | ||
import copy | ||
|
||
FEATURES=polling_intent.FEATURES_PATH | ||
ATTRIBUTES=polling_intent.ATTRIBUTES_PATH | ||
NAME=polling_intent.NAME_PATH | ||
|
||
class PollingStationsTestCase(mix_ins.RepromptTextTestMixIn, | ||
mix_ins.CardTitleTestMixIn, | ||
base_case.IntentBaseCase): | ||
|
||
intent_to_test = "PollingStationIntent" | ||
expected_title = polling_intent.CARD_TITLE_POLLING | ||
returns_reprompt_text = False | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
# Patch requests.get in PollingStationIntent | ||
self.mock_requests(get_data=copy.deepcopy(test_constants.GET_ADDRESS_CANDIDATES_API_MOCK), | ||
post_data=copy.deepcopy(test_constants.GET_POLLING_LOCATIONS_API_MOCK)) | ||
|
||
def test_response_contains_polling_first_station_name(self): | ||
first_station_name = test_constants.GET_POLLING_LOCATIONS_API_MOCK[FEATURES][0][ATTRIBUTES][NAME] | ||
response = self.controller.on_intent(self.request) | ||
self.assertTrue(first_station_name in response.output_speech) | ||
|
||
def test_no_feature_results(self): | ||
self.mock_requests(get_data=copy.deepcopy(test_constants.GET_ADDRESS_CANDIDATES_API_MOCK), | ||
post_data=copy.deepcopy(test_constants.NO_RESPONSE_POLLING_LOCATIONS_API_MOCK)) | ||
response = self.controller.on_intent(self.request) | ||
self.assertEqual(response.output_speech, intent_constants.NO_RESULTS_RESPONSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could alternatively use auto delegation