This project implements a backend API for managing hierarchical media content using Django. The API organizes media content into channels, supports hierarchical relationships, calculates ratings dynamically, and exports data to a CSV file.
We need to define an API for our media platform that organizes content in a hierarchical structure. Each piece of content can include files (e.g., videos, PDFs, or text), metadata (such as descriptions, authors, and genres), and a rating between 0 and 10.
Content is managed through Channels, which define the hierarchy. A channel has a title, language, and image, and it can either contain subchannels or content, but never both. Each channel must have at least one subchannel or one content item.
A channel's rating is calculated dynamically as the average rating of its subchannels or, if it has none, the average of its contents. Channels without content do not contribute to their parent's rating. Since the structure is dynamic, ratings cannot be stored directly and must be computed as needed.
- Features
- Requirements
- Installation
- Data Model
- Usage
- Admin Management
- Testing
- Docker setup
- Folder Structure
- Future Improvements
- License
- Manage hierarchical channels and their associated content.
- Compute dynamic ratings for channels based on their contents or subchannels.
- Filter channels by groups.
- Export channel ratings to a CSV file.
- RESTful API endpoints for managing channels, contents, and groups.
- Built-in unit tests to validate functionality.
- Optional Docker environment for easy deployment.
- Python 3.9 or later
- Django 4.x
- Django REST Framework (DRF)
- PostgreSQL (recommended) or SQLite
- Docker (optional, for containerized deployment)
git clone git@github.com:manel.jimeno/media_django_sample.git
cd media_django_sample
python -m venv .venv
source .venv/bin/activate # On Windows, use `venv\Scripts\activate`
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
The API will be accessible at http://127.0.0.1:8000.
- Channels:
GET /api/channels/
: List all channels.GET /api/channels/?group=<group_id>
: Filter channels by group.POST /api/channels/
: Create a new channel.GET /api/channels/<id>/
: Retrieve details of a specific channel.
- Contents:
GET /api/contents/
: List all contents.POST /api/contents/
: Create new content.
- Groups:
GET /api/groups/
: List all groups.POST /api/groups/
: Create a new group.
Run the management command to export channel ratings to a CSV file:
python manage.py export_channel_ratings
This will generate a file named channel_ratings.csv
with two columns: Channel Title
and Average Rating
.
To manage the Content
, Channel
, and Group
models through the Django admin interface, you first need to create
an admin user:
python manage.py createsuperuse
Provide the required information:
- Username: The admin username.
- Email address: The admin email (optional).
- Password: A secure password for the admin account.
Once created, log in to the Django admin panel at:
http://127.0.0.1:8000/admin
The following models can be managed from the admin panel:
- Content:
- Add or edit individual pieces of media content.
- Configure metadata, file URLs, and ratings.
- Channel:
- Create or edit channels, including hierarchical relationships.
- Assign subchannels or associate content with a channel.
- Group channels by using the Group feature.
- Group:
- Add or manage channel groups.
- Use groups to filter channels through the API.
The admin interface provides:
- Search functionality for quickly finding specific records.
- List filters for filtering by attributes (e.g., ratings, language, or groups).
- Inline editing for managing related models (e.g., linking Contents to a Channel).
To run unit tests:
python manage.py test
To run the project in a Docker container:
- Build the Docker image:
docker-compose build
- Run the container:
docker-compose up
The API will be available at http://localhost:8000.
media/
│
├── media_platform/
│ ├── migrations/ # Database migrations
│ ├── models.py # Database models
│ ├── serializers.py # API serializers
│ ├── views.py # API views
│ ├── urls.py # App-specific URLs
│ ├── tests.py # Unit tests
│ ├── management/
│ ├── commands/
│ ├── export_channel_ratings.py # CSV export command
│
├── media/
│ ├── settings.py # Project settings
│ ├── urls.py # Root URLs
│ ├── asgi.py # Application definition (Daphne, Uvicorn)
│ ├── wsgi.py # Application definition (Gunicorn, uWSGI, mod_wsgi)
│
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose configuration
├── requirements.txt # Python dependencies
├── README.md # Project documentation
- Add user authentication and permissions for managing channels and contents.
- Implement caching for performance optimization in rating calculations.
- Extend CI/CD pipelines for automated testing and deployment.
This project is developed as part of a technical demonstration and is not intended for production use.