Task 02 - Refactor files
Introduction
The views.py file defines app routes, endpoints, and views. The file is divided into the following sections:
- Import statements
- Back-end API endpoints
- Front-end API endpoints
The Dockerfile file defines the Docker image to use and provides instructions for copying app components to the Docker container when you build the container.
The requirements.txt file specifies the packages that the app requires to run.
Description
In the previous task, you added a copy of views.py to both the FrontEnd and Backend folders. You added the Dockerfile file and the requirements.txt file to the FrontEnd folder. In this task, you’ll modify all three files to remove references to back-end functionality. You’ll also modify the copy of views.py in the Backend folder to remove references to front-end functionality.
The key steps are as follows:
- Open views.py from the FrontEnd folder in Visual Studio Code and remove the Backend API endpoints section.
- Remove the dblayer reference from the views.py Imports section.
- Remove the code from views.py that checks to see if the database is set up.
- Update Dockerfile to remove the code that installs the MSSQL ODBC driver.
- Update requirements.txt to remove references to the pyodbc and psycopg2-binary libraries.
- Open views.py from the BackEnd folder in Visual Studio Code and remove the Frontend API endpoints section.
Success Criteria
- You’ve updated views.py, Dockerfile, and requirements.txt files in the FrontEnd folder and removed back-end code and references to data libraries.
- You’ve updated views.py in the BackEnd folder and removed front-end code.
Solution
Expand this section to view the solution
In this task, you’ll refactor the front-end views.py file in the UpdatedApp\FrontEnd folder.
-
In Visual Studio Code, select Ctrl+Shift+E to open the Explorer pane.
-
In the Explorer pane, select Open Folder.
-
In the Open Folder dialog, select the Downloads folder, select UpdatedApp, and the select Select Folder.
-
In the Do you trust the authors of the files in this folder? dialog, select Trust the authors of all files in the parent folder ‘Downloads’ and then select Yes, I trust the authors.
-
In the Explorer pane, expand the frontend folder and then expand the contoso_hotel folder.
-
Select views.py. The file displays in the right side of the Visual Studio Code window.
-
Delete all code between the following region markers in the code (at or around lines 9 - 304):
#region -------- BACKEND API ENDPOINTS -------- #endregion -------- BACKEND API ENDPOINTS --------
-
Locate the line of code that imports dblayer (at or around line 4).
-
Remove dblayer,.
Don’t forget to delete the comma after dblayer.
-
Locate the code that checks to see if the database is set up (at or around lines 22-26):
-
Delete the code that performs the database check.
-
Save and close the file.
-
In the Visual Studio Code Explorer pane, select Dockerfile. The file displays in the right side of the Visual Studio Code window.
-
Locate the code that installs the MSSQL ODBC driver (at or around lines 17-23):
-
Delete the code that installs the MSSQL ODBC driver.
-
Save and close the file.
-
In the Visual Studio Code Explorer pane, select requirements.txt. The file displays in the right side of the Visual Studio Code window.
-
Delete the pyodbc and psycopg2-binary libraries.
-
Save and close the file.
-
In the Explorer pane, expand the backend folder and then expand the contoso_hotel folder.
-
Select views.py. The file displays in the right side of the Visual Studio Code window.
-
Delete all code between the following region markers in the code (around lines 309 - 332):
#region -------- FRONTEND API ENDPOINTS -------- #endregion -------- FRONTEND API ENDPOINTS --------
-
Save and close the file.
-
Leave Visual Studio Code open. You’ll run additional commands in the next task.