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 3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,81 +1,26 @@ | ||
| import re | ||
| import logging | ||
| import json | ||
| from typing import Any, List | ||
| from itertools import groupby | ||
| import functools | ||
|
|
||
| from sqlalchemy import create_engine | ||
| from sqlalchemy.ext.declarative import declarative_base, declared_attr | ||
| from typing import Any, List | ||
|
|
||
| from sqlalchemy import and_, not_ | ||
| from sqlalchemy.orm import Query, sessionmaker | ||
| from sqlalchemy_filters import apply_pagination, apply_sort, apply_filters | ||
| from sqlalchemy_searchable import make_searchable | ||
| from sqlalchemy_searchable import search as search_db | ||
| from starlette.requests import Request | ||
|
|
||
| from dispatch.common.utils.composite_search import CompositeSearch | ||
| from dispatch.enums import Visibility, UserRoles | ||
| from dispatch.incident.models import Incident | ||
| from dispatch.individual.models import IndividualContact | ||
| from dispatch.participant.models import Participant | ||
| from dispatch.task.models import Task | ||
|
|
||
| from .base import Base, get_class_by_tablename, get_model_name_by_tablename | ||
|
|
||
| from .config import SQLALCHEMY_DATABASE_URI | ||
|
|
||
| log = logging.getLogger(__file__) | ||
|
|
||
| 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 | ||
|
|
||
|
|
||
| def paginate(query: Query, page: int, items_per_page: int): | ||
| # Never pass a negative OFFSET value to SQL. | ||
|
|
@@ -98,7 +43,7 @@ def search(*, db_session, query_str: str, model: str, sort=False): | |
| return search_db(q, query_str, sort=sort) | ||
|
|
||
|
|
||
| def create_filter_spec(model, fields, ops, values, user_role): | ||
| def create_filter_spec(model, fields, ops, values): | ||
| """Creates a filter spec.""" | ||
| filters = [] | ||
|
|
||
|
|
@@ -133,23 +78,8 @@ def create_filter_spec(model, fields, ops, values, user_role): | |
| else: | ||
| filter_spec.append({"or": filters}) | ||
|
|
||
| # add admin only filter | ||
| if user_role != UserRoles.admin: | ||
| # add support for filtering restricted incidents | ||
| if model.lower() in ["incident", "task"]: | ||
| filter_spec.append( | ||
| { | ||
| "model": "Incident", | ||
| "field": "visibility", | ||
| "op": "!=", | ||
| "value": Visibility.restricted, | ||
| } | ||
| ) | ||
|
|
||
| if filter_spec: | ||
| filter_spec = {"and": filter_spec} | ||
|
|
||
| log.debug(f"Filter Spec: {json.dumps(filter_spec, indent=2)}") | ||
|
|
||
| return filter_spec | ||
|
|
||
|
|
||
|
|
@@ -210,9 +140,10 @@ def search_filter_sort_paginate( | |
| ops: List[str] = None, | ||
| values: List[str] = None, | ||
| join_attrs: List[str] = None, | ||
| user_role: UserRoles = UserRoles.user, | ||
| user_role: UserRoles = UserRoles.user.value, | ||
| user_email: str = None, | ||
| ): | ||
| """Common functionality for searching, filtering and sorting""" | ||
| """Common functionality for searching, filtering, sorting, and pagination.""" | ||
| model_cls = get_class_by_tablename(model) | ||
| sort_spec = create_sort_spec(model, sort_by, descending) | ||
|
|
||
|
|
@@ -222,10 +153,26 @@ def search_filter_sort_paginate( | |
| else: | ||
| query = db_session.query(model_cls) | ||
|
|
||
| if user_role != UserRoles.admin.value: | ||
| if model.lower() == "incident": | ||
|
Contributor
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. Do we need to do something similar for tasks for restricted incidents?
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. 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 |
||
| # we filter restricted incidents based on incident participation | ||
| query = ( | ||
| query.join(Participant) | ||
| .join(IndividualContact) | ||
| .filter( | ||
| not_( | ||
| and_( | ||
| Incident.visibility == Visibility.restricted.value, | ||
| IndividualContact.email != user_email, | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| query = join_required_attrs(query, model_cls, join_attrs, fields, sort_by) | ||
|
|
||
| if not filter_spec: | ||
| filter_spec = create_filter_spec(model, fields, ops, values, user_role) | ||
| filter_spec = create_filter_spec(model, fields, ops, values) | ||
|
|
||
| query = apply_filters(query, filter_spec) | ||
| query = apply_sort(query, sort_spec) | ||
|
|
@@ -234,6 +181,7 @@ def search_filter_sort_paginate( | |
| items_per_page = None | ||
|
|
||
| query, pagination = apply_pagination(query, page_number=page, page_size=items_per_page) | ||
|
|
||
| return { | ||
| "items": query.all(), | ||
| "itemsPerPage": pagination.page_size, | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.