This repository contains the backend API for the SafeBoda project, built with Django and Django REST Framework (DRF). The API allows managing users and trips, supporting full CRUD operations while ensuring security and validation rules.
- Custom user model (
CustomUser) withuser_type(Rider / Driver) - Trip management with
rider,driver,pickup_location,dropoff_location,status, and timestamps - Full CRUD API endpoints for users and trips
- Passwords are write-only to prevent exposure
- Validation to prevent a user being both rider and driver in the same trip
- Clone the repository:
git clone https://github.com/Pelino-Courses/module-2-safeboda-project-phase-2-itbienvenu
cd safe_boda- Create a virtual environment:
python -m venv .venv- Activate the virtual environment:
- Windows:
.\.venv\Scripts\activate - macOS/Linux:
source .venv/bin/activate
- Install dependencies:
pip install -r requirements.txt- Apply migrations:
python manage.py makemigrations
python manage.py migrate- Run the server:
python manage.py runserver-
Inherits from Django's
AbstractUser -
Fields:
usernameemailpassworduser_type(Rider / Driver)
-
Fields:
rider(FK toCustomUser)driver(FK toCustomUser, nullable)pickup_location(string)dropoff_location(string)status(REQUESTED / IN_PROGRESS / COMPLETED)created_at(timestamp)
-
UserSerializer
- Handles serialization/deserialization for
CustomUser - Password is write-only
- Creates users using
create_userfor hashed passwords
- Handles serialization/deserialization for
-
TripSerializer
- Handles serialization/deserialization for
Trip - Validates that rider and driver are not the same
- Handles serialization/deserialization for
-
UserViewSet
- Full CRUD operations for users
- Endpoint:
/api/users/
-
TripViewSet
- Full CRUD operations for trips
- Endpoint:
/api/trips/
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/users/ |
List all users |
| POST | /api/users/ |
Create a new user |
| GET | /api/users/<id>/ |
Retrieve a specific user |
| PATCH | /api/users/<id>/ |
Update a specific user |
| DELETE | /api/users/<id>/ |
Delete a specific user |
| GET | /api/trips/ |
List all trips |
| POST | /api/trips/ |
Create a new trip |
| GET | /api/trips/<id>/ |
Retrieve a specific trip |
| PATCH | /api/trips/<id>/ |
Update a trip (e.g., status) |
| DELETE | /api/trips/<id>/ |
Delete a specific trip |
-
Open Postman and set the request URL to your local server, e.g.,
http://127.0.0.1:8000/api/users/ -
Test User Endpoints:
- Create user: POST JSON
{
"username": "john",
"email": "[email protected]",
"password": "securepassword",
"user_type": "Rider"
}- List users: GET
/api/users/
-
Test Trip Endpoints:
- Create trip: POST JSON
{
"rider": 1,
"driver": 2,
"pickup_location": "Kigali",
"dropoff_location": "Gisozi",
"status": "REQUESTED"
}- Update status: PATCH
/api/trips/1/with{ "status": "IN_PROGRESS" } - Delete trip: DELETE
/api/trips/1/
- Passwords are write-only in serializers
- Users cannot be both rider and driver in the same trip
- Status field has restricted choices (
REQUESTED,IN_PROGRESS,COMPLETED)