Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andersy005 committed Sep 30, 2023
1 parent bda1b9c commit b034c4b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 85 deletions.
57 changes: 14 additions & 43 deletions carbonplan_offsets_db/routers/credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,22 @@ def get_credits(
Project, Credit.project_id == Project.project_id, isouter=True
)

# Filter for project_id
if project_id:
query = apply_filters(
query=query, model=Credit, attribute='project_id', values=project_id, operation='=='
)

# Filters applying 'ilike' operation
ilike_filters = [
filters = [
('registry', registry, 'ilike', Project),
('transaction_type', transaction_type, 'ilike', Credit),
]

for attribute, values, operation, model in ilike_filters:
query = apply_filters(
query=query, model=model, attribute=attribute, values=values, operation=operation
)

list_attributes = [('category', category, 'ANY', Project)]
for attribute, values, operation, model in list_attributes:
query = apply_filters(
query=query, model=model, attribute=attribute, values=values, operation=operation
)

# Filter applying '==' operation
equal_filters = [('is_arb', is_arb, '==', Project), ('vintage', vintage, '==', Credit)]

for attribute, values, operation, model in equal_filters:
query = apply_filters(
query=query, model=model, attribute=attribute, values=values, operation=operation
)

# Filters applying '>=' or '<=' operations
date_filters = [
('category', category, 'ANY', Project),
('is_arb', is_arb, '==', Project),
('vintage', vintage, '==', Credit),
('transaction_date', transaction_date_from, '>=', Credit),
('transaction_date', transaction_date_to, '<=', Credit),
]

for attribute, values, operation, model in date_filters:
# Filter for project_id
if project_id:
# insert at the beginning of the list to ensure that it is applied first
filters.insert(0, ('project_id', project_id, '==', Project))

for attribute, values, operation, model in filters:
query = apply_filters(
query=query, model=model, attribute=attribute, values=values, operation=operation
)
Expand Down Expand Up @@ -142,21 +120,14 @@ def get_credit_stats(

query = session.query(CreditStats)

# Filters applying 'ilike' operation
ilike_filters = [
filters = [
('registry', registry, 'ilike', CreditStats),
('transaction_type', transaction_type, 'ilike', CreditStats),
('date', date_from, '>=', CreditStats),
('date', date_to, '<=', CreditStats),
]

for attribute, values, operation, model in ilike_filters:
query = apply_filters(
query=query, model=model, attribute=attribute, values=values, operation=operation
)

# Filters applying '>=' or '<=' operations
date_filters = [('date', date_from, '>=', CreditStats), ('date', date_to, '<=', CreditStats)]

for attribute, values, operation, model in date_filters:
for attribute, values, operation, model in filters:
query = apply_filters(
query=query, model=model, attribute=attribute, values=values, operation=operation
)
Expand Down
63 changes: 21 additions & 42 deletions carbonplan_offsets_db/routers/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,25 @@ def get_projects(

query = session.query(Project)

# Filters applying 'ilike' operation
filterable_attributes = [
('registry', registry, 'ilike'),
('country', country, 'ilike'),
]

for attribute, values, operation in filterable_attributes:
query = apply_filters(
query=query, model=Project, attribute=attribute, values=values, operation=operation
)

list_attributes = [('protocol', protocol, 'ANY'), ('category', category, 'ANY')]
for attribute, values, operation in list_attributes:
query = apply_filters(
query=query, model=Project, attribute=attribute, values=values, operation=operation
)

# Filters applying '==', '>=', or '<=' operations
other_filters = [
filters = [
('registry', registry, 'ilike', Project),
('country', country, 'ilike', Project),
('protocol', protocol, 'ANY', Project),
('category', category, 'ANY', Project),
('is_arb', is_arb, '=='),
('registered_at', registered_at_from, '>='),
('registered_at', registered_at_to, '<='),
('started_at', started_at_from, '>='),
('started_at', started_at_to, '<='),
('issued', issued_min, '>='),
('issued', issued_max, '<='),
('retired', retired_min, '>='),
('retired', retired_max, '<='),
('registered_at', registered_at_from, '>=', Project),
('registered_at', registered_at_to, '<=', Project),
('started_at', started_at_from, '>=', Project),
('started_at', started_at_to, '<=', Project),
('issued', issued_min, '>=', Project),
('issued', issued_max, '<=', Project),
('retired', retired_min, '>=', Project),
('retired', retired_max, '<=', Project),
]

for attribute, values, operation in other_filters:
for attribute, values, operation, model in filters:
query = apply_filters(
query=query, model=Project, attribute=attribute, values=values, operation=operation
query=query, model=model, attribute=attribute, values=values, operation=operation
)

# Handle 'search' filter separately due to its unique logic
Expand Down Expand Up @@ -169,22 +155,15 @@ def get_project_stats(

query = session.query(ProjectStats)

# Filters applying 'ilike' operation
filterable_attributes = [
('registry', registry, 'ilike'),
filters = [
('registry', registry, 'ilike', ProjectStats),
('date', date_from, '>=', ProjectStats),
('date', date_to, '<=', ProjectStats),
]

for attribute, values, operation in filterable_attributes:
query = apply_filters(
query=query, model=ProjectStats, attribute=attribute, values=values, operation=operation
)

# Filters applying '>=', or '<=' operations
other_filters = [('date', date_from, '>='), ('date', date_to, '<=')]

for attribute, values, operation in other_filters:
for attribute, values, operation, model in filters:
query = apply_filters(
query=query, model=ProjectStats, attribute=attribute, values=values, operation=operation
query=query, model=model, attribute=attribute, values=values, operation=operation
)

if sort:
Expand Down

0 comments on commit b034c4b

Please sign in to comment.