This project is a Django-based API for booking hotel rooms. It provides endpoints for user authentication and room booking.
- User authentication using JWT tokens
- Room booking with date validation and conflict checking
- Custom responses for API endpoints
- Logging for debugging and monitoring
- Django: Web framework for building the API.
- Django REST Framework: For creating RESTful API endpoints.
- JWT Authentication: For secure user authentication.
- PostgreSQL: Database used for storing data (you can modify it in the
.env
file). - Python: Programming language used.
Follow these steps to set up the project locally:
-
Clone the repository:
git clone https://github.com/arfa79/booking.git cd booking
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate
-
Install the dependencies:
pip install -r requirements.txt
-
Set up environment variables: Create a
.env
file in the root directory and add the following variables:SECRET_KEY=<your-secret-key> DEBUG=True DATABASE_URL=postgres://username:password@localhost/dbname
-
Apply migrations:
python manage.py migrate
-
Create a superuser:
python manage.py createsuperuser
-
Run the development server:
python manage.py runserver
The development server will be running at
http://127.0.0.1:8000
.
To create initial data for your application, you can use the Django shell.
- Open Django Shell
Run the following command in your terminal:python manage.py shell
- Import the Necessary Models
Use these imports to access your models and the Django User model:
from core.models import Hotel, Room from django.contrib.auth.models import User
- Create a Hotel Object
Replace "Grand Plaza Hotel" and "New York City" with your desired hotel name and location:
hotel = Hotel.objects.create(name="Grand Plaza Hotel", location="New York City")
- Create Room Objects
Use the Room model to create rooms associated with the hotel. Replace room numbers as needed:
room1 = Room.objects.create(hotel=hotel, room_number="101") room2 = Room.objects.create(hotel=hotel, room_number="102")
- Create a User
Replace "testuser" and "password123" with the username and password you want to use:
user = User.objects.create_user(username="testuser", password="password123")
- Print the IDs
Print the IDs of the created objects to confirm successful creation:
print(f"Hotel ID: {hotel.id}, Room1 ID: {room1.id}, Room2 ID: {room2.id}, User ID: {user.id}")
-
Obtain a JWT token:
POST /api/token/
Request Body:
{ "username": "alireza", "password": "123" }
Response:
{ "access": "your-access-token", "refresh": "your-refresh-token" }
-
Refresh a JWT token:
POST /api/token/refresh/
Request Body:
{ "refresh": "your-refresh-token" }
-
Create a booking:
POST /booking/
Request Body:
{ "room": 1, "start_at": "2026-06-01T12:00:00", "end_at": "2026-06-01T14:00:00" }
Response:
{ "detail": "Success", "code": "success", "error": null, "data": { "message": "Booking successful!", "booking_id": 1 } }
To run the tests, use the following command:
sh python manage.py test
Below are examples of using Postman to interact with the API:
POST /api/token/
Response:
POST /booking/
Response:
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.