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

Resident Directory Filter #205

Merged
merged 4 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion backend/app/rest/residents_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def get_residents():
page_number = int(request.args.get("page_number"))
except:
pass
try:
filters = json.loads(request.args.get("filters"))
except:
filters = None

results_per_page = 10
try:
Expand All @@ -142,7 +146,7 @@ def get_residents():
try:
resident_id = request.args.get("resident_id")
residents_results = residents_service.get_residents(
return_all, page_number, results_per_page, resident_id
return_all, page_number, results_per_page, resident_id, filters
)
return jsonify(residents_results), 201
except Exception as e:
Expand Down
46 changes: 45 additions & 1 deletion backend/app/services/implementations/residents_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def delete_resident(self, resident_id):
db.session.commit()

def get_residents(
self, return_all, page_number, results_per_page, resident_id=None
self, return_all, page_number, results_per_page, resident_id=None, filters=None
):
try:
if resident_id:
Expand All @@ -120,6 +120,50 @@ def get_residents(
.with_entities(Residents, Buildings.name.label("building"))
.all()
)
elif filters:
resident_id = filters.get("resident_id")
building_id = filters.get("building_id")
date_left = None
date_joined = None

if filters.get("date_range") is not None:
date_joined, date_left = filters.get("date_range")
if date_joined is not None:
date_joined = datetime.strptime(
date_joined, "%Y-%m-%d"
).replace(hour=0, minute=0)
if date_left is not None:
date_left = datetime.strptime(date_left, "%Y-%m-%d").replace(
hour=0, minute=0
)

residents_results = Residents.query.join(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably can be moved out of the if-else statements since it is always used.

Buildings, Buildings.id == Residents.building_id
).with_entities(Residents, Buildings.name.label("building"))

if building_id is not None:
residents_results = residents_results.filter(
Residents.building_id.in_(building_id)
)
if resident_id is not None:
residents_results = residents_results.filter(
Residents.resident_id.in_(resident_id)
)
if date_joined is not None and date_left is not None:
residents_results = residents_results.filter(
Residents.date_joined >= date_joined
)
residents_results = residents_results.filter(
Residents.date_left <= date_left
)
elif date_joined is not None:
residents_results = residents_results.filter(
Residents.date_joined >= date_joined
)
elif date_left is not None:
residents_results = residents_results.filter(
Residents.date_left <= date_left
)
else:
residents_results = (
Residents.query.join(
Expand Down
Loading