Skip to content

Commit 649718e

Browse files
authored
Merge pull request #14 from tarxemo/dev
the new changes
2 parents 6786261 + 66a18ec commit 649718e

9 files changed

Lines changed: 1122 additions & 10 deletions

File tree

github_management/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from users.abstract_models import BaseUser
1111
import math
1212

13+
1314
class Country(models.Model):
1415
"""Model to store available countries from committers.top"""
1516
name = models.CharField(max_length=100, unique=True)

github_management/views_auth.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.utils.decorators import method_decorator
1111
from django.conf import settings
1212
from django.contrib.sites.models import Site
13+
from django.utils import timezone
1314

1415
from allauth.socialaccount.models import SocialAccount, SocialApp, SocialToken
1516
from allauth.socialaccount.adapter import get_adapter as get_social_adapter
@@ -38,7 +39,103 @@ class ProfileView(TemplateView):
3839
def get_context_data(self, **kwargs):
3940
context = super().get_context_data(**kwargs)
4041
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+
4157
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+
})
42139

43140

44141
import json

0 commit comments

Comments
 (0)