Skip to content

Commit 90b9e8a

Browse files
committed
created update member profile
1 parent e6fb0e2 commit 90b9e8a

File tree

3 files changed

+75
-90
lines changed

3 files changed

+75
-90
lines changed

backend/apps/github/management/commands/github_update_users.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from apps.common.models import BATCH_SIZE
99
from apps.github.models.repository_contributor import RepositoryContributor
1010
from apps.github.models.user import User
11-
from apps.owasp.models.member_profile import MemberProfile
1211

1312
logger = logging.getLogger(__name__)
1413

@@ -47,35 +46,14 @@ def handle(self, *args, **options):
4746
.annotate(total_contributions=Sum("contributions_count"))
4847
}
4948
users = []
50-
profiles = []
5149
for idx, user in enumerate(active_users[offset:]):
5250
prefix = f"{idx + offset + 1} of {active_users_count - offset}"
5351
print(f"{prefix:<10} {user.title}")
5452

5553
user.contributions_count = user_contributions.get(user.id, 0)
5654
users.append(user)
5755

58-
profile, created = MemberProfile.objects.get_or_create(github_user_id=user.id)
59-
if created:
60-
profile.github_user = user
61-
profile.contributions_count = user.contributions_count
62-
profile.is_owasp_staff = user.is_owasp_staff
63-
profile.has_public_member_page = user.has_public_member_page
64-
profiles.append(profile)
65-
6656
if not len(users) % BATCH_SIZE:
6757
User.bulk_save(users, fields=("contributions_count",))
68-
MemberProfile.bulk_save(
69-
profiles,
70-
fields=(
71-
"contributions_count",
72-
"is_owasp_staff",
73-
"has_public_member_page",
74-
),
75-
)
7658

7759
User.bulk_save(users, fields=("contributions_count",))
78-
MemberProfile.bulk_save(
79-
profiles,
80-
fields=("contributions_count", "is_owasp_staff", "has_public_member_page"),
81-
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""A command to update member profiles."""
2+
3+
import logging
4+
5+
from django.core.management.base import BaseCommand
6+
from django.db.models import Q, Sum
7+
8+
from apps.common.models import BATCH_SIZE
9+
from apps.github.models.repository_contributor import RepositoryContributor
10+
from apps.github.models.user import User
11+
from apps.owasp.models.member_profile import MemberProfile
12+
13+
logger = logging.getLogger(__name__)
14+
15+
16+
class Command(BaseCommand):
17+
help = "Update member profiles."
18+
19+
def add_arguments(self, parser):
20+
"""Add command-line arguments to the parser.
21+
22+
Args:
23+
parser (argparse.ArgumentParser): The argument parser instance.
24+
25+
"""
26+
parser.add_argument("--offset", default=0, required=False, type=int)
27+
28+
def handle(self, *args, **options):
29+
"""Handle the command execution.
30+
31+
Args:
32+
*args: Variable length argument list.
33+
**options: Arbitrary keyword arguments containing command options.
34+
35+
"""
36+
active_users = User.objects.order_by("-created_at")
37+
active_users_count = active_users.count()
38+
offset = options["offset"]
39+
user_contributions = {
40+
item["user_id"]: item["total_contributions"]
41+
for item in RepositoryContributor.objects.exclude(
42+
Q(repository__is_fork=True)
43+
| Q(repository__organization__is_owasp_related_organization=False)
44+
| Q(user__login__in=User.get_non_indexable_logins()),
45+
)
46+
.values("user_id")
47+
.annotate(total_contributions=Sum("contributions_count"))
48+
}
49+
profiles = []
50+
for idx, user in enumerate(active_users[offset:]):
51+
prefix = f"{idx + offset + 1} of {active_users_count - offset}"
52+
print(f"{prefix:<10} {user.title}")
53+
54+
profile, created = MemberProfile.objects.get_or_create(github_user_id=user.id)
55+
if created:
56+
profile.github_user = user
57+
profile.contributions_count = user_contributions.get(user.id, 0)
58+
profiles.append(profile)
59+
60+
if not len(profiles) % BATCH_SIZE:
61+
MemberProfile.bulk_save(
62+
profiles,
63+
fields=("contributions_count",),
64+
)
65+
66+
MemberProfile.bulk_save(
67+
profiles,
68+
fields=("contributions_count",),
69+
)

backend/tests/apps/github/management/commands/github_update_users_test.py

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@ def test_add_arguments(self):
3030
"--offset", default=0, required=False, type=int
3131
)
3232

33-
@patch("apps.github.management.commands.github_update_users.MemberProfile")
3433
@patch("apps.github.management.commands.github_update_users.User")
3534
@patch("apps.github.management.commands.github_update_users.RepositoryContributor")
3635
@patch("apps.github.management.commands.github_update_users.BATCH_SIZE", 2)
37-
def test_handle_with_default_offset(
38-
self, mock_repository_contributor, mock_user, mock_member_profile
39-
):
36+
def test_handle_with_default_offset(self, mock_repository_contributor, mock_user):
4037
"""Test command execution with default offset."""
41-
mock_member_profile.objects.get_or_create.return_value = (MagicMock(), False)
4238
mock_user1 = MagicMock(id=1, title="User 1", contributions_count=0)
4339
mock_user2 = MagicMock(id=2, title="User 2", contributions_count=0)
4440
mock_user3 = MagicMock(id=3, title="User 3", contributions_count=0)
@@ -80,15 +76,11 @@ def test_handle_with_default_offset(
8076
assert mock_user.bulk_save.call_count == 2
8177
assert mock_user.bulk_save.call_args_list[-1][0][0] == [mock_user1, mock_user2, mock_user3]
8278

83-
@patch("apps.github.management.commands.github_update_users.MemberProfile")
8479
@patch("apps.github.management.commands.github_update_users.User")
8580
@patch("apps.github.management.commands.github_update_users.RepositoryContributor")
8681
@patch("apps.github.management.commands.github_update_users.BATCH_SIZE", 2)
87-
def test_handle_with_custom_offset(
88-
self, mock_repository_contributor, mock_user, mock_member_profile
89-
):
82+
def test_handle_with_custom_offset(self, mock_repository_contributor, mock_user):
9083
"""Test command execution with custom offset."""
91-
mock_member_profile.objects.get_or_create.return_value = (MagicMock(), False)
9284
mock_user1 = MagicMock(id=2, title="User 2", contributions_count=0)
9385
mock_user2 = MagicMock(id=3, title="User 3", contributions_count=0)
9486

@@ -123,15 +115,13 @@ def test_handle_with_custom_offset(
123115
assert mock_user.bulk_save.call_count == 2
124116
assert mock_user.bulk_save.call_args_list[-1][0][0] == [mock_user1, mock_user2]
125117

126-
@patch("apps.github.management.commands.github_update_users.MemberProfile")
127118
@patch("apps.github.management.commands.github_update_users.User")
128119
@patch("apps.github.management.commands.github_update_users.RepositoryContributor")
129120
@patch("apps.github.management.commands.github_update_users.BATCH_SIZE", 3)
130121
def test_handle_with_users_having_no_contributions(
131-
self, mock_repository_contributor, mock_user, mock_member_profile
122+
self, mock_repository_contributor, mock_user
132123
):
133124
"""Test command execution when users have no contributions."""
134-
mock_member_profile.objects.get_or_create.return_value = (MagicMock(), False)
135125
mock_user1 = MagicMock(id=1, title="User 1", contributions_count=0)
136126
mock_user2 = MagicMock(id=2, title="User 2", contributions_count=0)
137127

@@ -159,15 +149,11 @@ def test_handle_with_users_having_no_contributions(
159149
assert mock_user.bulk_save.call_count == 1
160150
assert mock_user.bulk_save.call_args_list[-1][0][0] == [mock_user1, mock_user2]
161151

162-
@patch("apps.github.management.commands.github_update_users.MemberProfile")
163152
@patch("apps.github.management.commands.github_update_users.User")
164153
@patch("apps.github.management.commands.github_update_users.RepositoryContributor")
165154
@patch("apps.github.management.commands.github_update_users.BATCH_SIZE", 1)
166-
def test_handle_with_single_user(
167-
self, mock_repository_contributor, mock_user, mock_member_profile
168-
):
155+
def test_handle_with_single_user(self, mock_repository_contributor, mock_user):
169156
"""Test command execution with single user."""
170-
mock_member_profile.objects.get_or_create.return_value = (MagicMock(), False)
171157
mock_user1 = MagicMock(id=1, title="User 1", contributions_count=0)
172158

173159
mock_users_queryset = MagicMock()
@@ -193,15 +179,11 @@ def test_handle_with_single_user(
193179
assert mock_user.bulk_save.call_count == 2
194180
assert mock_user.bulk_save.call_args_list[-1][0][0] == [mock_user1]
195181

196-
@patch("apps.github.management.commands.github_update_users.MemberProfile")
197182
@patch("apps.github.management.commands.github_update_users.User")
198183
@patch("apps.github.management.commands.github_update_users.RepositoryContributor")
199184
@patch("apps.github.management.commands.github_update_users.BATCH_SIZE", 2)
200-
def test_handle_with_empty_user_list(
201-
self, mock_repository_contributor, mock_user, mock_member_profile
202-
):
185+
def test_handle_with_empty_user_list(self, mock_repository_contributor, mock_user):
203186
"""Test command execution with no users."""
204-
mock_member_profile.objects.get_or_create.return_value = (MagicMock(), False)
205187
mock_users_queryset = MagicMock()
206188
mock_users_queryset.count.return_value = 0
207189
mock_users_queryset.__getitem__.return_value = []
@@ -220,18 +202,12 @@ def test_handle_with_empty_user_list(
220202

221203
assert mock_user.bulk_save.call_count == 1
222204
assert mock_user.bulk_save.call_args_list[-1][0][0] == []
223-
assert mock_member_profile.bulk_save.call_count == 1
224-
assert mock_member_profile.bulk_save.call_args_list[-1][0][0] == []
225205

226-
@patch("apps.github.management.commands.github_update_users.MemberProfile")
227206
@patch("apps.github.management.commands.github_update_users.User")
228207
@patch("apps.github.management.commands.github_update_users.RepositoryContributor")
229208
@patch("apps.github.management.commands.github_update_users.BATCH_SIZE", 2)
230-
def test_handle_with_exact_batch_size(
231-
self, mock_repository_contributor, mock_user, mock_member_profile
232-
):
209+
def test_handle_with_exact_batch_size(self, mock_repository_contributor, mock_user):
233210
"""Test command execution when user count equals batch size."""
234-
mock_member_profile.objects.get_or_create.return_value = (MagicMock(), False)
235211
mock_user1 = MagicMock(id=1, title="User 1", contributions_count=0)
236212
mock_user2 = MagicMock(id=2, title="User 2", contributions_count=0)
237213

@@ -261,41 +237,3 @@ def test_handle_with_exact_batch_size(
261237

262238
assert mock_user.bulk_save.call_count == 2
263239
assert mock_user.bulk_save.call_args_list[-1][0][0] == [mock_user1, mock_user2]
264-
265-
@patch("apps.github.management.commands.github_update_users.MemberProfile")
266-
@patch("apps.github.management.commands.github_update_users.User")
267-
@patch("apps.github.management.commands.github_update_users.RepositoryContributor")
268-
@patch("apps.github.management.commands.github_update_users.BATCH_SIZE", 2)
269-
def test_handle_member_profile_created(
270-
self, mock_repository_contributor, mock_user, mock_member_profile
271-
):
272-
"""Test command execution when MemberProfile is newly created."""
273-
mock_profile = MagicMock()
274-
mock_member_profile.objects.get_or_create.return_value = (mock_profile, True)
275-
276-
mock_user1 = MagicMock(
277-
id=1,
278-
contributions_count=0,
279-
is_owasp_staff=True,
280-
has_public_member_page=False,
281-
)
282-
283-
mock_users_queryset = MagicMock()
284-
mock_users_queryset.count.return_value = 1
285-
mock_users_queryset.__getitem__.return_value = [mock_user1]
286-
mock_user.objects.order_by.return_value = mock_users_queryset
287-
288-
mock_rc_objects = MagicMock()
289-
mock_rc_objects.exclude.return_value.values.return_value.annotate.return_value = [
290-
{"user_id": 1, "total_contributions": 5}
291-
]
292-
mock_repository_contributor.objects = mock_rc_objects
293-
294-
command = Command()
295-
command.handle(offset=0)
296-
297-
assert mock_profile.github_user == mock_user1
298-
assert mock_profile.contributions_count == 5
299-
assert mock_profile.is_owasp_staff is True
300-
assert mock_profile.has_public_member_page is False
301-
mock_member_profile.bulk_save.assert_called_once()

0 commit comments

Comments
 (0)