def interested_profiles(request, bounty_id): """Retrieve memberships who like a Status in a community. :request method: GET Args: bounty_id (int): ID of the Bounty. Parameters: page (int): The page number. limit (int): The number of interests per page. Returns: django.core.paginator.Paginator: Paged interest results. """ page = request.GET.get('page', 1) limit = request.GET.get('limit', 10) current_profile = request.session.get('profile_id') profile_interested = False # Get all interests for the Bounty. interests = Interest.objects \ .filter(bounty__id=bounty_id) \ .select_related('profile') \ .order_by('created') # Check whether or not the current profile has already expressed interest. if current_profile and interests.filter( profile__pk=current_profile).exists(): profile_interested = True paginator = Paginator(interests, limit) try: interests = paginator.page(page) except PageNotAnInteger: interests = paginator.page(1) except EmptyPage: return JsonResponse([]) interests_data = [] for interest in interests: interest_data = ProfileSerializer(interest.profile).data interests_data.append(interest_data) if request.is_ajax(): return JsonResponse(json.dumps(interests_data), safe=False) return JsonResponse({ 'paginator': { 'num_pages': interests.paginator.num_pages, }, 'data': interests_data, 'profile_interested': profile_interested })
def new_interest(request, bounty_id): """Claim Work for a Bounty. :request method: POST Args: post_id (int): ID of the Bounty. Returns: dict: The success key with a boolean value and accompanying error. """ profile_id = request.session.get('profile_id') if not profile_id: return JsonResponse({'error': 'You must be authenticated!'}, status=401) try: bounty = Bounty.objects.get(pk=bounty_id) except Bounty.DoesNotExist: raise Http404 try: Interest.objects.get(profile_id=profile_id, bounty=bounty) return JsonResponse( { 'error': 'You have already expressed interest in this bounty!', 'success': False }, status=401) except Interest.DoesNotExist: interest = Interest.objects.create(profile_id=profile_id) bounty.interested.add(interest) maybe_market_to_slack(bounty, 'start_work') except Interest.MultipleObjectsReturned: bounty_ids = bounty.interested \ .filter(profile_id=profile_id) \ .values_list('id', flat=True) \ .order_by('-created')[1:] Interest.objects.filter(pk__in=list(bounty_ids)).delete() return JsonResponse( { 'error': 'You have already expressed interest in this bounty!', 'success': False }, status=401) return JsonResponse({ 'success': True, 'profile': ProfileSerializer(interest.profile).data })
def new_interest(request, bounty_id): """Claim Work for a Bounty. :request method: POST Args: post_id (int): ID of the Bounty. Returns: dict: The success key with a boolean value and accompanying error. """ profile_id = request.session.get('profile_id') if not profile_id: return JsonResponse( { 'error': 'You must be authenticated via github to use this feature!' }, status=401) try: bounty = Bounty.objects.get(pk=bounty_id) except Bounty.DoesNotExist: raise Http404 num_issues = 3 active_bounties = Bounty.objects.current().filter( idx_status__in=['open', 'started']) num_active = Interest.objects.filter(profile_id=profile_id, bounty__in=active_bounties).count() is_working_on_too_much_stuff = num_active >= num_issues if is_working_on_too_much_stuff: return JsonResponse( { 'error': f'You may only work on max of {num_issues} issues at once.', 'success': False }, status=401) try: Interest.objects.get(profile_id=profile_id, bounty=bounty) return JsonResponse( { 'error': 'You have already expressed interest in this bounty!', 'success': False }, status=401) except Interest.DoesNotExist: interest = Interest.objects.create(profile_id=profile_id) bounty.interested.add(interest) record_user_action( Profile.objects.get(pk=profile_id).handle, 'start_work', interest) maybe_market_to_slack(bounty, 'start_work') except Interest.MultipleObjectsReturned: bounty_ids = bounty.interested \ .filter(profile_id=profile_id) \ .values_list('id', flat=True) \ .order_by('-created')[1:] Interest.objects.filter(pk__in=list(bounty_ids)).delete() return JsonResponse( { 'error': 'You have already expressed interest in this bounty!', 'success': False }, status=401) return JsonResponse({ 'success': True, 'profile': ProfileSerializer(interest.profile).data })
def new_interest(request, bounty_id): """Claim Work for a Bounty. :request method: POST Args: bounty_id (int): ID of the Bounty. Returns: dict: The success key with a boolean value and accompanying error. """ profile_id = request.user.profile.pk if request.user.is_authenticated and hasattr( request.user, 'profile') else None access_token = request.GET.get('token') if access_token: helper_handle_access_token(request, access_token) github_user_data = get_github_user_data(access_token) profile = Profile.objects.prefetch_related('bounty_set') \ .filter(handle=github_user_data['login']).first() profile_id = profile.pk else: profile = request.user.profile if profile_id else None if not profile_id: return JsonResponse( { 'error': _('You must be authenticated via github to use this feature!') }, status=401) try: bounty = Bounty.objects.get(pk=bounty_id) except Bounty.DoesNotExist: raise Http404 num_issues = profile.max_num_issues_start_work active_bounties = Bounty.objects.current().filter( idx_status__in=['open', 'started']) num_active = Interest.objects.filter(profile_id=profile_id, bounty__in=active_bounties).count() is_working_on_too_much_stuff = num_active >= num_issues if is_working_on_too_much_stuff: return JsonResponse( { 'error': _(f'You may only work on max of {num_issues} issues at once.'), 'success': False }, status=401) if profile.has_been_removed_by_staff(): return JsonResponse( { 'error': _('Because a staff member has had to remove you from a bounty in the past, you are unable to start more work at this time. Please contact support.' ), 'success': False }, status=401) try: Interest.objects.get(profile_id=profile_id, bounty=bounty) return JsonResponse( { 'error': _('You have already expressed interest in this bounty!'), 'success': False }, status=401) except Interest.DoesNotExist: issue_message = request.POST.get("issue_message") interest = create_new_interest_helper(bounty, request.user, issue_message) except Interest.MultipleObjectsReturned: bounty_ids = bounty.interested \ .filter(profile_id=profile_id) \ .values_list('id', flat=True) \ .order_by('-created')[1:] Interest.objects.filter(pk__in=list(bounty_ids)).delete() return JsonResponse( { 'error': _('You have already expressed interest in this bounty!'), 'success': False }, status=401) return JsonResponse({ 'success': True, 'profile': ProfileSerializer(interest.profile).data })