|
10 | 10 | from django.utils.decorators import method_decorator |
11 | 11 | from django.conf import settings |
12 | 12 | from django.contrib.sites.models import Site |
| 13 | +from django.utils import timezone |
13 | 14 |
|
14 | 15 | from allauth.socialaccount.models import SocialAccount, SocialApp, SocialToken |
15 | 16 | from allauth.socialaccount.adapter import get_adapter as get_social_adapter |
@@ -38,7 +39,103 @@ class ProfileView(TemplateView): |
38 | 39 | def get_context_data(self, **kwargs): |
39 | 40 | context = super().get_context_data(**kwargs) |
40 | 41 | context['user'] = self.request.user |
| 42 | + |
| 43 | + # Add intelligent follow context |
| 44 | + user = self.request.user |
| 45 | + context['can_enable_intelligent'] = bool(user.github_access_token) |
| 46 | + context['intelligent_follow_enabled'] = user.intelligent_follow_enabled |
| 47 | + context['intelligent_follow_schedule'] = user.intelligent_follow_schedule |
| 48 | + context['last_intelligent_follow'] = user.last_intelligent_follow |
| 49 | + |
| 50 | + # Get recent follow actions |
| 51 | + from github_management.models import GitHubFollowAction |
| 52 | + recent_actions = GitHubFollowAction.objects.filter( |
| 53 | + user=user |
| 54 | + ).order_by('-followed_at')[:10] |
| 55 | + context['recent_actions'] = recent_actions |
| 56 | + |
41 | 57 | return context |
| 58 | + |
| 59 | + def post(self, request, *args, **kwargs): |
| 60 | + """Handle intelligent follow settings updates""" |
| 61 | + user = request.user |
| 62 | + |
| 63 | + # Check if user has GitHub token |
| 64 | + if not user.github_access_token: |
| 65 | + return JsonResponse({ |
| 66 | + 'success': False, |
| 67 | + 'message': 'You must add a GitHub access token first to enable intelligent following.' |
| 68 | + }) |
| 69 | + |
| 70 | + # Validate GitHub token for intelligent follow |
| 71 | + try: |
| 72 | + from users.validators import validate_github_token_for_intelligent_follow |
| 73 | + validate_github_token_for_intelligent_follow(user.github_access_token) |
| 74 | + except Exception as e: |
| 75 | + return JsonResponse({ |
| 76 | + 'success': False, |
| 77 | + 'message': f'GitHub token validation failed: {str(e)}' |
| 78 | + }) |
| 79 | + |
| 80 | + action = request.POST.get('action') |
| 81 | + |
| 82 | + if action == 'toggle_intelligent': |
| 83 | + enabled = request.POST.get('enabled') == 'true' |
| 84 | + user.intelligent_follow_enabled = enabled |
| 85 | + user.save() |
| 86 | + |
| 87 | + return JsonResponse({ |
| 88 | + 'success': True, |
| 89 | + 'message': f"Intelligent following {'enabled' if enabled else 'disabled'}" |
| 90 | + }) |
| 91 | + |
| 92 | + elif action == 'update_schedule': |
| 93 | + schedule = request.POST.get('schedule') |
| 94 | + if schedule in ['daily', 'weekly', 'manual']: |
| 95 | + user.intelligent_follow_schedule = schedule |
| 96 | + user.save() |
| 97 | + |
| 98 | + return JsonResponse({ |
| 99 | + 'success': True, |
| 100 | + 'message': f"Schedule updated to {schedule}" |
| 101 | + }) |
| 102 | + else: |
| 103 | + return JsonResponse({ |
| 104 | + 'success': False, |
| 105 | + 'message': 'Invalid schedule' |
| 106 | + }) |
| 107 | + |
| 108 | + elif action == 'run_manual': |
| 109 | + from users.services.intelligent_follow_service import IntelligentFollowService |
| 110 | + from github_management.models import GitHubFollowAction |
| 111 | + |
| 112 | + # Run intelligent follow manually |
| 113 | + result = IntelligentFollowService.intelligent_follow_users(user, max_follows=10) |
| 114 | + |
| 115 | + # Record action - using existing model structure |
| 116 | + from github_management.models import GitHubUser |
| 117 | + # Create or get a placeholder GitHubUser for tracking |
| 118 | + placeholder_user, _ = GitHubUser.objects.get_or_create( |
| 119 | + github_username="manual_run", |
| 120 | + defaults={ |
| 121 | + 'country': GitHubUser.objects.first().country if GitHubUser.objects.exists() else None |
| 122 | + } |
| 123 | + ) |
| 124 | + GitHubFollowAction.objects.create( |
| 125 | + user=user, |
| 126 | + github_user=placeholder_user, |
| 127 | + status='followed_back' if result['success'] else 'pending' |
| 128 | + ) |
| 129 | + |
| 130 | + user.last_intelligent_follow = timezone.now() |
| 131 | + user.save() |
| 132 | + |
| 133 | + return JsonResponse(result) |
| 134 | + |
| 135 | + return JsonResponse({ |
| 136 | + 'success': False, |
| 137 | + 'message': 'Invalid action' |
| 138 | + }) |
42 | 139 |
|
43 | 140 |
|
44 | 141 | import json |
|
0 commit comments