-
Notifications
You must be signed in to change notification settings - Fork 0
Wip better search #24
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bf7beb1
wip: better search
garland3 a2ee926
fixed some problems on search
garland3 fdd6e8e
Potential fix for code scanning alert no. 1481: Unused import
garland3 80b888d
bot fixes
garland3 d0092e0
tests
garland3 a39adda
tests
garland3 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import uuid | ||
from sqlalchemy import select, update, delete, and_ | ||
import re | ||
from sqlalchemy import select, update, delete, and_, text, or_, cast, String | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.orm import selectinload | ||
from core import models, schemas | ||
|
@@ -155,18 +156,39 @@ | |
""" | ||
return await get_data_instance(db, image_id) | ||
|
||
async def get_data_instances_for_project(db: AsyncSession, project_id: uuid.UUID, skip: int = 0, limit: int = 100) -> List[models.DataInstance]: | ||
async def get_data_instances_for_project(db: AsyncSession, project_id: uuid.UUID, skip: int = 0, limit: int = 100, search_field: Optional[str] = None, search_value: Optional[str] = None) -> List[models.DataInstance]: | ||
# First check if the project exists | ||
project = await get_project(db, project_id) | ||
if not project: | ||
return [] | ||
|
||
result = await db.execute( | ||
select(models.DataInstance) | ||
.where(models.DataInstance.project_id == project_id) | ||
.offset(skip) | ||
.limit(limit) | ||
) | ||
query = select(models.DataInstance).where(models.DataInstance.project_id == project_id) | ||
|
||
if search_field and search_value: | ||
search_value_lower = f"%{search_value.lower()}%" | ||
|
||
if search_field == 'filename': | ||
query = query.where(models.DataInstance.filename.ilike(search_value_lower)) | ||
elif search_field == 'content_type': | ||
query = query.where(models.DataInstance.content_type.ilike(search_value_lower)) | ||
elif search_field == 'uploaded_by': | ||
query = query.where(models.DataInstance.uploaded_by_user_id.ilike(search_value_lower)) | ||
elif search_field == 'metadata': | ||
# Search across all metadata values using safe SQLAlchemy cast | ||
query = query.where(cast(models.DataInstance.metadata_, String).ilike(search_value_lower)) | ||
else: | ||
# Search specific metadata key using JSON path with input validation | ||
# Only allow alphanumeric characters, underscores, and hyphens for security | ||
if re.match(r'^[a-zA-Z0-9_-]+$', search_field): | ||
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. [nitpick] The regex pattern for validating metadata keys is a magic string that could benefit from being defined as a constant at the module level with a descriptive name like Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
# Use SQLAlchemy's JSON path operator safely | ||
query = query.where(models.DataInstance.metadata_[search_field].astext.ilike(search_value_lower)) | ||
else: | ||
# Invalid key format, skip filtering for security | ||
safe_search_field = search_field.replace('\n', '').replace('\r', '') if search_field else 'None' | ||
logger.warning(f"Invalid metadata key format rejected: {safe_search_field}") | ||
|
||
query = query.offset(skip).limit(limit) | ||
result = await db.execute(query) | ||
return result.scalars().all() | ||
|
||
async def get_deleted_images_for_project(db: AsyncSession, project_id: uuid.UUID, skip: int = 0, limit: int = 100) -> List[models.DataInstance]: | ||
|
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
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.
Check notice
Code scanning / CodeQL
Unused import Note
Copilot Autofix
AI about 1 month ago
The best way to fix the problem is to remove the unnecessary
text
import from the import statement on line 3 inbackend/utils/crud.py
. Keep all other imported symbols intact, as they may be used elsewhere in the code. The change should only modify the import line, specifically deletingtext
from the list of symbols imported fromsqlalchemy
. No other changes are required to existing functionality—just a precise edit to the import.