-
-
Notifications
You must be signed in to change notification settings - Fork 656
Refactor badges update job: modularize handlers, introduce base class, and add tests #3040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2a6afd6
update nest badges code
mrkeshav-05 4115edc
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 a1a0d96
update tests in nest badge
mrkeshav-05 1e54fb8
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 e7132f6
apply coderabbit suggestions
mrkeshav-05 bcaaad1
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 0f253bc
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 374a602
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 5c51218
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 8e590fe
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 6cb3957
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 14a6dcf
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 822bed7
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 07759b0
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 7cd1fdc
Refactor badges with base command
mrkeshav-05 21e2374
resolve sonarcloud issues
mrkeshav-05 53d4443
Update code
arkid15r dffe527
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 26d10e9
apply bulk save and pluralize log messages
mrkeshav-05 a5dffd7
update code
mrkeshav-05 6b1ed18
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 1104ce2
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 79bc870
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 461f495
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 c3ab19c
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 0db9318
Merge branch 'main' into refactor/nest-badges
mrkeshav-05 cf3a1f7
Update code
arkid15r baf8898
Merge branch 'main' into refactor/nest-badges
arkid15r 67c1f71
Update code
arkid15r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """Badge handlers package for managing user badges.""" | ||
|
|
||
| from apps.nest.badges.project_leader_badge import OWASPProjectLeaderBadgeHandler | ||
| from apps.nest.badges.staff_badge import OWASPStaffBadgeHandler | ||
|
|
||
| __all__ = [ | ||
| "OWASPStaffBadgeHandler", | ||
| "OWASPProjectLeaderBadgeHandler", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import logging | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| from django.db.models import QuerySet | ||
|
|
||
| from apps.github.models.user import User | ||
| from apps.nest.models.badge import Badge | ||
| from apps.nest.models.user_badge import UserBadge | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class BaseBadgeHandler(ABC): | ||
| """Base class for handling badge updates.""" | ||
|
|
||
| name: str | ||
| description: str | ||
| css_class: str | ||
| weight: int | ||
|
|
||
| def __init__(self, stdout=None, style=None): | ||
| self.stdout = stdout | ||
| self.style = style | ||
|
|
||
| @abstractmethod | ||
| def get_eligible_users(self) -> QuerySet[User]: | ||
| """ | ||
| Return a QuerySet of users who should currently have this badge. | ||
| """ | ||
| pass | ||
|
|
||
| def get_badge_defaults(self) -> dict: | ||
| return { | ||
| "description": self.description, | ||
| "css_class": self.css_class, | ||
| "weight": self.weight, | ||
| } | ||
|
|
||
| def _log(self, message, style_func=None): | ||
| """Helper to log to both file logger and stdout if available.""" | ||
| logger.info(message) | ||
| if self.stdout: | ||
| if style_func: | ||
| message = style_func(message) | ||
| self.stdout.write(message) | ||
|
|
||
| def process(self): | ||
| """ | ||
| Main execution method to sync the badge. | ||
| 1. Creates/Updates the Badge definition. | ||
| 2. Assigns badge to eligible users. | ||
| 3. Revokes badge from ineligible users. | ||
| """ | ||
| if not self.name: | ||
| raise ValueError("Badge name must be defined.") | ||
|
|
||
| # 1. Get or Create the Badge | ||
| badge, created = Badge.objects.get_or_create( | ||
| name=self.name, | ||
| defaults=self.get_badge_defaults(), | ||
| ) | ||
|
|
||
| if created: | ||
| self._log(f"Created badge: '{badge.name}'") | ||
|
|
||
| # 2. Assign Badge to Eligible Users | ||
| eligible_users_qs = self.get_eligible_users() | ||
|
|
||
| # Filter for users who are eligible but don't have the badge actively assigned | ||
| users_to_add = eligible_users_qs.exclude( | ||
| user_badges__badge=badge, | ||
| user_badges__is_active=True | ||
| ) | ||
|
|
||
| added_count = 0 | ||
| for user in users_to_add: | ||
| user_badge, _ = UserBadge.objects.get_or_create(user=user, badge=badge) | ||
| if not user_badge.is_active: | ||
| user_badge.is_active = True | ||
| user_badge.save(update_fields=["is_active"]) | ||
| added_count += 1 | ||
|
|
||
| self.stdout.write(f"Added '{self.name}' badge to {added_count} users") | ||
| if added_count: | ||
| self._log(f"Added '{self.name}' badge to {added_count} users") | ||
|
|
||
| # 3. Revoke Badge from Ineligible Users | ||
| # Users who have the badge active, but are NOT in the eligible queryset | ||
| users_to_revoke = UserBadge.objects.filter( | ||
| badge=badge, | ||
| is_active=True | ||
| ).exclude(user__in=eligible_users_qs) | ||
|
|
||
| revoked_count = users_to_revoke.count() | ||
| if revoked_count: | ||
| users_to_revoke.update(is_active=False) | ||
| self._log(f"Removed '{self.name}' badge from {revoked_count} users") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Handler for the OWASP Project Leader badge. | ||
|
|
||
| This module manages the assignment and revocation of the OWASP Project Leader badge | ||
| based on users' leadership roles in OWASP projects. | ||
| """ | ||
|
|
||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.db.models import QuerySet | ||
|
|
||
| from apps.github.models.user import User | ||
| from apps.nest.badges.base import BaseBadgeHandler | ||
| from apps.owasp.models.entity_member import EntityMember | ||
| from apps.owasp.models.project import Project | ||
|
|
||
|
|
||
| class OWASPProjectLeaderBadgeHandler(BaseBadgeHandler): | ||
| """Handler for managing the OWASP Project Leader badge. | ||
|
|
||
| This badge is awarded to users who are active and reviewed leaders | ||
| of OWASP projects. It uses the EntityMember model for users with the LEADER role. | ||
| """ | ||
|
|
||
| name = "OWASP Project Leader" | ||
| description = "Official OWASP Project Leader" | ||
| css_class = "fa-user-shield" | ||
| weight = 90 | ||
|
|
||
| def get_eligible_users(self) -> QuerySet[User]: | ||
| """ | ||
| Get all users who should have the Project Leader badge. | ||
|
|
||
| A user is eligible if they are an active, reviewed leader of at least | ||
| one OWASP project. | ||
|
|
||
| Returns: | ||
| QuerySet of users who are project leaders. | ||
| """ | ||
|
|
||
| # Get IDs of users who are active and reviewed project leaders | ||
| leader_ids = EntityMember.objects.filter( | ||
| entity_type=ContentType.objects.get_for_model(Project), | ||
| role=EntityMember.Role.LEADER, | ||
| is_active=True, | ||
| is_reviewed=True, | ||
| member__isnull=False, | ||
| ).values_list("member_id", flat=True) | ||
| return User.objects.filter(id__in=leader_ids).distinct() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Handler for the OWASP Staff badge. | ||
|
|
||
| This module manages the assignment and revocation of the OWASP Staff badge | ||
| based on users' staff status. | ||
| """ | ||
|
|
||
| from django.db.models import QuerySet | ||
|
|
||
| from apps.github.models.user import User | ||
| from apps.nest.badges.base import BaseBadgeHandler | ||
|
|
||
|
|
||
| class OWASPStaffBadgeHandler(BaseBadgeHandler): | ||
| """Handler for managing the OWASP Staff badge.""" | ||
|
|
||
| name = "OWASP Staff" | ||
| description = "Official OWASP Staff" | ||
| css_class = "fa-user-shield" | ||
| weight = 100 | ||
|
|
||
| def get_eligible_users(self) -> QuerySet[User]: | ||
| """ | ||
| Get all users who should have the OWASP Staff badge. | ||
|
|
||
| Returns: | ||
| QuerySet of users with is_owasp_staff=True. | ||
| """ | ||
| return User.objects.filter(is_owasp_staff=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.