Skip to content

Commit

Permalink
Created a static get_logins and updated User::is_indexable and User::…
Browse files Browse the repository at this point in the history
…get_queryset
  • Loading branch information
abhayymishraa committed Jan 9, 2025
1 parent adfa1e1 commit 7b4b142
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend/apps/github/index/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from apps.common.index import IndexBase
from apps.github.models.user import User
from apps.github.models.organization import Organization


@register(User)
Expand Down Expand Up @@ -63,7 +64,7 @@ class UserIndex(AlgoliaIndex, IndexBase):

def get_queryset(self):
"""Get queryset for indexing."""
return User.objects.all()
return User.objects.exclude(login__in=Organization.get_logins())

@staticmethod
def update_synonyms():
Expand Down
5 changes: 5 additions & 0 deletions backend/apps/github/models/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def from_github(self, gh_organization):
if value is not None:
setattr(self, model_field, value)

@staticmethod
def get_logins():
"""Retrieve all organization logins """
return list(Organization.objects.values_list("name", flat=True))

@staticmethod
def bulk_save(organizations):
"""Bulk save organizations."""
Expand Down
5 changes: 4 additions & 1 deletion backend/apps/github/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from apps.common.models import TimestampedModel
from apps.github.models.common import GenericUserModel, NodeModel
from apps.github.models.mixins.user import UserIndexMixin
from apps.github.models.organization import Organization


class User(NodeModel, GenericUserModel, TimestampedModel, UserIndexMixin):
Expand All @@ -25,7 +26,9 @@ def __str__(self):
@property
def is_indexable(self):
"""Users to index."""
return self.login != "ghost" # See https://github.com/ghost for more info.
return (
self.login != "ghost" and self.login not in Organization.get_logins()
) # See https://github.com/ghost for more info.

def from_github(self, gh_user):
"""Update instance based on GitHub user data."""
Expand Down
9 changes: 8 additions & 1 deletion backend/tests/github/api/organization_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from apps.github.api.organization import OrganizationSerializer

from apps.github.models.organization import Organization

class TestOrganizationSerializer:
@pytest.mark.parametrize(
Expand Down Expand Up @@ -45,3 +45,10 @@ def test_organization_serializer(self, mock_filter, organization_data):
validated_data["updated_at"].isoformat().replace("+00:00", "Z")
)
assert validated_data == organization_data

@patch("apps.github.models.organization.Organization.objects.values_list")
def test_get_logins(self, mock_values_list):
mock_values_list.return_value = ["github", "microsoft"]
logins = Organization.get_logins()
assert logins == ["github", "microsoft"]
mock_values_list.assert_called_once_with("name", flat=True)
17 changes: 17 additions & 0 deletions backend/tests/github/api/user_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest

from apps.github.api.user import UserSerializer
from apps.github.index.user import UserIndex
from apps.github.models.user import User


class TestUserSerializer:
Expand Down Expand Up @@ -46,3 +48,18 @@ def test_user_serializer(self, mock_filter, user_data):
validated_data["updated_at"].isoformat().replace("+00:00", "Z")
)
assert validated_data == user_data

@pytest.mark.parametrize(
"login, organization_logins, expected_result",
[
("johndoe", ["github", "microsoft"], True), # Normal user
("github", ["github", "microsoft"], False), # Organization login
("ghost", ["github", "microsoft"], False), # Special 'ghost' user
],
)
@patch("apps.github.models.organization.Organization.get_logins")
def test_is_indexable(self, mock_get_logins, login, organization_logins, expected_result):
mock_get_logins.return_value = organization_logins
user = User(login=login)
assert user.is_indexable == expected_result

0 comments on commit 7b4b142

Please sign in to comment.