From 556aaf487230a217055ea21f6edb8a912f0e10db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Dom=C3=ADnguez=20Molina?= Date: Tue, 16 Jul 2024 16:51:39 -0500 Subject: [PATCH 01/17] Editing dockerfile to ensure npm is installed correctly --- Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 4c464fdf..06d207ea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,11 @@ FROM python:3.8 # Add the NodeSource PPA # (see: https://github.com/nodesource/distributions/blob/master/README.md) -RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - +RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - && \ + apt-get update && \ + apt-get install -y --no-install-recommends postgresql-client nodejs && \ + apt-get install -y npm && \ + node -v && npm -v # Install any additional OS-level packages you need via apt-get. RUN statements # add additional layers to your image, increasing its final size. Keep your From 1c8b01706e6a58556f72196eceb0d9f98d19ca4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Dom=C3=ADnguez=20Molina?= Date: Tue, 16 Jul 2024 17:10:26 -0500 Subject: [PATCH 02/17] Editing dockerfile to ensure npm is installed correctly --- Dockerfile | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 06d207ea..d250560a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,11 +8,7 @@ FROM python:3.8 # Add the NodeSource PPA # (see: https://github.com/nodesource/distributions/blob/master/README.md) -RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - && \ - apt-get update && \ - apt-get install -y --no-install-recommends postgresql-client nodejs && \ - apt-get install -y npm && \ - node -v && npm -v +RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - # Install any additional OS-level packages you need via apt-get. RUN statements # add additional layers to your image, increasing its final size. Keep your @@ -23,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 From c7af93f9e06a6f5b38d1b16a1e876db186948319 Mon Sep 17 00:00:00 2001 From: FedericoDM Date: Wed, 17 Jul 2024 19:41:38 +0000 Subject: [PATCH 03/17] Completing parse function --- parserator_web/views.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/parserator_web/views.py b/parserator_web/views.py index 0be3f4a9..0e43dc21 100644 --- a/parserator_web/views.py +++ b/parserator_web/views.py @@ -6,6 +6,7 @@ from rest_framework.exceptions import ParseError + class Home(TemplateView): template_name = 'parserator_web/index.html' @@ -14,11 +15,43 @@ class AddressParse(APIView): renderer_classes = [JSONRenderer] def get(self, request): + """ + Extracts the address from the get 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 = request.GET("address", "") + parsed_address, address_type = self.parse(address) + + return Response({"input_string": address, + "address_components": parsed_address, + "address_type": address_type }) def parse(self, address): + """ + Uses the usaddress package to parse the address + """ # TODO: Implement this method to return the parsed components of a # given address using usaddress: https://github.com/datamade/usaddress + + if not address: + raise ParseError("Address can not be empty") + + try: + parsed_address = usaddress.parse(address) + address_type = usaddress.tag(address) + + except Exception as error: + print(f"Error parsing address: {error}") + return {}, "ERROR" + + address_components = {} + + for element in parsed_address: + address_components[element[1]] = element[0] + + address_type = address_type[-1] + + return address_components, address_type From d88e72e26974a944fffe2d8c952ff102db7c0ae2 Mon Sep 17 00:00:00 2001 From: FedericoDM Date: Wed, 17 Jul 2024 21:56:38 +0000 Subject: [PATCH 04/17] Adding javascript --- parserator_web/static/js/index.js | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/parserator_web/static/js/index.js b/parserator_web/static/js/index.js index 492674cc..06f22e66 100644 --- a/parserator_web/static/js/index.js +++ b/parserator_web/static/js/index.js @@ -1,2 +1,62 @@ /* TODO: Flesh this out to connect the form to the API and render results in the #address-results div. */ + + document.addEventListener("DOMContentLoaded", function () { + const form = document.getElementById("address-form"); + + form.addEventListener("submit", async function (e) { + e.preventDefault(); + + const address = document.getElementById("address").value; + const url = "/api/parse/"; + const queryParams = new URLSearchParams({ address: address }); + + try { + const response = await fetch(`${url}?${queryParams}`, { + method: 'GET' + }); + + const respData = await response.json(); + + if (!response.ok) { + throw new Error(respData.detail || 'An error occurred'); + } + + displayResults(respData); + } catch (error) { + displayError(error.message); + } + }); + + function displayResults(data) { + document.getElementById("error-results").style.display = "none"; + const resultsDiv = document.getElementById("address-results"); + resultsDiv.style.display = "block"; + + document.getElementById("parse-type").textContent = data.address_type; + + const tableBody = document.querySelector("#address-results-table tbody"); + tableBody.innerHTML = ''; + + for (const [key, value] of Object.entries(data.address_components)) { + const row = document.createElement("tr"); + const partCell = document.createElement("td"); + const tagCell = document.createElement("td"); + + partCell.textContent = key; + tagCell.textContent = value; + + row.appendChild(partCell); + row.appendChild(tagCell); + tableBody.appendChild(row); + } + } + + function displayError(errorMessage) { + document.getElementById("address-results").style.display = "none"; + const errorDiv = document.getElementById("error-results"); + errorDiv.style.display = "block"; + document.getElementById("parse-error").textContent = errorMessage; + } + }); + \ No newline at end of file From fb167097f4211e5fa8b415152801c166f46adaab Mon Sep 17 00:00:00 2001 From: FedericoDM Date: Wed, 17 Jul 2024 22:05:28 +0000 Subject: [PATCH 05/17] Debugging code --- parserator_web/static/js/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/parserator_web/static/js/index.js b/parserator_web/static/js/index.js index 06f22e66..589a0280 100644 --- a/parserator_web/static/js/index.js +++ b/parserator_web/static/js/index.js @@ -1,12 +1,14 @@ /* TODO: Flesh this out to connect the form to the API and render results in the #address-results div. */ +// Adding event listeners document.addEventListener("DOMContentLoaded", function () { const form = document.getElementById("address-form"); form.addEventListener("submit", async function (e) { e.preventDefault(); - + + // Declaring variables const address = document.getElementById("address").value; const url = "/api/parse/"; const queryParams = new URLSearchParams({ address: address }); @@ -16,10 +18,10 @@ method: 'GET' }); - const respData = await response.json(); + const content = await response.json(); if (!response.ok) { - throw new Error(respData.detail || 'An error occurred'); + throw new Error(content.detail || 'An error occured during execution'); } displayResults(respData); From b4d1957ab5cc5017da2d380e35935cf14a1aa427 Mon Sep 17 00:00:00 2001 From: FedericoDM Date: Wed, 17 Jul 2024 22:20:00 +0000 Subject: [PATCH 06/17] Added necessary ids --- parserator_web/templates/parserator_web/index.html | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/parserator_web/templates/parserator_web/index.html b/parserator_web/templates/parserator_web/index.html index a72d9c80..43bf31ad 100644 --- a/parserator_web/templates/parserator_web/index.html +++ b/parserator_web/templates/parserator_web/index.html @@ -11,7 +11,7 @@

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 %} @@ -21,7 +21,7 @@

U.S. addres