Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 14.0 - bug fixes for search issues #5511

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def getoibasequery(cls, additionalfilter=None, userid=None, isiaorestrictedfilem
@classmethod
def getrequestssubquery(cls, groups, filterfields, keyword, additionalfilter, userid, iaoassignee, ministryassignee, requestby, isiaorestrictedfilemanager=False, isministryrestrictedfilemanager=False):
oibasequery = cls.getoibasequery(additionalfilter, userid, isiaorestrictedfilemanager, groups)

if len(filterfields) > 0 and keyword is not None:
filtercondition = cls.getfilterforrequestssubquery(filterfields, keyword)
return oibasequery.filter(filtercondition)
Expand Down Expand Up @@ -387,23 +386,66 @@ def findfield(cls, field):
return FOIAssignee.firstname
elif field == 'assignedToLastName':
return FOIAssignee.lastname
elif field == 'idNumber':
return cast(FOIMinistryRequest.filenumber, String)
elif field == 'axisRequestId':
return cast(FOIMinistryRequest.axisrequestid, String)
elif field == 'currentState':
return FOIRequestStatus.name
else:
return text(field)

@classmethod
def getfilterforrequestssubquery(cls, filterfields, keyword):
return or_(*[cls.getfiltercondition(filterfield, keyword) for filterfield in filterfields])
_keywords = []
if(keyword is not None):
_keywords = keyword.lower().replace(",", " ").split()

#filter/search
filtercondition = []
for _keyword in _keywords:
onekeywordfiltercondition = []
if(_keyword != 'restricted'):
for field in filterfields:
field_value = cls.findfield(field)
condition = field_value.ilike('%'+_keyword+'%')
exists_query = db.session.query(field_value).filter(condition).exists()
has_match = db.session.query(exists_query).scalar()
if has_match:
onekeywordfiltercondition.append(condition)
else:
filtercondition.append(FOIRestrictedMinistryRequest.isrestricted == True)

filtercondition.append(or_(*onekeywordfiltercondition))

return and_(*filtercondition)

@classmethod
def getfiltercondition(cls, filterfield, keyword):
if filterfield == 'requestType':
return FOIRequest.requesttype.ilike(f'%{keyword}%')
elif filterfield == 'publicationStatus':
return OpenInformationStatuses.name.ilike(f'%{keyword}%')
elif filterfield == 'assignee':
return cls.oiassignedto.ilike(f'%{keyword}%')

# Map filter fields to their corresponding model attributes
field_mapping = {
'requestType': FOIRequest.requesttype,
'publicationStatus': OpenInformationStatuses.name,
'assignee': cls.oiassignedto,
'idNumber': cast(FOIMinistryRequest.filenumber, String),
'axisRequestId': cast(FOIMinistryRequest.axisrequestid, String),
'description': FOIMinistryRequest.description,
'ministry': ProgramArea.bcgovcode,
'firstName': FOIRequestApplicant.firstname,
'lastName': FOIRequestApplicant.lastname,
'assignedToFirstName': FOIAssignee.firstname,
'assignedToLastName': FOIAssignee.lastname,
# Add more mappings as needed
}

# Get the corresponding model attribute from the mapping
field = field_mapping.get(filterfield)

if field is not None:
return field.ilike(f'%{keyword}%')
else:
return text(filterfield).ilike(f'%{keyword}%')
return filterfield.ilike(f'%{keyword}%')

@classmethod
def getgroupfilters(cls, groups):
Expand Down