A comprehensive library management system built with Django and Django REST Framework, featuring JWT authentication, role-based access control, and a RESTful API.
-
User Authentication & Authorization
- User registration and login with JWT tokens
- Role-based access control (Admin, Registered User, Anonymous)
- Secure password management
- Token refresh and blacklisting
- User profile management
-
Book Catalog Management
- Complete CRUD operations for books
- ISBN validation and uniqueness
- Book availability tracking
- Cover images and ratings
- Status management (Available, Borrowed, Maintenance, Lost)
-
Advanced Search & Filtering
- Full-text search across title, author, ISBN, description
- Filter by status, genre, language, author, publication date, rating
- Sort by title, author, date, rating
- Pagination (10 items per page)
-
Loan Management System
- Borrow books (14-day loan period)
- Return books with automatic availability updates
- Renew loans (up to 2 renewals)
- Overdue tracking and management
- Loan limit (max 5 active loans per user)
-
Security Features
- CSRF protection (built-in Django middleware)
- XSS protection (Django template escaping)
- SQL injection protection (Django ORM)
- JWT token authentication
- Permission-based access control
-
User Roles
- Anonymous Users: Browse and search books (read-only)
- Registered Users: Browse, search, and borrow books
- Administrators: Full access to manage books, users, and loans
- Backend: Django 6.0
- API: Django REST Framework 3.16
- Authentication: JWT (djangorestframework-simplejwt)
- Database: PostgreSQL
- Environment Management: python-dotenv
- Python 3.10+
- PostgreSQL
- pip
git clone <repository-url>
cd LibraryMangementpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtCreate a .env file in the root directory:
# Database Configuration
DB_NAME=library_db
DB_USER=library_user
DB_PASSWORD=your_secure_password
DB_HOST=localhost
DB_PORT=5432
# Django Configuration
SECRET_KEY=your-secret-key-here
DEBUG=True# Login to PostgreSQL
psql -U postgres
# Create database and user
CREATE DATABASE library_db;
CREATE USER library_user WITH PASSWORD 'your_secure_password';
ALTER ROLE library_user SET client_encoding TO 'utf8';
ALTER ROLE library_user SET default_transaction_isolation TO 'read committed';
ALTER ROLE library_user SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE library_db TO library_user;
\qpython manage.py makemigrations
python manage.py migratepython manage.py createsuperuserFollow the prompts to create an administrator account.
python manage.py runserverThe API will be available at http://localhost:8000/api/
The Swagger documentation will be available at http://localhost:8000/swagger/
POST /api/auth/register/- Register a new userPOST /api/auth/login/- Login and get JWT tokensPOST /api/auth/logout/- Logout and blacklist tokenPOST /api/auth/token/refresh/- Refresh access token
GET /api/users/me/- Get current user infoGET /api/users/profile/- Get/Update own profileGET /api/users/profile/{id}/- Get/Update specific user (Admin)POST /api/users/change-password/- Change passwordGET /api/users/- List users (role-based access)
POST /api/admin/create-admin/- Create a new admin user (Admin only)POST /api/admin/promote/- Promote user to admin (Admin only)
GET /api/books/- List all books (with filtering & search)GET /api/books/{id}/- Get book detailsPOST /api/books/create/- Add new book (Admin only)PUT /api/books/{id}/update/- Update book (Admin only)DELETE /api/books/{id}/delete/- Delete book (Admin only)GET /api/books/{id}/availability/- Check book availability
GET /api/loans/- List loans (filtered by user role)GET /api/loans/my/- Get current user's loansGET /api/loans/overdue/- List overdue loans (Admin only)GET /api/loans/{id}/- Get loan detailsPOST /api/loans/borrow/- Borrow a bookPOST /api/loans/{id}/return/- Return a borrowed bookPOST /api/loans/{id}/renew/- Renew a loan
- Swagger UI:
http://localhost:8000/swagger/- Interactive API documentation - ReDoc:
http://localhost:8000/redoc/- Alternative documentation view - OpenAPI Schema:
http://localhost:8000/swagger.json- Raw OpenAPI spec
# Register a new user
curl -X POST http://localhost:8000/api/auth/register/ \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"password": "SecurePass123!",
"password2": "SecurePass123!"
}'
# Login and get access token
curl -X POST http://localhost:8000/api/auth/login/ \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "SecurePass123!"
}'# List all books
curl http://localhost:8000/api/books/
# Search for books
curl "http://localhost:8000/api/books/?search=python"
# Filter available books
curl "http://localhost:8000/api/books/?status=AVAILABLE"
# Filter by genre
curl "http://localhost:8000/api/books/?genre__icontains=fiction"
# Get book details
curl http://localhost:8000/api/books/1/# Borrow book with ID 1
curl -X POST http://localhost:8000/api/loans/borrow/ \
-H "Authorization: Bearer <your_access_token>" \
-H "Content-Type: application/json" \
-d '{"book_id": 1, "notes": "First book borrowed"}'
# View my loans
curl -X GET http://localhost:8000/api/loans/my/ \
-H "Authorization: Bearer <your_access_token>"# Add a new book (Admin only)
curl -X POST http://localhost:8000/api/books/create/ \
-H "Authorization: Bearer <admin_access_token>" \
-H "Content-Type: application/json" \
-d '{
"title": "Clean Code",
"author": "Robert C. Martin",
"isbn": "9780132350884",
"page_count": 464,
"genre": "Programming",
"total_copies": 5,
"available_copies": 5
}'Visit http://localhost:8000/swagger/ for interactive API documentation and testing!
LibraryMangement/
βββ config/ # Project configuration
β βββ settings.py # Django settings with JWT config
β βββ urls.py # Main URL configuration
β βββ wsgi.py
βββ core/ # Main application
β βββ models.py # User model with roles
β βββ serializers.py # API serializers
β βββ views.py # API views
β βββ permissions.py # Custom permissions
β βββ urls.py # App URLs
β βββ admin.py # Admin configuration
βββ docker/ # Docker configuration
βββ .env # Environment variables (not in git)
βββ .gitignore
βββ requirements.txt # Python dependencies
βββ API_AUTHENTICATION.md # Detailed API documentation
βββ README.md # This file
- Browse books (read-only)
- View book details
- Search for books
- All anonymous user permissions
- Borrow books
- View borrowing history
- Manage own profile
- All registered user permissions
- Add/remove books
- Manage all users
- View all loans
- Access admin panel
python manage.py makemigrationspython manage.py migrate# Run all tests
python manage.py test
# Run specific app tests
python manage.py test core
python manage.py test library
python manage.py test loan
# Run with coverage
coverage run --source='.' manage.py test
coverage report
# See detailed testing guide
# Check TESTING_GUIDE.md- 80+ unit tests covering models, serializers, and views
- Integration tests for complete workflows
- Security tests for unauthorized access
- Performance tests for pagination and filtering
- Create a superuser (if not already done)
- Navigate to
http://localhost:8000/admin/ - Login with superuser credentials
- JWT token authentication
- Token rotation and blacklisting
- Password hashing with Django's built-in validators
- Role-based access control
- Secure environment variable management
- CSRF protection
- SQL injection protection (Django ORM)
Required environment variables in .env:
| Variable | Description | Example |
|---|---|---|
DB_NAME |
PostgreSQL database name | library_db |
DB_USER |
PostgreSQL username | library_user |
DB_PASSWORD |
PostgreSQL password | your_password |
DB_HOST |
Database host | localhost |
DB_PORT |
Database port | 5432 |
SECRET_KEY |
Django secret key | random-secret-key |
DEBUG |
Debug mode | True or False |
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- User authentication system
- JWT token management
- Role-based access control
- Book model and management
- Loan tracking system
- Advanced search and filtering
- Book availability management
- API documentation with Swagger/OpenAPI
- Security implementation (CSRF, XSS, SQL Injection protection)
- Comprehensive unit tests (80+ tests)
- Integration tests for workflows
- API tests for all endpoints
- Security and permission tests
- Email notifications for overdue books
- Book reservations
- Fine calculation for overdue returns
- Book cover image upload
- Export reports (PDF/Excel)
- Docker deployment
- CI/CD pipeline with automated testing
This project is licensed under the MIT License.
For questions or support, please open an issue on the GitHub repository.
Note: This project is currently in development. The authentication system is complete, and book/loan management features are coming soon.