Skip to main content Link Menu Expand (external link) Document Search Copy Copied

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:

  1. Open views.py from the FrontEnd folder in Visual Studio Code and remove the Backend API endpoints section.
  2. Remove the dblayer reference from the views.py Imports section.
  3. Remove the code from views.py that checks to see if the database is set up.
  4. Update Dockerfile to remove the code that installs the MSSQL ODBC driver.
  5. Update requirements.txt to remove references to the pyodbc and psycopg2-binary libraries.
  6. 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.

  1. In Visual Studio Code, select Ctrl+Shift+E to open the Explorer pane.

  2. In the Explorer pane, select Open Folder.

  3. In the Open Folder dialog, select the Downloads folder, select UpdatedApp, and the select Select Folder.

  4. 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.

    yq54qnra.png

  5. In the Explorer pane, expand the frontend folder and then expand the contoso_hotel folder.

  6. Select views.py. The file displays in the right side of the Visual Studio Code window.

  7. 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 -------- 
    

    qhkyv58o.png

  8. Locate the line of code that imports dblayer (at or around line 4).

    c16t85vh.png

  9. Remove dblayer,.

    1pqgxt9o.png

    Don’t forget to delete the comma after dblayer.

  10. Locate the code that checks to see if the database is set up (at or around lines 22-26):

    7xe8gkmv.png

  11. Delete the code that performs the database check.

    l338dtbt.png

  12. Save and close the file.

  13. In the Visual Studio Code Explorer pane, select Dockerfile. The file displays in the right side of the Visual Studio Code window.

    bndhhefz.png

  14. Locate the code that installs the MSSQL ODBC driver (at or around lines 17-23):

    zwgu2ey9.png

  15. Delete the code that installs the MSSQL ODBC driver.

    l673ddid.png

  16. Save and close the file.

  17. In the Visual Studio Code Explorer pane, select requirements.txt. The file displays in the right side of the Visual Studio Code window.

    8tm5egdx.png

  18. Delete the pyodbc and psycopg2-binary libraries.

    00z3g507.png

  19. Save and close the file.

  20. In the Explorer pane, expand the backend folder and then expand the contoso_hotel folder.

  21. Select views.py. The file displays in the right side of the Visual Studio Code window.

  22. Delete all code between the following region markers in the code (around lines 309 - 332):

     #region -------- FRONTEND API ENDPOINTS --------
     #endregion -------- FRONTEND API ENDPOINTS -------- 
    

    zo0ce49i.png

  23. Save and close the file.

  24. Leave Visual Studio Code open. You’ll run additional commands in the next task.