-
-
Notifications
You must be signed in to change notification settings - Fork 313
Migrate User fields to MemberProfile (PR 1: Schema & Sync) #2686
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
7d33b9a
e78b731
e6fb0e2
c064284
607f50f
e895d6e
818b398
2316c6d
d145a04
e9e0535
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,10 @@ | |
| "avatar_url", | ||
| "bio", | ||
| "company", | ||
| "contributions_count", | ||
| "email", | ||
| "followers_count", | ||
| "following_count", | ||
| "id", | ||
| "is_owasp_staff", | ||
| "location", | ||
| "login", | ||
| "name", | ||
|
|
@@ -28,6 +26,13 @@ | |
| class UserNode: | ||
| """GitHub user node.""" | ||
|
|
||
| @strawberry.field | ||
| def contributions_count(self) -> int: | ||
| """Resolve contributions count.""" | ||
| if hasattr(self, "owasp_profile"): | ||
| return self.owasp_profile.contributions_count | ||
| return 0 | ||
|
|
||
| @strawberry.field | ||
| def badge_count(self) -> int: | ||
| """Resolve badge count.""" | ||
|
|
@@ -83,6 +88,13 @@ def is_gsoc_mentor(self) -> bool: | |
| return self.owasp_profile.is_gsoc_mentor | ||
| return False | ||
|
|
||
| @strawberry.field | ||
| def is_owasp_staff(self) -> bool: | ||
| """Resolve if the user is an OWASP staff member.""" | ||
| if hasattr(self, "owasp_profile"): | ||
| return self.owasp_profile.is_owasp_staff | ||
| return False | ||
|
|
||
|
Comment on lines
91
to
97
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. Return the old field. |
||
| @strawberry.field | ||
| def issues_count(self) -> int: | ||
| """Resolve issues count.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,4 +50,8 @@ def user( | |
| User or None: The user object if found, otherwise None. | ||
|
|
||
| """ | ||
| return User.objects.filter(has_public_member_page=True, login=login).first() | ||
| return ( | ||
| User.objects.select_related("owasp_profile") | ||
| .filter(owasp_profile__has_public_member_page=True, login=login) | ||
| .first() | ||
| ) | ||
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This command was originally made for updating the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you asked, I created a new member profile update in the owasp folder only updating contributions_count, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,7 +124,9 @@ def idx_contributions(self): | |
| @property | ||
| def idx_contributions_count(self) -> int: | ||
| """Return contributions count for indexing.""" | ||
| return self.contributions_count | ||
| if not hasattr(self, "owasp_profile"): | ||
| return 0 | ||
| return int(self.owasp_profile.contributions_count) | ||
|
||
|
|
||
| @property | ||
| def idx_issues(self) -> list[dict]: | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ def update_owasp_staff_badge(self): | |
|
|
||
| # Assign badge to employees who don't have it. | ||
| employees_without_badge = User.objects.filter( | ||
| is_owasp_staff=True, | ||
| owasp_profile__is_owasp_staff=True, | ||
| ).exclude( | ||
| user_badges__badge=badge, | ||
| ) | ||
|
|
@@ -60,7 +60,7 @@ def update_owasp_staff_badge(self): | |
|
|
||
| # Remove badge from non-OWASP employees. | ||
| non_employees = User.objects.filter( | ||
| is_owasp_staff=False, | ||
| owasp_profile__is_owasp_staff=False, | ||
| user_badges__badge=badge, | ||
| ).distinct() | ||
| removed_count = non_employees.count() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,11 @@ class HasDashboardAccess(BasePermission): | |
|
|
||
| def has_permission(self, source, info, **kwargs) -> bool: | ||
| """Check if the user has dashboard access.""" | ||
| user = info.context.request.user | ||
| if not (user and user.is_authenticated and user.github_user): | ||
| return False | ||
|
|
||
| return ( | ||
| (user := info.context.request.user) | ||
| and user.is_authenticated | ||
| and user.github_user.is_owasp_staff | ||
| hasattr(user.github_user, "owasp_profile") | ||
| and user.github_user.owasp_profile.is_owasp_staff | ||
|
||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,26 @@ | |
|
|
||
| from functools import wraps | ||
|
|
||
| from django.core.exceptions import ObjectDoesNotExist | ||
| from django.http import HttpResponseForbidden | ||
|
|
||
|
|
||
| def has_dashboard_permission(request): | ||
| """Check if user has dashboard access.""" | ||
| return (user := request.user) and user.is_authenticated and user.github_user.is_owasp_staff | ||
| user = getattr(request, "user", None) | ||
| if not (user and getattr(user, "is_authenticated", False)): | ||
| return False | ||
| try: | ||
| github_user = user.github_user | ||
| except ObjectDoesNotExist: | ||
| return False | ||
|
|
||
| try: | ||
| profile = github_user.owasp_profile | ||
| except ObjectDoesNotExist: | ||
| return False | ||
|
|
||
| return bool(getattr(profile, "is_owasp_staff", False)) | ||
|
||
|
|
||
|
|
||
| def dashboard_access_required(view_func): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Generated by Django 5.2.7 on 2025-11-18 17:59 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0065_memberprofile_linkedin_page_id"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="memberprofile", | ||
| name="contributions_count", | ||
| field=models.PositiveIntegerField(default=0, verbose_name="Contributions count"), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="memberprofile", | ||
| name="has_public_member_page", | ||
| field=models.BooleanField(default=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="memberprofile", | ||
| name="is_owasp_staff", | ||
| field=models.BooleanField( | ||
| default=False, | ||
| help_text="Indicates if the user is OWASP Foundation staff.", | ||
| verbose_name="Is OWASP Staff", | ||
| ), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Generated by Django 5.2.7 on 2025-11-18 18:04 | ||
|
|
||
| from django.db import migrations | ||
|
|
||
|
|
||
| def copy_user_data_to_member_profile(apps, _schema_editor): | ||
| """Copy user data to member profile.""" | ||
| User = apps.get_model("github", "User") | ||
| MemberProfile = apps.get_model("owasp", "MemberProfile") | ||
| profiles_to_update = [] | ||
| batch_size = 500 | ||
| update_fields = [ | ||
| "has_public_member_page", | ||
| "is_owasp_staff", | ||
| "contributions_count", | ||
| ] | ||
|
|
||
| for user in User.objects.all().iterator(chunk_size=batch_size): | ||
| profile, _ = MemberProfile.objects.get_or_create(github_user=user) | ||
| profile.has_public_member_page = user.has_public_member_page | ||
| profile.is_owasp_staff = user.is_owasp_staff | ||
| profile.contributions_count = user.contributions_count | ||
| profiles_to_update.append(profile) | ||
|
|
||
| if len(profiles_to_update) >= batch_size: | ||
| MemberProfile.objects.bulk_update( | ||
| profiles_to_update, update_fields, batch_size=batch_size | ||
| ) | ||
| profiles_to_update = [] | ||
|
|
||
| if profiles_to_update: | ||
| MemberProfile.objects.bulk_update(profiles_to_update, update_fields, batch_size=batch_size) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0066_memberprofile_contributions_count_and_more"), | ||
| ("github", "0039_remove_commit_commit_repo_created_idx"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(copy_user_data_to_member_profile, migrations.RunPython.noop), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Generated by Django 5.2.8 on 2025-11-26 14:23 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0067_memberprofile_backward_compatibility"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="memberprofile", | ||
| name="has_public_member_page", | ||
| field=models.BooleanField( | ||
| default=True, | ||
| help_text="Whether the member's profile is publicly visible on the OWASP website", | ||
| verbose_name="Has Public Member Page", | ||
| ), | ||
| ), | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should ensure backward compatibility. You should return the
Userold fieldcontribution_countif you found thatcontribution_countofowasp_profileis the default value i.e. 0.