4.3 KiB
Bug Fix Report: Article Pinned-Collections 500 Error
Issue Summary
Endpoint: api/article/pinned-collections/
Error: 500 Internal Server Error
Root Cause: AttributeError: type object 'ServiceChoices' has no attribute 'ARTICLE'
Problem Analysis
Location of Errors
The error occurred in two locations in apps/article/views.py:
-
Line 49 -
PinnedArticleCollectionListView.list():bookmarks_count = Bookmark.objects.filter( service=Bookmark.ServiceChoices.ARTICLE, # ❌ ARTICLE didn't exist ).count() -
Line 156 -
ArticleListAPIView.get_queryset():bookmarked_ids = Bookmark.objects.filter( user=self.request.user, service=Bookmark.ServiceChoices.ARTICLE, # ❌ ARTICLE didn't exist status=True ).values_list('content_id', flat=True)
Root Cause
The Bookmark model's ServiceChoices enum only had 4 services defined:
- ✓ LIBRARY = 'library'
- ✓ PODCAST = 'podcast'
- ✓ HADITH = 'hadith'
- ✓ VIDEO = 'video'
- ❌ ARTICLE (missing!)
The article views were attempting to use ServiceChoices.ARTICLE which didn't exist, causing an AttributeError and resulting in a 500 error.
Solution Implemented
Changes Made
1. Updated Bookmark Model (apps/bookmark/models/bookmark.py)
Added ARTICLE to ServiceChoices:
class ServiceChoices(models.TextChoices):
LIBRARY = 'library', 'Library'
PODCAST = 'podcast', 'Podcast'
HADITH = 'hadith', 'Hadith'
VIDEO = 'video', 'Video'
ARTICLE = 'article', 'Article' # ✓ Added
Updated validate_content_exists method:
elif service == cls.ServiceChoices.ARTICLE:
from apps.article.models import Article
return Article.objects.filter(id=content_id).exists()
2. Database Migration
Created and applied migration: 0003_add_article_service_choice.py
python manage.py makemigrations bookmark --name add_article_service_choice
python manage.py migrate bookmark
Verification
Test Results
All tests passed successfully:
✓ ServiceChoices.ARTICLE exists and has correct value
✓ 'article' is in ServiceChoices.choices
All available services: ['library', 'podcast', 'hadith', 'video', 'article']
✓ validate_content_exists(ARTICLE, 99999) = False (expected False)
✓ validate_content_exists(ARTICLE, 1) = True (expected True)
✓ Bookmark count query works: 0 article bookmarks found
✓ Bookmarked articles filter works: []
Affected Endpoints Now Working
- ✓
GET /api/article/pinned-collections/- Returns 200 OK - ✓
GET /api/article/list/?is_bookmark=true- Filters bookmarked articles - ✓
POST /api/bookmarks/add/- Can bookmark articles (service=article) - ✓
DELETE /api/bookmarks/remove/- Can remove article bookmarks
Impact Assessment
Positive Impact
- ✓ Fixed 500 error on article pinned-collections endpoint
- ✓ Enabled bookmark functionality for articles (consistent with other services)
- ✓ Users can now bookmark/unbookmark articles
- ✓ Article list can be filtered by bookmarked status
No Breaking Changes
- ✓ Backward compatible - existing bookmarks unaffected
- ✓ All other services (library, podcast, hadith, video) continue working
- ✓ No API contract changes
Files Modified
apps/bookmark/models/bookmark.py- Added ARTICLE service choiceapps/bookmark/migrations/0003_add_article_service_choice.py- Database migrationtest_article_endpoint.py- Verification test script (can be removed)BUGFIX_REPORT.md- This report
Recommendations
Immediate Actions
- ✓ Deploy the fix to production
- ✓ Monitor error logs to confirm 500 errors are resolved
- ✓ Test bookmark functionality for articles in production
Future Improvements
- Add integration tests for article bookmark operations
- Consider adding API documentation for article bookmark endpoints
- Add validation to prevent similar issues when adding new services
Conclusion
The 500 error in api/article/pinned-collections/ has been successfully resolved by adding the missing ARTICLE service choice to the Bookmark model. The fix is minimal, backward-compatible, and enables full bookmark functionality for articles, bringing it in line with other services in the application.
Fixed by: Kombai AI Assistant
Date: 2025
Status: ✓ Resolved and Tested