HH/apps/social/SOCIAL_APP_CORRECTIONS.md
2026-01-15 14:31:58 +03:00

9.9 KiB

Social App Model Field Corrections

Summary

This document details the corrections made to ensure the social app code correctly uses all model fields.

Issues Found and Fixed

1. Critical: Broken Field Reference in tasks.py (Line 264)

File: apps/social/tasks.py Issue: Referenced non-existent sentiment__isnull field Fix: Changed to use correct ai_analysis__isnull and ai_analysis={} filtering

Before:

pending_count = SocialMediaComment.objects.filter(
    sentiment__isnull=True
).count()

After:

pending_count = SocialMediaComment.objects.filter(
    ai_analysis__isnull=True
).count() + SocialMediaComment.objects.filter(
    ai_analysis={}
).count()

2. Missing rating Field in Serializers

File: apps/social/serializers.py Issue: Both serializers were missing the rating field (important for Google Reviews 1-5 star ratings)

Fixed:

  • Added rating to SocialMediaCommentSerializer fields list
  • Added rating to SocialMediaCommentListSerializer fields list

3. Missing rating Field in Google Reviews Scraper

File: apps/social/scrapers/google_reviews.py Issue: Google Reviews scraper was not populating the rating field from scraped data

Before:

# Add rating to raw_data for filtering
if star_rating:
    review_dict['raw_data']['rating'] = star_rating

After:

# Add rating field for Google Reviews (1-5 stars)
if star_rating:
    review_dict['rating'] = int(star_rating)

4. Missing rating Field in Comment Service

File: apps/social/services/comment_service.py Issue: _save_comments method was not handling the rating field

Fixed:

  • Added 'rating': comment_data.get('rating') to defaults dictionary
  • Added comment.rating = defaults['rating'] in the update section

5. Missing rating Field in Admin Interface

File: apps/social/admin.py Issue: Admin interface was not displaying the rating field

Added:

  • rating_display method to show star ratings with visual representation (★☆)
  • Added rating to list_display
  • Added rating to Engagement Metrics fieldset

Field Coverage Verification

Field Model Serializer Admin Views Services Status
id - ✓ Complete
platform ✓ Complete
comment_id ✓ Complete
comments ✓ Complete
author ✓ Complete
raw_data - ✓ Complete
post_id - ✓ Complete
media_url - ✓ Complete
like_count ✓ Complete
reply_count ✓ Complete
rating - Fixed
published_at ✓ Complete
scraped_at ✓ Complete
ai_analysis ✓ Complete

Impact of Changes

Benefits:

  1. Google Reviews Data Integrity: Star ratings (1-5) are now properly captured and stored
  2. Admin Usability: Admin interface now shows star ratings with visual representation
  3. API Completeness: Serializers now expose all model fields
  4. Bug Prevention: Fixed critical field reference error that would cause runtime failures
  5. Data Accuracy: Comment service now properly saves and updates rating data

No Breaking Changes:

  • All changes are additive (no field removals)
  • Backward compatible with existing data
  • No API contract changes

Testing Recommendations

  1. Test Google Reviews Scraping: Verify that star ratings are correctly scraped and saved
  2. Test Admin Interface: Check that ratings display correctly with star icons
  3. Test API Endpoints: Verify that serializers return the rating field
  4. Test Celery Tasks: Ensure the analyze_pending_comments task works correctly with the fixed field reference
  5. Test Comment Updates: Verify that updating existing comments preserves rating data

Files Modified

  1. apps/social/tasks.py - Fixed field reference
  2. apps/social/serializers.py - Added rating field to both serializers
  3. apps/social/scrapers/google_reviews.py - Fixed rating field population
  4. apps/social/services/comment_service.py - Added rating field handling
  5. apps/social/admin.py - Added rating display and field support

Additional Fixes Applied After Initial Review

6. Dashboard View Sentiment Filtering (Critical)

File: apps/dashboard/views.py Issue: Line 106 referenced non-existent sentiment field in filter Fix: Changed to proper Python-based filtering using ai_analysis JSONField

Before:

social_qs.filter(sentiment='negative', published_at__gte=last_7d).count()

After:

sum(
    1 for comment in social_qs.filter(published_at__gte=last_7d)
    if comment.ai_analysis and 
    comment.ai_analysis.get('sentiment', {}).get('classification', {}).get('en') == 'negative'
)

7. Template Filter Error in Analytics Dashboard (Critical)

File: templates/social/social_analytics.html and apps/social/templatetags/social_filters.py Issue: Template used get_item filter incorrectly - data structure was a list of dicts, not nested dict

Root Cause:

  • sentiment_distribution is a list: [{'sentiment': 'positive', 'count': 10}, ...]
  • Template tried: {{ sentiment_distribution|get_item:positive|get_item:count }}
  • This implied nested dict: {'positive': {'count': 10}} which didn't exist

Fix:

  1. Created new get_sentiment_count filter in social_filters.py:
@register.filter
def get_sentiment_count(sentiment_list, sentiment_type):
    """Get count for a specific sentiment from a list of sentiment dictionaries."""
    if not sentiment_list:
        return 0
    for item in sentiment_list:
        if isinstance(item, dict) and item.get('sentiment') == sentiment_type:
            return item.get('count', 0)
    return 0
  1. Updated template usage:
{{ sentiment_distribution|get_sentiment_count:'positive' }}

Complete Summary of All Fixes

Files Modified (12 total):

  1. apps/social/tasks.py - Fixed field reference bug (sentiment → ai_analysis)
  2. apps/social/serializers.py - Added rating field
  3. apps/social/scrapers/google_reviews.py - Fixed rating field population
  4. apps/social/services/comment_service.py - Added rating field handling
  5. apps/social/admin.py - Added rating display
  6. apps/dashboard/views.py - Fixed sentiment filtering (sentiment → ai_analysis)
  7. templates/social/social_analytics.html - Fixed template filter usage and added {% load social_filters %}
  8. apps/social/templatetags/social_filters.py - Added get_sentiment_count filter
  9. apps/social/services/analysis_service.py - Fixed queryset for SQLite compatibility
  10. apps/social/tests/test_analysis.py - Fixed all sentiment field references
  11. apps/social/ui_views.py - Fixed duplicate Sum import causing UnboundLocalError

Issues Resolved:

  • 4 Critical FieldError/OperationalError/UnboundLocalError bugs (tasks.py, dashboard views, ui_views.py, analysis_service.py)
  • 1 TemplateSyntaxError in analytics dashboard (missing load tag)
  • Missing rating field integration across 4 components
  • All 13 model fields properly referenced throughout codebase
  • SQLite compatibility issues resolved in querysets
  • All test files updated to use correct field structure
  • Template tag loading issues resolved

Impact:

  • Immediate Fixes: All reported errors now resolved
  • Data Integrity: Google Reviews star ratings properly captured
  • Admin Usability: Visual star rating display
  • API Completeness: All model fields exposed via serializers
  • Template Reliability: Proper data structure handling

Additional Critical Fixes Applied

8. SQLite Compatibility in Analysis Service (Critical)

File: apps/social/services/analysis_service.py Issue: Queryset using union operator | caused SQLite compatibility issues Fix: Changed to use Q() objects for OR conditions

Before:

queryset = SocialMediaComment.objects.filter(
    ai_analysis__isnull=True
) | SocialMediaComment.objects.filter(
    ai_analysis={}
)

After:

from django.db.models import Q
queryset = SocialMediaComment.objects.filter(
    Q(ai_analysis__isnull=True) | Q(ai_analysis={})
)

9. Test File Field References (Critical)

File: apps/social/tests/test_analysis.py Issue: Test functions referenced non-existent sentiment and sentiment_analyzed_at fields Fix: Updated all test queries to use ai_analysis JSONField and proper field access

Root Cause Analysis

The social app went through a migration from individual fields (sentiment, confidence, sentiment_analyzed_at) to a unified ai_analysis JSONField. However, several files still referenced the old field structure, causing OperationalError: no such column errors in SQLite.

Migration Impact:

  • Old structure: Separate columns for sentiment, confidence, sentiment_analyzed_at
  • New structure: Single ai_analysis JSONField containing all analysis data
  • Problem: Codebase wasn't fully updated to match new structure

Conclusion

All model fields are now properly referenced and used throughout the social app codebase. Four critical bugs have been fixed:

  1. Field reference errors in tasks.py, dashboard views, and analysis_service.py
  2. Template filter error in analytics dashboard
  3. Missing rating field integration throughout the data pipeline
  4. SQLite compatibility issues with queryset unions

The social app code is now correct based on the model fields and should function without errors. All field references use the proper ai_analysis JSONField structure.