Backend REST API for a Leave Management System built with Django 5 and Django REST Framework. It manages employees, authentication (JWT), and leave requests with role-based access control.
- Python 3.11+
- Django 5.x
- Django REST Framework (DRF)
- djangorestframework-simplejwt (JWT auth)
- django-cors-headers
- drf-spectacular (OpenAPI schema)
-
models.py–Employee(custom user model) andLeavemodels -serializers.py– serializers for leave CRUD, registration, and password update -
views.py– DRF generic views for authentication and leave management -
urls.py– app-level routes (mounted under/api/) -
leavesystem/ -
settings.py– Django configuration (custom user model, REST_FRAMEWORK, logging) -
urls.py– project-level URLs (/admin/,/api/...)
From the backendproject directory:
-
Create and activate a virtual environment (Windows PowerShell)
uv venv .venv .\.venv\Scripts\Activate.ps1
-
Install dependencies (from
pyproject.toml):uv add django djangorestframework djangorestframework-simplejwt django-cors-headers drf-spectacular django-environ django-extensions
-
Apply migrations
python manage.py makemigrations python manage.py migrate
-
Create a superuser (for Django admin)
python manage.py createsuperuser
-
Run the development server
python manage.py runserver
The API will be available at: http://127.0.0.1:8000/.
Custom user model: leaves.Employee (email used as login identifier).
Fields (simplified):
email(unique, used as username)first_name,last_nameemployee_departmentemployee_positionphone_numberemployee_role– one ofSTAFF,MANAGER,HR
Roles affect access to leave data:
HR/MANAGER– can see all leave requestsSTAFF– can see only their own leave requests
Default DRF permission (settings.REST_FRAMEWORK) is IsAuthenticated, except where explicitly overridden (e.g. registration).
- Django admin:
GET /admin/ - API base: all app endpoints are under
/api/(see below).
All auth endpoints are defined in leaves/urls.py and mounted under /api/ in leavesystem/urls.py.
- URL:
POST /api/auth/register/ - Permissions: Public (
AllowAny) - Body (JSON):
json { "first_name": "Michael", "last_name": "Ataro", "email": "michael@example.com", "password": "SecurePassword123!", "employee_department": "Computer Science", "employee_position": "Student Developer", "phone_number": "0712345678", "employee_role": "STAFF" } - Response: 201 Created with employee details (password excluded).
-
URL:
POST /api/auth/login/ -
Permissions: Public
-
Body (JSON):
{ "email": "michael@example.com", "password": "SecurePassword123!"} -
Response:
{ "refresh": "<refresh_token>", "access": "<access_token>" }
Use the access token in the Authorization header for protected endpoints:
Authorization: Bearer <access_token>- URL:
POST /api/auth/token/refresh/ - Body:
{ "refresh": "<refresh_token>" }
-
URL:
PUT /api/auth/update-password/ -
Permissions: Authenticated
-
Body (JSON):
{ "old_password": "OldPassword123!", "new_password": "NewSecurePassword456!" }
Defined in leaves/urls.py and protected by IsAuthenticated.
-
URL:
GET /api/leaves/all/ -
HR/MANAGER: list all leaves -
STAFF: list only their own leaves -
URL:
POST /api/leaves/all/ -
Create a new leave request for the current user.
-
Body (JSON) example:
{ "leave_type": "SICK", "start_date": "2026-03-20", "end_date": "2026-03-22", "reason": "Flu" }
- URL:
/api/leaves/<id>/(e.g./api/leaves/3/) - Methods:
GET– retrieve a leavePUT/PATCH– update a leaveDELETE– delete a leave
Access rules:
HR/MANAGER: can access any leaveSTAFF: can only access their own leaves
Validation in the Leave model & serializer ensures:
end_dateis not beforestart_datestart_dateis not in the past- Certain leave types (e.g.
SICK,STUDY) require a supporting document
- Logging is configured in
leavesystem/settings.pyto output to the console. - The
leavesapp logs key user actions (registration, leave creation, listing, and password updates) using Python’sloggingmodule. - When running
python manage.py runserver, check the terminal to trace user activity and debug issues.
From the backendproject directory:
python manage.py testThis will run tests for the leaves app (and any other apps with tests).