From 380eec25913f32d72ae44d676c54b8fea03f8270 Mon Sep 17 00:00:00 2001 From: bilalq99 <133537558+bilalq99@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:11:20 -0500 Subject: [PATCH 1/3] views.py --- parserator_web/views.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/parserator_web/views.py b/parserator_web/views.py index 0be3f4a9..a0f029f2 100644 --- a/parserator_web/views.py +++ b/parserator_web/views.py @@ -14,11 +14,24 @@ class AddressParse(APIView): renderer_classes = [JSONRenderer] def get(self, request): - # TODO: Flesh out this method to parse an address string using the - # parse() method and return the parsed components to the frontend. - return Response({}) + address_string = request.query_params.get('address', None) + if not address_string: + raise ParseError(detail="No address provided") + + address_components, address_type = self.parse(address_string) + + if address_components is None: + raise ParseError(detail="Invalid address format") + + return Response({ + "input_string": address_string, + "address_components": address_components, + "address_type": address_type + }) def parse(self, address): - # TODO: Implement this method to return the parsed components of a - # given address using usaddress: https://github.com/datamade/usaddress - return address_components, address_type + try: + parsed_address, address_type = usaddress.tag(address) + return parsed_address, address_type + except usaddress.RepeatedLabelError: + return None, None From 2e5b03ae19a5fbaf29263afdf2e081a6b5fb9009 Mon Sep 17 00:00:00 2001 From: bilalq99 <133537558+bilalq99@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:12:59 -0500 Subject: [PATCH 2/3] index.html --- .../templates/parserator_web/index.html | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/parserator_web/templates/parserator_web/index.html b/parserator_web/templates/parserator_web/index.html index a72d9c80..0dcf86e1 100644 --- a/parserator_web/templates/parserator_web/index.html +++ b/parserator_web/templates/parserator_web/index.html @@ -11,13 +11,13 @@

U.S. addres

Dealing with some messy or unstructured addresses? We can parse them for you.

Try it out! Parse an address in the United States into fields like AddressNumber, StreetName and ZipCode.

-
+ {% csrf_token %}
- + @@ -38,5 +38,43 @@

Parsing results

{% endblock %} {% block extra_js %} - + {% endblock %} From a1df5b56d496f5b8a8a3a1e037a3fdf33e996cc1 Mon Sep 17 00:00:00 2001 From: bilalq99 <133537558+bilalq99@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:14:11 -0500 Subject: [PATCH 3/3] test_views.py --- tests/test_views.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/test_views.py b/tests/test_views.py index bfd5d0b7..7aedeeb3 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,15 +1,27 @@ import pytest +from django.urls import reverse - +@pytest.mark.django_db def test_api_parse_succeeds(client): - # TODO: Finish this test. Send a request to the API and confirm that the - # data comes back in the appropriate format. + # This test verifies that the API successfully parses a valid address address_string = '123 main st chicago il' - pytest.fail() - + response = client.get(reverse('parse_address'), {'address': address_string}) + + assert response.status_code == 200 + data = response.json() + assert 'input_string' in data + assert data['input_string'] == address_string + assert 'address_components' in data + assert 'address_type' in data + assert len(data['address_components']) > 0 # Ensure some components are parsed +@pytest.mark.django_db def test_api_parse_raises_error(client): - # TODO: Finish this test. The address_string below will raise a - # RepeatedLabelError, so ParseAddress.parse() will not be able to parse it. + # This test verifies that the API returns an error for an invalid address address_string = '123 main st chicago il 123 main st' - pytest.fail() + response = client.get(reverse('parse_address'), {'address': address_string}) + + assert response.status_code == 400 + data = response.json() + assert 'error' in data + assert data['error'] == 'Invalid address format'