This repository was archived by the owner on Sep 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 631
Consolidates restricted visibility-based filtering #995
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
38638f0
Consolidates restricted visibility-based filtering
mvilanova 207dfcd
Merge branch 'master' into feature/restricted-incidents-view
mvilanova 7e73d9d
Improvements
mvilanova a29d0b3
Renames base.py to core.py
mvilanova ee63ce7
Removes unused imports
mvilanova 0e93122
Adds restricted filtering for incident types
mvilanova 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
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
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import re | ||
| import functools | ||
| from typing import Any | ||
|
|
||
| from sqlalchemy import create_engine | ||
| from sqlalchemy.ext.declarative import declarative_base, declared_attr | ||
| from sqlalchemy.orm import sessionmaker | ||
| from sqlalchemy_searchable import make_searchable | ||
| from starlette.requests import Request | ||
|
|
||
| from dispatch.config import SQLALCHEMY_DATABASE_URI | ||
|
|
||
|
|
||
| engine = create_engine(str(SQLALCHEMY_DATABASE_URI)) | ||
| SessionLocal = sessionmaker(bind=engine) | ||
|
|
||
|
|
||
| def resolve_table_name(name): | ||
| """Resolves table names to their mapped names.""" | ||
| names = re.split("(?=[A-Z])", name) # noqa | ||
| return "_".join([x.lower() for x in names if x]) | ||
|
|
||
|
|
||
| raise_attribute_error = object() | ||
|
|
||
|
|
||
| def resolve_attr(obj, attr, default=None): | ||
| """Attempts to access attr via dotted notation, returns none if attr does not exist.""" | ||
| try: | ||
| return functools.reduce(getattr, attr.split("."), obj) | ||
| except AttributeError: | ||
| return default | ||
|
|
||
|
|
||
| class CustomBase: | ||
| @declared_attr | ||
| def __tablename__(self): | ||
| return resolve_table_name(self.__name__) | ||
|
|
||
|
|
||
| Base = declarative_base(cls=CustomBase) | ||
| make_searchable(Base.metadata) | ||
|
|
||
|
|
||
| def get_db(request: Request): | ||
| return request.state.db | ||
|
|
||
|
|
||
| def get_model_name_by_tablename(table_fullname: str) -> str: | ||
| """Returns the model name of a given table.""" | ||
| return get_class_by_tablename(table_fullname=table_fullname).__name__ | ||
|
|
||
|
|
||
| def get_class_by_tablename(table_fullname: str) -> Any: | ||
| """Return class reference mapped to table.""" | ||
| mapped_name = resolve_table_name(table_fullname) | ||
| for c in Base._decl_class_registry.values(): | ||
| if hasattr(c, "__table__"): | ||
| if c.__table__.fullname.lower() == mapped_name.lower(): | ||
| return c | ||
| raise Exception(f"Incorrect tablename '{mapped_name}'. Check the name of your model.") | ||
|
|
||
|
|
||
| def get_table_name_by_class_instance(class_instance: Base) -> str: | ||
| """Returns the name of the table for a given class instance.""" | ||
| return class_instance._sa_instance_state.mapper.mapped_table.name |
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
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.
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.
Do we need to do something similar for tasks for restricted incidents?
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.
I was going to add logic for it, but when I started testing tasks I realized they were already being filtered. I did some research to try to figure out the why and the conclusion that I reached was that when we load
/taskswe perform a GET against the/api/v1/incidents/endpoint, which returns a list of filtered incidents, and then sqlalchemy filters out the tasks based on that list.