Request/Response Flow
Relevant source files
The following files were used as context for generating this wiki page:The request/response flow in the Campuspal Room Finder application is designed to provide a dynamic and responsive user experience. It primarily handles two major features: searching for available rooms and uploading schedule data. The system leverages a combination of traditional Django server-rendered pages for initial loads, asynchronous JavaScript (AJAX) for partial page updates during searches, and a dedicated API endpoint for retrieving detailed data on demand.
This architecture minimizes full-page reloads, creating a fluid interface. The main search functionality is handled by a single view that can respond to both standard HTTP requests and AJAX requests, while a separate view manages the file upload process for administrators.
Room Search and Timeline Display (/)
The core user interaction revolves around the search page, which allows users to find rooms and view their availability on a visual timeline. This process involves an initial page load and subsequent AJAX requests for dynamic updates.
Initial Page Load (GET Request)
When a user first navigates to the application's root URL, a standard GET request is processed to render the main search interface.
- The request to
/is handled by thehomeview, which issues a redirect to thesearch_roomsview. - The
search_roomsview is called with a GET request. It initializes an emptyRoomSearchForm. - The view then renders the
finder/search.htmltemplate, passing the form and empty lists for room data. - The template displays the search form and a welcome message, as no search has been performed yet.
This flow is illustrated in the diagram below.
AJAX Search Submission (POST Request)
To provide a seamless experience without page reloads, form submissions are handled asynchronously.
- The user fills out the search form and clicks "Search".
- The
submitevent is intercepted byfinder/static/finder/js/scripts.js. - An AJAX
POSTrequest is sent to thesearch_roomsview. The request includes the form data and a special header,X-Requested-With: XMLHttpRequest, to identify it as an AJAX call. - The
search_roomsview processes the form data, queries the database for rooms and their schedules, and builds a timeline for each room. - If a specific time is searched, rooms are categorized into
available_roomsandunavailable_rooms. - Crucially, the view calculates
start_percentageandwidth_percentagefor each timeline block, which are used by the CSS to render the visual timeline correctly. - Detecting the AJAX header, the view renders the
finder/timeline_partial.htmltemplate instead of the fullsearch.html. - The partial HTML is returned to the browser, and the JavaScript injects it into the results container.
The following sequence diagram details this client-server interaction.
The context passed to the template includes the following data:
| Key | Type | Description |
|---|---|---|
form | RoomSearchForm | The search form instance. |
available_rooms | list | A list of rooms that are free for the specified time and duration. Each item contains room and timeline data. |
unavailable_rooms | list | A list of all other rooms, or all rooms if no specific time was searched. |
errors | list | A list of error messages, if any. |
search_performed | bool | A flag indicating if a specific time-based search was executed. |
| Sources: finder/views.py:102-108 |
Room Details API (/api/room/<room_id>/)
When a user clicks on a room in the timeline, a modal appears with detailed information. This is powered by a simple API endpoint that returns JSON data.
Frontend Interaction
- An event listener in
scripts.jsdetects a click on a.timeline-rowelement. - It extracts the
data-room-idfrom the clicked element and the currentdayfrom the search form. - A
fetchGET request is made to/api/room/<room_id>/?day=<day>. - Upon receiving a JSON response, the script dynamically populates the modal's HTML with the room's details and its full daily schedule.
- The modal is then displayed to the user.
Sources: finder/static/finder/js/scripts.js:40-69
Backend API Logic
The request is routed to the api.room_details view, which is responsible for serving the room data.
The sequence diagram below illustrates the API flow.
Based on the JavaScript client, the API returns a JSON object with the following structure:
| Key | Type | Description |
|---|---|---|
room | object | An object containing room details (name, building, capacity, amenities). |
timeline | array | An array of schedule blocks, each with start, end, type, and course. |
| Sources: finder/static/finder/js/scripts.js:48-62 |
Data Upload Flow (/upload/)
This flow allows staff members to upload rooms.xlsx and timetable.xlsx files to populate the database.
- A staff member navigates to
/upload/. Theupload_excelview is protected by the@staff_member_requireddecorator, ensuring only authorized users can access it. - On a
GETrequest, the view rendersfinder/templates/finder/upload_excel.html, which contains a form for file uploads. - The user selects files and submits the form, making a
POSTrequest. - The
upload_excelview validates theExcelUploadForm. - It uses the
pandaslibrary to read the data from the uploaded Excel files in memory. - The view first clears existing data (
RoomandClassScheduleobjects) before importing new data. - It iterates through the rows, validates the data (e.g., day/time format), and creates new model instances.
- Feedback is provided to the user via the Django messages framework.
- Upon successful processing, the user is redirected back to the main search page.
The logic for handling the file upload is shown below.
URL Routing Configuration
Django's URL dispatcher maps incoming request paths to the appropriate view functions. The project's routing is configured across two main files.
| Path | View Function | Name | Description |
|---|---|---|---|
"" (root) | include("finder.urls") | - | Includes all URLs from the finder app. |
"" (in finder) | views.home | finder:home | Redirects to the search page. |
search/ (effective path) | views.search_rooms | finder:search_rooms | Handles room searching and timeline display. |
upload/ | views.upload_excel | finder:upload_excel | Handles Excel file uploads for staff. |
api/room/<int:room_id>/ | api.room_details | finder:room_details | API endpoint to fetch details for a specific room. |
| Sources: roomfinder/settings.py:53, finder/urls.py (whole file), README.md |