Skip to content

Commit

Permalink
PR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ochiu committed Jan 22, 2024
1 parent 84b88a4 commit 5ca7122
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
1 change: 1 addition & 0 deletions pay-api/src/pay_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .cfs_account_status_code import CfsAccountStatusCode, CfsAccountStatusCodeSchema
from .corp_type import CorpType, CorpTypeSchema # noqa: I001
from .credit import Credit
from .custom_query import CustomQuery
from .db import db, ma # noqa: I001
from .disbursement_status_code import DisbursementStatusCode
from .distribution_code import DistributionCode, DistributionCodeLink
Expand Down
31 changes: 31 additions & 0 deletions pay-api/src/pay_api/models/custom_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright © 2023 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Custom Query class to extend BaseQuery class functionality."""
from datetime import datetime
from flask_sqlalchemy import BaseQuery
from sqlalchemy import func


class CustomQuery(BaseQuery):
"""Custom Query class to extend the base query class for helper functionality."""

def filter_conditionally(self, search_criteria, model_attribute):
"""Add query filter if present."""
if search_criteria is None:
return self

if isinstance(search_criteria, datetime):
return self.filter(func.DATE(model_attribute) == search_criteria.date())

return self.filter(model_attribute == search_criteria)
5 changes: 3 additions & 2 deletions pay-api/src/pay_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy_continuum import make_versioned
from sqlalchemy_continuum.plugins import ActivityPlugin

from .custom_query import CustomQuery

# by convention in the Flask community these are lower case,
# whereas pylint wants them upper case
ma = Marshmallow() # pylint: disable=invalid-name
db = SQLAlchemy() # pylint: disable=invalid-name
db = SQLAlchemy(query_class=CustomQuery) # pylint: disable=invalid-name


activity_plugin = ActivityPlugin() # pylint: disable=invalid-name

Expand Down
31 changes: 9 additions & 22 deletions pay-api/src/pay_api/services/eft_gl_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
from datetime import datetime
from typing import Optional

from sqlalchemy import func

from pay_api.models import EFTGLTransfer as EFTGLTransferModel
from pay_api.models import db

Expand Down Expand Up @@ -76,27 +74,16 @@ def search(search_criteria: EFTGlTransferSearch = EFTGlTransferSearch()) -> [EFT
"""Return EFT Transfers by search criteria."""
query = db.session.query(EFTGLTransferModel)

query = EFTGlTransfer.filterIfPresent(query, search_criteria.created_on, EFTGLTransferModel.created_on)
query = EFTGlTransfer.filterIfPresent(query, search_criteria.invoice_id, EFTGLTransferModel.invoice_id)
query = EFTGlTransfer.filterIfPresent(query, search_criteria.is_processed, EFTGLTransferModel.is_processed)
query = EFTGlTransfer.filterIfPresent(query, search_criteria.processed_on, EFTGLTransferModel.processed_on)
query = EFTGlTransfer.filterIfPresent(query, search_criteria.short_name_id, EFTGLTransferModel.short_name_id)
query = EFTGlTransfer.filterIfPresent(query, search_criteria.source_gl, EFTGLTransferModel.source_gl)
query = EFTGlTransfer.filterIfPresent(query, search_criteria.target_gl, EFTGLTransferModel.target_gl)
query = EFTGlTransfer.filterIfPresent(query, search_criteria.transfer_type, EFTGLTransferModel.transfer_type)
query = EFTGlTransfer.filterIfPresent(query, search_criteria.transfer_date, EFTGLTransferModel.transfer_date)
query = query.filter_conditionally(search_criteria.created_on, EFTGLTransferModel.created_on)
query = query.filter_conditionally(search_criteria.invoice_id, EFTGLTransferModel.invoice_id)
query = query.filter_conditionally(search_criteria.is_processed, EFTGLTransferModel.is_processed)
query = query.filter_conditionally(search_criteria.processed_on, EFTGLTransferModel.processed_on)
query = query.filter_conditionally(search_criteria.short_name_id, EFTGLTransferModel.short_name_id)
query = query.filter_conditionally(search_criteria.source_gl, EFTGLTransferModel.source_gl)
query = query.filter_conditionally(search_criteria.target_gl, EFTGLTransferModel.target_gl)
query = query.filter_conditionally(search_criteria.transfer_type, EFTGLTransferModel.transfer_type)
query = query.filter_conditionally(search_criteria.transfer_date, EFTGLTransferModel.transfer_date)

query = query.order_by(EFTGLTransferModel.created_on.asc())

return query.all()

@staticmethod
def filterIfPresent(query, search_criteria, model_attribute):
"""Add query filter if present."""
if search_criteria is None:
return query

if isinstance(search_criteria, datetime):
return query.filter(func.DATE(model_attribute) == search_criteria.date())

return query.filter(model_attribute == search_criteria)
2 changes: 1 addition & 1 deletion pay-api/src/pay_api/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
Development release segment: .devN
"""

__version__ = '1.20.5' # pylint: disable=invalid-name
__version__ = '1.20.6' # pylint: disable=invalid-name

0 comments on commit 5ca7122

Please sign in to comment.