wevote / WeVoteServer

We Vote's API application server written in Django/Python. Election data pulled from many sources, used by https://github.com/wevote/WebApp and https://github.com/wevote/WeVoteCordova and https://github.com/wevote/Campaigns.
https://api.wevoteusa.org
MIT License
50 stars 451 forks source link

Calculate Voters with 3+ Friends #1510

Closed DaleMcGrew closed 3 years ago

DaleMcGrew commented 3 years ago

We already show "Voters With Friends: 8" on the Admin home page. We want to be able to show "Voters with X+ Friends". In the file "/WeVoteServer/friend/models.py" in the function "fetch_voters_with_friends_count" we already figure out how many voters have 1 or more friendships. We need to figure out how to count number of voter_we_vote_ids in either column, so we can limit the count of the number of friendships to those voter_we_vote_ids which appear "this_many_friends_or_more" times or more. Started Google searching with "django python count distinct".

                friends_query = CurrentFriend.objects.using('readonly')\
                    .annotate(voter_we_vote_id=F('viewee_voter_we_vote_id')).values_list('voter_we_vote_id', flat=True)\
                    .union(
                    CurrentFriend.objects.using('readonly')
                        .annotate(voter_we_vote_id=F('viewer_voter_we_vote_id'))
                        .values_list('voter_we_vote_id', flat=True)
                )
                voters_with_friends_count = friends_query.count()
SailingSteve commented 3 years ago
Screen Shot 2021-01-13 at 8 59 49 PM
SailingSteve commented 3 years ago

I took a different approach