A simple Django REST Framework project for managing inventory with JWT authentication for regular users and Basic Authentication for admin users. This API includes CRUD operations on inventory items, data validation, and error handling.
- User registration and authentication using JWT for regular users.
- CRUD operations for inventory items (admin only, using Basic Authentication).
- Data validation and error handling.
- Regular users can only list items and must be JWT authenticated.
Follow these steps to set up the project on your local machine:
- Python 3.x
- pip
-
Clone the repository:
git clone <repository-url> cd inventory_management
-
Create and activate a virtual environment:
On macOS/Linux:
python3 -m venv venv source venv/bin/activateOn Windows:
python -m venv venv ./venv/Scripts/activate
-
Install dependencies:
pip install -r requirements.txt
-
Run migrations:
python manage.py makemigrations python manage.py migrate
-
Create a superuser (for accessing the admin interface and performing CRUD operations):
python manage.py createsuperuser
-
Start the development server:
python manage.py runserver
-
Create a new user:
curl -X POST http://localhost:8000/register/ -H "Content-Type: application/json" -d '{ "username": "your_username", "email": "your_email@example.com", "password": "your_password" }'
-
Get the JWT token using the user credentials:
curl -X POST http://localhost:8000/token/ -d "username=your_username&password=your_password"The response will include the access token and refresh token (Regular user need to include the
accesstoken in the request){ "refresh": "your_refresh_token", "access": "your_access_token" }
-
Create an Item (Admin Only):
curl -X POST http://localhost:8000/items/ -u adminuser:your_admin_password -H "Content-Type: application/json" -d '{ "name": "Item 1", "description": "Description of Item 1", "quantity": <some-positive-number> }'
-
Read Items (Admin Only):
curl -X GET http://localhost:8000/items/ -u adminuser:your_admin_password
-
Read a Specific Item (Admin Only):
curl -X GET http://localhost:8000/items/1/ -u adminuser:your_admin_password
-
Update an Item (Admin Only):
curl -X PUT http://localhost:8000/items/1/ -u adminuser:your_admin_password -H "Content-Type: application/json" -d '{ "name": "Updated Item1", "description": "Updated description of Item1", "quantity": <some-positive-number> }'
-
Delete an Item (Admin Only):
curl -X DELETE http://localhost:8000/items/1/ -u adminuser:your_admin_password
Regular users can only list items and must be JWT authenticated.
-
Read Items:
curl -X GET http://localhost:8000/items/ -H "Authorization: Bearer your_access_token" -
Read a Specific Item:
curl -X GET http://localhost:8000/items/1/ -H "Authorization: Bearer your_access_token"
To run the tests, use the following command:
python manage.py test