-
-
Notifications
You must be signed in to change notification settings - Fork 270
Fix #1785 Refactor owasp app admin.py into separate files #1909
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
9 commits
Select commit
Hold shift + click to select a range
9871aa6
Refactor owasp app admin.py into separate files
anurag2787 16f702d
eliminate duplicated code
anurag2787 cb36050
Merge branch 'main' into refactor/admin.py
anurag2787 2d6b3ca
fixed code checks
anurag2787 ba4aff9
add checks for missing values
anurag2787 8a013df
Merge branch 'main' into refactor/admin.py
anurag2787 4c677ff
Merge branch 'main' into refactor/admin.py
anurag2787 aaf0812
Update code
arkid15r 863483f
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,14 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.project_health_requirements import ProjectHealthRequirements | ||
|
|
||
| from .chapter import ChapterAdmin | ||
| from .committee import CommitteeAdmin | ||
| from .event import EventAdmin | ||
| from .post import PostAdmin | ||
| from .project import ProjectAdmin | ||
| from .project_health_metrics import ProjectHealthMetricsAdmin | ||
| from .snapshot import SnapshotAdmin | ||
| from .sponsor import SponsorAdmin | ||
|
|
||
| admin.site.register(ProjectHealthRequirements) |
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,30 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.chapter import Chapter | ||
|
|
||
| from .mixins import GenericEntityAdminMixin, LeaderAdminMixin | ||
|
|
||
|
|
||
| class ChapterAdmin(admin.ModelAdmin, GenericEntityAdminMixin, LeaderAdminMixin): | ||
| """Admin for Chapter model.""" | ||
|
|
||
| autocomplete_fields = ("owasp_repository",) | ||
| filter_horizontal = LeaderAdminMixin.filter_horizontal | ||
| list_display = ( | ||
| "name", | ||
| "created_at", | ||
| "updated_at", | ||
| "region", | ||
| "custom_field_owasp_url", | ||
| "custom_field_github_urls", | ||
| ) | ||
| list_filter = ( | ||
| "is_active", | ||
| "is_leaders_policy_compliant", | ||
| "country", | ||
| "region", | ||
| ) | ||
| search_fields = ("name", "key") | ||
|
|
||
|
|
||
| admin.site.register(Chapter, ChapterAdmin) |
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,19 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.committee import Committee | ||
|
|
||
| from .mixins import GenericEntityAdminMixin, LeaderAdminMixin | ||
|
|
||
|
|
||
| class CommitteeAdmin(admin.ModelAdmin, GenericEntityAdminMixin, LeaderAdminMixin): | ||
| """Admin for Committee model.""" | ||
|
|
||
| autocomplete_fields = ( | ||
| "leaders", | ||
| "owasp_repository", | ||
| ) | ||
| filter_horizontal = LeaderAdminMixin.filter_horizontal | ||
| search_fields = ("name",) | ||
|
|
||
|
|
||
| admin.site.register(Committee, CommitteeAdmin) |
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,16 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.event import Event | ||
|
|
||
|
|
||
| class EventAdmin(admin.ModelAdmin): | ||
| """Admin for Event model.""" | ||
|
|
||
| list_display = ( | ||
| "name", | ||
| "suggested_location", | ||
| ) | ||
| search_fields = ("name",) | ||
|
|
||
|
|
||
| admin.site.register(Event, EventAdmin) |
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,58 @@ | ||
| from django.contrib import admin, messages | ||
| from django.utils.safestring import mark_safe | ||
|
|
||
|
|
||
| class GenericEntityAdminMixin: | ||
| """Mixin for generic entity admin.""" | ||
|
|
||
| def get_queryset(self, request): | ||
| """Get queryset.""" | ||
| return super().get_queryset(request).prefetch_related("repositories") | ||
|
|
||
| def custom_field_github_urls(self, obj): | ||
| """Entity GitHub URLs.""" | ||
| if not hasattr(obj, "repositories"): | ||
| return mark_safe( # noqa: S308 | ||
| f"<a href='https://github.com/{obj.owasp_repository.owner.login}/" | ||
| f"{obj.owasp_repository.key}' target='_blank'>↗️</a>" | ||
| ) | ||
|
|
||
| urls = [ | ||
| f"<a href='https://github.com/{repository.owner.login}/" | ||
| f"{repository.key}' target='_blank'>↗️</a>" | ||
| for repository in obj.repositories.all() | ||
| ] | ||
|
|
||
| return mark_safe(" ".join(urls)) # noqa: S308 | ||
|
|
||
| def custom_field_owasp_url(self, obj): | ||
| """Entity OWASP URL.""" | ||
| return mark_safe( # noqa: S308 | ||
| f"<a href='https://owasp.org/{obj.key}' target='_blank'>↗️</a>" | ||
| ) | ||
|
|
||
| custom_field_github_urls.short_description = "GitHub 🔗" | ||
| custom_field_owasp_url.short_description = "OWASP 🔗" | ||
|
|
||
|
|
||
| class LeaderAdminMixin: | ||
| """Admin mixin for entities that can have leaders.""" | ||
|
|
||
| actions = ("approve_suggested_leaders",) | ||
| filter_horizontal = ( | ||
| "leaders", | ||
| "suggested_leaders", | ||
| ) | ||
|
|
||
| def approve_suggested_leaders(self, request, queryset): | ||
| """Approve suggested leaders for selected entities.""" | ||
| for entity in queryset: | ||
| suggestions = entity.suggested_leaders.all() | ||
| entity.leaders.add(*suggestions) | ||
| self.message_user( | ||
| request, | ||
| f"Approved {suggestions.count()} leader suggestions for {entity.name}", | ||
| messages.SUCCESS, | ||
| ) | ||
|
|
||
| approve_suggested_leaders.short_description = "Approve suggested leaders" | ||
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,23 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.post import Post | ||
|
|
||
|
|
||
| class PostAdmin(admin.ModelAdmin): | ||
| """Admin configuration for Post model.""" | ||
|
|
||
| list_display = ( | ||
| "author_name", | ||
| "published_at", | ||
| "title", | ||
| ) | ||
| search_fields = ( | ||
| "author_image_url", | ||
| "author_name", | ||
| "published_at", | ||
| "title", | ||
| "url", | ||
| ) | ||
|
|
||
|
|
||
| admin.site.register(Post, PostAdmin) |
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,54 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.project import Project | ||
|
|
||
| from .mixins import GenericEntityAdminMixin, LeaderAdminMixin | ||
|
|
||
|
|
||
| class ProjectAdmin(admin.ModelAdmin, GenericEntityAdminMixin, LeaderAdminMixin): | ||
| """Admin for Project model.""" | ||
|
|
||
| autocomplete_fields = ( | ||
| "organizations", | ||
| "owasp_repository", | ||
| "owners", | ||
| "repositories", | ||
| ) | ||
| filter_horizontal = LeaderAdminMixin.filter_horizontal | ||
| list_display = ( | ||
| "custom_field_name", | ||
| "created_at", | ||
| "updated_at", | ||
| "stars_count", | ||
| "forks_count", | ||
| "commits_count", | ||
| "releases_count", | ||
| "custom_field_owasp_url", | ||
| "custom_field_github_urls", | ||
| ) | ||
| list_filter = ( | ||
| "is_active", | ||
| "is_leaders_policy_compliant", | ||
| "has_active_repositories", | ||
| "level", | ||
| "type", | ||
| ) | ||
| ordering = ("-created_at",) | ||
| search_fields = ( | ||
| "custom_tags", | ||
| "description", | ||
| "key", | ||
| "languages", | ||
| "leaders_raw", | ||
| "name", | ||
| "topics", | ||
| ) | ||
|
|
||
| def custom_field_name(self, obj) -> str: | ||
| """Project custom name.""" | ||
| return f"{obj.name or obj.key}" | ||
|
|
||
| custom_field_name.short_description = "Name" | ||
|
|
||
|
|
||
| admin.site.register(Project, ProjectAdmin) |
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,32 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.project_health_metrics import ProjectHealthMetrics | ||
|
|
||
|
|
||
| class ProjectHealthMetricsAdmin(admin.ModelAdmin): | ||
| """Admin for ProjectHealthMetrics model.""" | ||
|
|
||
| autocomplete_fields = ("project",) | ||
| list_filter = ( | ||
| "project__level", | ||
| "nest_created_at", | ||
| ) | ||
| list_display = ( | ||
| "project", | ||
| "nest_created_at", | ||
| "score", | ||
| "contributors_count", | ||
| "stars_count", | ||
| "forks_count", | ||
| "open_issues_count", | ||
| "open_pull_requests_count", | ||
| "recent_releases_count", | ||
| ) | ||
| search_fields = ("project__name",) | ||
|
|
||
| def project(self, obj): | ||
| """Display project name.""" | ||
| return obj.project.name if obj.project else "N/A" | ||
|
|
||
|
|
||
| admin.site.register(ProjectHealthMetrics, ProjectHealthMetricsAdmin) |
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,38 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.snapshot import Snapshot | ||
|
|
||
|
|
||
| class SnapshotAdmin(admin.ModelAdmin): | ||
| """Admin for Snapshot model.""" | ||
|
|
||
| autocomplete_fields = ( | ||
| "new_chapters", | ||
| "new_issues", | ||
| "new_projects", | ||
| "new_releases", | ||
| "new_users", | ||
| ) | ||
| list_display = ( | ||
| "title", | ||
| "start_at", | ||
| "end_at", | ||
| "status", | ||
| "created_at", | ||
| "updated_at", | ||
| ) | ||
| list_filter = ( | ||
| "status", | ||
| "start_at", | ||
| "end_at", | ||
| ) | ||
| ordering = ("-start_at",) | ||
| search_fields = ( | ||
| "title", | ||
| "key", | ||
| "status", | ||
| "error_message", | ||
| ) | ||
|
|
||
|
|
||
| admin.site.register(Snapshot, SnapshotAdmin) |
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,36 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.sponsor import Sponsor | ||
|
|
||
|
|
||
| class SponsorAdmin(admin.ModelAdmin): | ||
| """Admin for Sponsor model.""" | ||
|
|
||
| list_display = ( | ||
| "name", | ||
| "sort_name", | ||
| "sponsor_type", | ||
| "is_member", | ||
| "member_type", | ||
| ) | ||
|
|
||
| search_fields = ( | ||
| "name", | ||
| "sort_name", | ||
| "description", | ||
| ) | ||
|
|
||
| list_filter = ( | ||
| "sponsor_type", | ||
| "is_member", | ||
| "member_type", | ||
| ) | ||
|
|
||
| fieldsets = ( | ||
| ("Basic Information", {"fields": ("name", "sort_name", "description")}), | ||
| ("URLs and Images", {"fields": ("url", "job_url", "image_url")}), | ||
| ("Status", {"fields": ("is_member", "member_type", "sponsor_type")}), | ||
| ) | ||
|
|
||
|
|
||
| admin.site.register(Sponsor, SponsorAdmin) |
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.