Bug Hunt: Movie Recommendations API
You are reviewing a Django REST Framework codebase for a Movie Recommendations API built by a junior developer. The API lets authenticated users rate movies, manage a watchlist, and get personalized recommendations based on their activity.
The test suite has 6 tests, and they are all failing. Your job is to find and fix the 6 bugs hidden in views.py so that every test passes.
You should only edit
views.py. The models, serializers, URL config, and tests are all correct.
API Reference
Ratings
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/movies/<id>/rate/ |
Rate a movie (1-10). Creates a new rating (201 Created) or updates an existing one (200 OK). |
GET |
/api/movies/<id>/my-rating/ |
Get the current user's rating for a movie. |
GET |
/api/movies/<id>/average/ |
Get the average user rating (public, no auth required). |
Watchlist
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/movies/<id>/watchlist/ |
Toggle a movie on/off the user's watchlist. |
Recommendations
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/movies/recommendations/ |
Get up to 10 personalized recommendations. |
Models
Movie
- id (auto)
- title (string)
- year (integer)
- genre (string, comma-separated e.g. "Crime,Drama")
- rating (float, 0-10, average critic rating)
- description (text)
- popularity (integer)
UserRating
- id (auto)
- user (FK -> User)
- movie (FK -> Movie)
- score (integer, 1-10, the user's personal rating)
- created_at (auto timestamp)
Watchlist
- id (auto)
- user (FK -> User)
- movie (FK -> Movie)
- added_at (auto timestamp)
Recommendation Algorithm
The get_recommendations endpoint uses this logic:
- No activity: If the user has zero ratings AND zero watchlist items, return a getting-started message.
- Rated movies:
- Rated HIGH (score > 5): Find similar-genre candidates with
rating >= 7.0, excluding the source movie. Apply a 1.2x multiplier. - Rated LOW (score <= 5): Find different-genre candidates with
rating >= 7.0, excluding the source. No multiplier.
- Rated HIGH (score > 5): Find similar-genre candidates with
- Watchlisted movies (skip if also rated):
- Find similar-genre candidates with
rating >= 7.0, excluding the source. No multiplier.
- Find similar-genre candidates with
- Scoring:
genreScore = matching_genres / source_genresratingScore = candidate_rating / 10- Rated-high / watchlist:
totalScore = (genreScore * 0.7 + ratingScore * 0.3) * multiplier - Rated-low:
totalScore = ratingScore(no genre component)
- Deduplicate: Keep the higher score when a movie appears from multiple sources.
- Sort by score descending, return top 10.
Response format
| Situation | Message |
|---|---|
| No activity | "Start exploring movies by rating them or adding them to your watchlist!" |
| Activity but no qualifying candidates | "No recommendations found" |
| Results | "Found N personalized recommendations" |
Each recommendation includes: id, title, year, rating, genre, popularity, score, source ("rated" or "watchlist"), sourceMovie, and optionally userRating (only when source is "rated").
What to look for
Each bug is a small, realistic mistake. Think about:
- HTTP status codes - are the right ones being returned for create vs update?
- Conditional logic - are the conditions checking the right thing?
- Query filters - is the quality filter and source exclusion applied everywhere?
- Data labels - do the response fields match the source of the recommendation?
- Ordering - are results sorted before being sliced?