-
Notifications
You must be signed in to change notification settings - Fork 19
Code Flows
Luke Repasky edited this page Jun 30, 2023
·
4 revisions
There are several "flows" through the code that are particularly common and will likely be relevant in future code changes.
The most common flow is: SomeReact.jsx -> api.js -> urls.py -> some_function() in views.py
- All components that handle the frontend can be found at
PPUC/frontend/src/components/[any_filename].jsx
with the most important components for searching beingCommentary.jsx
andResearchers.jsx
- Both classes have a method called
handleSearch()
which does exactly what it says, it takes a user search query and sends it to the backend to be dealt with. This is the parameter for the API call in these files. - To interact with backend data, API calls are made in both files. It looks something like this:
Api.getResearcherSearchResults(searchQuery).then((resp) =>
with the arrow pointing towards some function to deal with the resp (the JSON response received from the backend). - This call is made on the frontend within a component and connects first to
PPUC/frontend/src/libs/api.js
which handles the API endpoints and the URLs they are found at. An example of this:${Api.BASE_PATH}/locations
where base path is simply PPUC or the root directory. In this instance, locations is a pointer to a URL. - These URLs are connected to the backend with
PPUC/PxPUC/urls.py
which defines where resources are able to found at. It also connects toPPUC/PxPUC/views.py
that handles the logic of defining what these resources will be. - In our case, a lot of these views return a queryset, or a
list
of sorts, pulling data from the SQLite database using Django operations. These querysets will follow the fields described withinPPUC/PxPUC/serializers.py
, using the corresponding serializer that is defined per view. - For the search algorithm itself, this can be found in the method ResearchersSearchList within
views.py
. The bulk of the work is done within the for loop, as it iteratively shrinks the user query until it becomes just a single word. It filters in sentence and location objects relevant to the user query, both of which are objects/models defined inPPUC/PxPUC/models.py
, and annotates new fields for rank which is just an ASCII letter grade.
- As mentioned above, the important data can be found to be defined within
PPUC/PxPUC/models.py
. Things like the sentence model, location model, and all of the new models added in SP23 and SU23 can be found here. - Models deals purely with defining what a model will be, the fields/variables it will take on, and the relationships it will have to other models.
- Many of the older models (pre-SP23 and SU23) are connected to the location model (which in this case can be thought of as a contract that has contract text).
- How these models get populated with actual data to fill those fields/variables tends to vary. The older models tend to read from contract text files, either pulling the names out of the file titles in the case of location models, or pulling sentences out in the case of sentence models. The newer models (aka Provision, Keyword, Municipality, MasterContract, Department) all read from the master sheet found at
PPUC/PxPUC/static/app/mastersheets
(at the time of writing this, the sheet is only within the read-in-spreadsheet branch). - In the case of the older models (with a focus on contracts, sentences, and locations), this data read-in occurs within the setup scripts. In particular,
PPUC/scripts/add_contracts.py
. Add_contracts handles all of the populating for these important models and follows the conventions defined in models.py as mentioned above. - For the newer models, this data read in happens from the mastersheet as referenced above. This occurs in the setup script as well but within
PPUC/scripts/read_master_sheet.py
. To actually receive the contract text files from the master sheet,PPUC/scripts/download_contracts.py
handles that. To reiterate, the last two mentioned scripts are only within the read-in-spreadsheet branch until that gets updated to the main branch.
-
Citizens.jsx
contains the front-end code and api.js references to get the appropriate database objects.
Note: Much of the Citizens.jsx code should be deprecated, as it refers to database columns (i.e. Location ID) that have no use for this specific application.
-
api.js
is called for bothApi.getLocationQuestions(0)
andApi.getLocationStages(0)
. These refer to urls.py's "/location/0/questions" and "location/0/stages" respectively -
urls.py
refers toLocationQuestionList
andLocationStageList
for Locations and Stages respectively -
views.py
contains LocationQuestionList, which gets a queryset of questions from the database (queries Question.objects.all()). LocationStageList returns a JSON response containing the hard-coded "stages of a police complaint", which serve as rough categories for the questions.