diff --git a/Dockerfile b/Dockerfile index 4c464fdf..d250560a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - # # Read more on Dockerfile best practices at the source: # https://docs.docker.com/develop/develop-images/dockerfile_best-practices -RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client nodejs +RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client nodejs npm # Inside the container, create an app directory and switch into it RUN mkdir /app diff --git a/parserator_web/static/js/index.js b/parserator_web/static/js/index.js index 492674cc..0dc5604f 100644 --- a/parserator_web/static/js/index.js +++ b/parserator_web/static/js/index.js @@ -1,2 +1,5 @@ /* TODO: Flesh this out to connect the form to the API and render results in the #address-results div. */ + +/*My code was not functioning correctly when I sperated it into this file, but it works when I wrote the js inline within the index.html */ + diff --git a/parserator_web/templates/parserator_web/index.html b/parserator_web/templates/parserator_web/index.html index a72d9c80..e75d5027 100644 --- a/parserator_web/templates/parserator_web/index.html +++ b/parserator_web/templates/parserator_web/index.html @@ -11,13 +11,12 @@

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 +37,58 @@

Parsing results

{% endblock %} {% block extra_js %} - + + + {% endblock %} diff --git a/parserator_web/urls.py b/parserator_web/urls.py index ce5f2b97..45d95c44 100644 --- a/parserator_web/urls.py +++ b/parserator_web/urls.py @@ -1,5 +1,4 @@ from django.urls import path - from parserator_web import views urlpatterns = [ diff --git a/parserator_web/views.py b/parserator_web/views.py index 0be3f4a9..cb33bd74 100644 --- a/parserator_web/views.py +++ b/parserator_web/views.py @@ -1,24 +1,38 @@ import usaddress from django.views.generic import TemplateView +from rest_framework import status from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.renderers import JSONRenderer -from rest_framework.exceptions import ParseError class Home(TemplateView): - template_name = 'parserator_web/index.html' + template_name = "parserator_web/index.html" 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({}) + def post(self, request, *args, **kwargs): + address = request.data.get("address") + try: + parsed_address, address_type = usaddress.tag(address) + return Response( + { + "status": "success", + "parsed": parsed_address, + "address_type": address_type, + }, + status=status.HTTP_200_OK, + ) + except usaddress.RepeatedLabelError as e: + return Response( + {"status": "error", "message": str(e)}, + status=status.HTTP_400_BAD_REQUEST, + ) - 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 + def get(self, request, *args, **kwargs): + return Response( + {"message": "Send a POST request with an address to parse."}, + status=status.HTTP_200_OK, + ) diff --git a/requirements.txt b/requirements.txt index dfa10939..beb25e4c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,5 @@ whitenoise==5.2.0 djangorestframework==3.11.1 usaddress==0.5.10 six +django-json-response==1.1.5 +django-urls==1.1.3 \ No newline at end of file diff --git a/tests/test_views.py b/tests/test_views.py index bfd5d0b7..93db5a9a 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,15 +1,27 @@ import pytest +from django.urls import reverse +from rest_framework.test import APIClient -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. - address_string = '123 main st chicago il' - pytest.fail() +@pytest.mark.django_db +def test_parse_address_success(): + client = APIClient() + response = client.post( + reverse("address-parse"), + {"address": "123 Main St. Suite 100 Chicago, IL"}, + format="json", + ) + assert response.status_code == 200 + assert response.data["status"] == "success" + assert "AddressNumber" in response.data["parsed"] -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. - address_string = '123 main st chicago il 123 main st' - pytest.fail() +@pytest.mark.django_db +def test_parse_address_failure(): + client = APIClient() + response = client.post( + reverse("address-parse"), {"address": "123 main st chicago il 123 main st"}, + format="json" + ) + assert response.status_code == 400 + assert response.data["status"] == "error"