-
-
Notifications
You must be signed in to change notification settings - Fork 630
feat: add reusable search component with category filter and sorting #4199
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
Draft
HarshitVerma109
wants to merge
6
commits into
OWASP:main
Choose a base branch
from
HarshitVerma109:feat/reusable-search-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a45a799
feat: add reusable search component with category filter and sorting
HarshitVerma109 8e83c47
fix issues
HarshitVerma109 20adb6a
update code
HarshitVerma109 f4ec2aa
fix comments
HarshitVerma109 632b24c
fix comment
HarshitVerma109 9f3e3e4
fix: issue
HarshitVerma109 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
Some comments aren't visible on the classic Files Changed page.
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
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,20 @@ | ||
| """Strawberry Django filter definitions for the Project model.""" | ||
|
|
||
| import strawberry_django | ||
| from django.db.models import Q | ||
|
|
||
| from apps.owasp.models.enums.project import ProjectType | ||
| from apps.owasp.models.project import Project | ||
|
|
||
|
|
||
| @strawberry_django.filter_type(Project, lookups=True) | ||
| class ProjectFilter: | ||
| """Strawberry filter type enabling category-based project queries.""" | ||
|
|
||
| @strawberry_django.filter_field | ||
| def type(self, value: ProjectType, prefix: str): | ||
| """Narrow results to a specific project category (code, tool, etc.).""" | ||
| if not value: | ||
| return Q() | ||
| lookup = f"{prefix}type" if prefix else "type" | ||
| return Q(**{lookup: value}) |
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 @@ | ||
| """Strawberry Django ordering definitions for the Project model.""" | ||
|
|
||
| import strawberry | ||
| from strawberry_django import order_type | ||
|
|
||
| from apps.owasp.models.project import Project | ||
|
|
||
|
|
||
| @order_type(Project) | ||
| class ProjectOrder: | ||
| """Sortable fields exposed to the GraphQL schema for project listings.""" | ||
|
|
||
| contributors_count: strawberry.auto | ||
| created_at: strawberry.auto | ||
| forks_count: strawberry.auto | ||
| level: strawberry.auto | ||
| name: strawberry.auto | ||
| stars_count: strawberry.auto | ||
| updated_at: strawberry.auto |
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
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
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
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
47 changes: 47 additions & 0 deletions
47
backend/tests/apps/owasp/api/internal/filters/project_test.py
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 @@ | ||
| """Test cases for the ProjectFilter.""" | ||
|
|
||
| from django.db.models import Q | ||
|
|
||
| from apps.owasp.api.internal.filters.project import ProjectFilter | ||
| from apps.owasp.models.enums.project import ProjectType | ||
|
|
||
|
|
||
| class TestProjectFilter: | ||
| """Test cases for ProjectFilter class.""" | ||
|
|
||
| def test_filter_has_strawberry_definition(self): | ||
| """Check if ProjectFilter has valid Strawberry definition.""" | ||
| assert hasattr(ProjectFilter, "__strawberry_definition__") | ||
|
|
||
| def test_filter_fields(self): | ||
| """Test if the filter fields are correctly defined.""" | ||
| filter_fields = {field.name for field in ProjectFilter.__strawberry_definition__.fields} | ||
| assert "type" in filter_fields | ||
|
|
||
| def test_type_filter_with_valid_value(self): | ||
| """Test type filter returns Q object with condition when value is provided.""" | ||
| type_filter_method = ProjectFilter.__dict__["type"] | ||
| result = type_filter_method(None, value=ProjectType.CODE, prefix="") | ||
| assert isinstance(result, Q) | ||
| assert result == Q(type=ProjectType.CODE) | ||
|
|
||
| def test_type_filter_with_prefix(self): | ||
| """Test type filter uses prefix for nested lookups.""" | ||
| type_filter_method = ProjectFilter.__dict__["type"] | ||
| result = type_filter_method(None, value=ProjectType.CODE, prefix="project__") | ||
| assert isinstance(result, Q) | ||
| assert result == Q(project__type=ProjectType.CODE) | ||
|
|
||
| def test_type_filter_with_none_value(self): | ||
| """Test type filter returns empty Q() when value is None.""" | ||
| type_filter_method = ProjectFilter.__dict__["type"] | ||
| result = type_filter_method(None, value=None, prefix="") | ||
| assert isinstance(result, Q) | ||
| assert result == Q() | ||
|
|
||
| def test_type_filter_with_different_types(self): | ||
| """Test type filter works with various ProjectType values.""" | ||
| type_filter_method = ProjectFilter.__dict__["type"] | ||
| for project_type in [ProjectType.CODE, ProjectType.DOCUMENTATION, ProjectType.TOOL]: | ||
| result = type_filter_method(None, value=project_type, prefix="") | ||
| assert result == Q(type=project_type) |
Oops, something went wrong.
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.