Skip to content

Commit 14c4776

Browse files
rafalkowalskithelostone-mc
authored andcommitted
Fix default user ordering (#4972)
1 parent a619aa7 commit 14c4776

2 files changed

Lines changed: 95 additions & 26 deletions

File tree

app/dashboard/tests/test_users_list.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,75 @@
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
1919
"""
20-
2120
import json
21+
from datetime import timedelta
2222

2323
from django.contrib.auth.models import User
24+
from django.db.models import Count, Q
2425
from django.test.client import RequestFactory
26+
from django.utils import timezone
2527

26-
from dashboard.models import Profile
28+
from dashboard.models import Bounty, BountyFulfillment, Profile
2729
from dashboard.views import users_fetch
2830
from test_plus.test import TestCase
2931

32+
CURRENT_USERNAME = "asdfasdf"
33+
34+
def setup_bounties():
35+
owners = [CURRENT_USERNAME, 'user2']
36+
for owner in owners:
37+
Bounty.objects.create(
38+
title='foo',
39+
value_in_token=3,
40+
token_name='USDT',
41+
web3_created=timezone.now() - timedelta(days=7),
42+
github_url='https://github.com/oogetyboogety/gitcointestproject/issues/28',
43+
token_address='0x0',
44+
issue_description='hello world',
45+
bounty_owner_github_username=owner,
46+
is_open=True,
47+
accepted=True,
48+
expires_date=timezone.now() + timedelta(days=1, hours=1),
49+
idx_project_length=5,
50+
project_length='Months',
51+
bounty_type='Feature',
52+
experience_level='Intermediate',
53+
raw_data={},
54+
idx_status='submitted',
55+
bounty_owner_email='asdfasdf@bar.com',
56+
current_bounty=True
57+
)
58+
59+
BountyFulfillment.objects.create(
60+
fulfiller_address='0x0000000000000000000000000000000000000000',
61+
fulfiller_email='fred@bar.com',
62+
fulfiller_github_username='user1',
63+
fulfiller_name='Fred',
64+
accepted=True,
65+
bounty=Bounty.objects.first(),
66+
profile=User.objects.filter(username='user1').first().profile
67+
)
68+
69+
BountyFulfillment.objects.create(
70+
fulfiller_address='0x0000000000000000000000000000000000000000',
71+
fulfiller_email='fred@bar.com',
72+
fulfiller_github_username='user19',
73+
fulfiller_name='Fred',
74+
accepted=True,
75+
bounty=Bounty.objects.last(),
76+
profile=User.objects.last().profile
77+
)
78+
3079

3180
class UsersListTest(TestCase):
3281
"""Define tests for the user list."""
3382

3483
def setUp(self):
3584
self.request = RequestFactory()
3685
self.current_user = User.objects.create(
37-
password="asdfasdf", username="asdfasdf")
86+
password="asdfasdf", username=CURRENT_USERNAME)
3887
current_user_profile = Profile.objects.create(
39-
user=self.current_user, data={}, hide_profile=False, handle="asdfasdf")
88+
user=self.current_user, data={}, hide_profile=False, handle=CURRENT_USERNAME)
4089

4190
for i in range(20):
4291
user = User.objects.create(password="{}".format(i),
@@ -48,3 +97,17 @@ def test_user_list(self):
4897
request = self.request
4998
request.user = self.current_user
5099
assert json.loads(users_fetch(request.get('/api/v0.1/users_fetch?user={}'.format(self.current_user.id))).content)['count'] == 21
100+
101+
def test_default_users_ordering_with_previous_workers_at_the_top(self):
102+
setup_bounties()
103+
104+
all_profiles = Profile.objects.annotate(
105+
worked_with=Count(
106+
'fulfilled', filter=Q(
107+
fulfilled__accepted=True,
108+
fulfilled__bounty__bounty_owner_github_username__iexact=CURRENT_USERNAME
109+
)
110+
)
111+
).order_by('-worked_with')
112+
113+
assert all_profiles.values('user__username', 'worked_with')[0] == {'user__username': 'user1', 'worked_with': 1}

app/dashboard/views.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -826,41 +826,47 @@ def users_fetch(request):
826826
fulfilled__accepted=True,
827827
fulfilled__bounty__github_url__icontains=organisation
828828
).distinct()
829-
profile_list = profile_list.annotate(
830-
feedbacks_got_count=Count('feedbacks_got', filter=Q(feedbacks_got__sender_profile=current_user.profile.id))
831-
)
829+
830+
def previous_worked():
831+
if current_user.profile.persona_is_funder:
832+
return Count(
833+
'fulfilled',
834+
filter=Q(
835+
fulfilled__bounty__network=network,
836+
fulfilled__accepted=True,
837+
fulfilled__bounty__bounty_owner_github_username__iexact=current_user.profile.handle
838+
)
839+
)
840+
841+
return Count(
842+
'bounties_funded__fulfillments',
843+
filter=Q(
844+
bounties_funded__fulfillments__bounty__network=network,
845+
bounties_funded__fulfillments__accepted=True,
846+
bounties_funded__fulfillments__fulfiller_github_username=current_user.profile.handle
847+
)
848+
)
849+
832850
profile_list = Profile.objects.filter(pk__in=profile_list).annotate(
833851
average_rating=Avg('feedbacks_got__rating', filter=Q(feedbacks_got__bounty__network=network))
834-
).order_by(
835-
order_by
852+
).annotate(previous_worked=previous_worked()).order_by(
853+
order_by, '-previous_worked'
836854
)
837855
profile_list = profile_list.values_list('pk', flat=True)
838856
params = dict()
839857
all_pages = Paginator(profile_list, limit)
840858
all_users = []
841859
this_page = all_pages.page(page)
842860

843-
this_page = Profile.objects.filter(pk__in=[ele for ele in this_page]).annotate(
844-
feedbacks_got_count=Count('feedbacks_got', filter=Q(feedbacks_got__sender_profile=current_user.profile.id))
845-
).order_by(order_by).annotate(
846-
previous_worked_count=Count('fulfilled', filter=Q(
847-
fulfilled__bounty__network=network,
848-
fulfilled__accepted=True,
849-
fulfilled__bounty__bounty_owner_github_username__iexact=current_user.profile.handle
850-
))).annotate(
861+
this_page = Profile.objects.filter(pk__in=[ele for ele in this_page])\
862+
.order_by(order_by).annotate(
863+
previous_worked_count=previous_worked()).annotate(
851864
count=Count('fulfilled', filter=Q(fulfilled__bounty__network=network, fulfilled__accepted=True))
852865
).annotate(
853866
average_rating=Avg('feedbacks_got__rating', filter=Q(feedbacks_got__bounty__network=network))
854-
)
867+
).order_by('-previous_worked_count')
855868
for user in this_page:
856869
previously_worked_with = 0
857-
if current_user:
858-
previously_worked_with = BountyFulfillment.objects.filter(
859-
bounty__bounty_owner_github_username__iexact=current_user.profile.handle,
860-
fulfiller_github_username__iexact=user.handle,
861-
bounty__network=network,
862-
accepted=True
863-
).count()
864870
count_work_completed = Activity.objects.filter(profile=user, activity_type='work_done').count()
865871
count_work_in_progress = Activity.objects.filter(profile=user, activity_type='start_work').count()
866872
profile_json = {
@@ -870,7 +876,7 @@ def users_fetch(request):
870876
'job_type', 'linkedin_url', 'resume', 'remote', 'keywords',
871877
'organizations', 'is_org']}
872878
profile_json['job_status'] = user.job_status_verbose if user.job_search_status else None
873-
profile_json['previously_worked'] = previously_worked_with > 0
879+
profile_json['previously_worked'] = user.previous_worked_count > 0
874880
profile_json['position_contributor'] = user.get_contributor_leaderboard_index()
875881
profile_json['position_funder'] = user.get_funder_leaderboard_index()
876882
profile_json['work_done'] = count_work_completed

0 commit comments

Comments
 (0)