Skip to content

Commit

Permalink
function to retrieve non-GVL systems
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsachs committed Nov 29, 2023
1 parent 1c500ab commit d135a11
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
56 changes: 56 additions & 0 deletions src/fides/api/util/tcf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from typing import List

from loguru import logger
from sqlalchemy import not_, or_
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.sql import Select

from fides.api.models.sql_models import System
from fides.api.util import errors

AC_PREFIX = "gacp."
GVL_PREFIX = "gvl."


def exclude_gvl_systems(query: Select):
"""Utility function to add a query clause that excludes GVL systems"""
return query.where(

Check warning on line 19 in src/fides/api/util/tcf/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/util/tcf/__init__.py#L19

Added line #L19 was not covered by tests
or_(System.vendor_id == None, not_(System.vendor_id.startswith(GVL_PREFIX)))
)


def exclude_ac_systems(query: Select):
"""Utility function to add a query clause that excludes AC systems"""
return query.where(

Check warning on line 26 in src/fides/api/util/tcf/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/util/tcf/__init__.py#L26

Added line #L26 was not covered by tests
or_(System.vendor_id == None, not_(System.vendor_id.startswith(AC_PREFIX)))
)


async def list_non_tcf_systems(
async_session: AsyncSession, exclude_gvl: bool = True, exclude_ac: bool = True
) -> List[System]:
"""
Utility to retrieve all Systems that are not GVL systems using
the provided (async) DB session.
"""
with logger.contextualize(sql_model="System"):
async with async_session.begin():
try:
logger.debug("Fetching non TCF systems")
query = select(System)

Check warning on line 42 in src/fides/api/util/tcf/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/util/tcf/__init__.py#L40-L42

Added lines #L40 - L42 were not covered by tests
if exclude_gvl:
query = exclude_gvl_systems(query)

Check warning on line 44 in src/fides/api/util/tcf/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/util/tcf/__init__.py#L44

Added line #L44 was not covered by tests
if exclude_ac:
query = exclude_ac_systems(query)
result = await async_session.execute(query)
sql_resources = result.scalars().all()
except SQLAlchemyError:
error = errors.QueryError()
logger.bind(error=error.detail["error"]).info( # type: ignore[index]

Check warning on line 51 in src/fides/api/util/tcf/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/util/tcf/__init__.py#L46-L51

Added lines #L46 - L51 were not covered by tests
"Failed to fetch non TCF systems"
)
raise error

Check warning on line 54 in src/fides/api/util/tcf/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/util/tcf/__init__.py#L54

Added line #L54 was not covered by tests

return sql_resources

Check warning on line 56 in src/fides/api/util/tcf/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/util/tcf/__init__.py#L56

Added line #L56 was not covered by tests
4 changes: 1 addition & 3 deletions src/fides/api/util/tcf/tcf_experience_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
TCFVendorLegitimateInterestsRecord,
TCFVendorRelationships,
)
from fides.api.util.tcf import AC_PREFIX
from fides.config import CONFIG

AC_PREFIX = "gacp."
GVL_PREFIX = "gvl."

PURPOSE_DATA_USES: List[str] = []
for purpose in MAPPED_PURPOSES.values():
PURPOSE_DATA_USES.extend(purpose.data_uses)
Expand Down

0 comments on commit d135a11

Please sign in to comment.