This is a Django REST API that allows users to authenticate, upload images, and retrieve images based on various filters. It is currently deployed to https://image-django-api-33d51ec30738.herokuapp.com/
- User registration and authentication
- Image upload functionality
- Image retrieval with filtering options:
- By author (case-insensitive)
- By maximum width
- By maximum height
- Pagination for image retrieval
- Database seeded with initial images from an external source
- Dynamic test data generation for robust testing
- Python 3.x
- Django 5.x
- Django REST Framework
- PostgreSQL
- Docker (for containerization)
- django-pgbulk (for efficient bulk operations)
-
Clone the repository:
git clone https://github.com/yourusername/image-api.git cd image_django_api
-
Set up a virtual environment:
python3 -m venv venv source venv/bin/activate
-
Install dependencies:
python3 -m pip install -r requirements.txt
-
Create a
.env
file in the root directory and add the following line:IMAGE_SEED_URL=https://picsum.photos/v2/list?page=2&limit=100
-
Set up the PostgreSQL database and update the
DATABASES
configuration insettings.py
. -
Run migrations:
python3 manage.py migrate
-
Seed the database with initial images:
python3 manage.py seed_images
-
Run the development server:
python3 manage.py runserver
POST /api/register/
: Register a new userPOST /api/login/
: Login and receive an authentication tokenGET /api/images/
: Retrieve all images (supports pagination)GET /api/images/?page=2
: Retrieve all images on page 2GET /api/images/?author=<name>
: Retrieve images by authorGET /api/images/?maxWidth=<width>
: Retrieve images with width <= specified valueGET /api/images/?maxWeight=<height>
: Retrieve images with height <= specified value
Run the test suite with:
python3 manage.py test
To register a new user, use the following curl
command:
curl -X POST -H "Content-Type: application/json" -d '{"username": "your_username", "password": "your_password", "email": "[email protected]"}' http://localhost:8000/api/register/
After registering, log in to obtain an authentication token:
curl -X POST -H "Content-Type: application/json" -d '{"username": "your_username", "password": "your_password"}' http://localhost:8000/api/login/
The response will include a token:
{
"token": "your_token_here"
}
Now that you have the token, you can access the /api/images/
endpoint:
curl -H "Authorization: Token your_token_here" http://localhost:8000/api/images/
Replace your_token_here
with the actual token you received from the login response.