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

Add support for unit_include with multiple units #291

Merged
merged 2 commits into from
Aug 4, 2023
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
15 changes: 14 additions & 1 deletion services/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,13 +1147,13 @@
class AdministrativeDivisionSerializer(munigeo_api.AdministrativeDivisionSerializer):
def to_representation(self, obj):
ret = super(AdministrativeDivisionSerializer, self).to_representation(obj)

if "request" not in self.context:
return ret

query_params = self.context["request"].query_params
unit_include = query_params.get("unit_include", None)
service_point_id = ret["service_point_id"]

if service_point_id and unit_include:
try:
unit = Unit.objects.get(id=service_point_id)
Expand All @@ -1167,6 +1167,19 @@
ser = UnitSerializer(unit, context={"only": unit_include.split(",")})
ret["unit"] = ser.data

unit_ids = ret["units"]

Check warning on line 1170 in services/api.py

View check run for this annotation

Codecov / codecov/patch

services/api.py#L1170

Added line #L1170 was not covered by tests
if unit_ids and unit_include:
units = Unit.objects.filter(id__in=unit_ids)

Check warning on line 1172 in services/api.py

View check run for this annotation

Codecov / codecov/patch

services/api.py#L1172

Added line #L1172 was not covered by tests
if units:
units_data = []

Check warning on line 1174 in services/api.py

View check run for this annotation

Codecov / codecov/patch

services/api.py#L1174

Added line #L1174 was not covered by tests
for unit in units:
units_data.append(

Check warning on line 1176 in services/api.py

View check run for this annotation

Codecov / codecov/patch

services/api.py#L1176

Added line #L1176 was not covered by tests
UnitSerializer(
unit, context={"only": unit_include.split(",")}
).data
)
ret["units"] = units_data

Check warning on line 1181 in services/api.py

View check run for this annotation

Codecov / codecov/patch

services/api.py#L1181

Added line #L1181 was not covered by tests

include_fields = query_params.get("include", [])
if "centroid" in include_fields and obj.geometry:
centroid = obj.geometry.boundary.centroid
Expand Down
Loading