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

update summary serializer for geolocation information #213

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 31 additions & 8 deletions src/design/plone/contenttypes/restapi/serializers/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from plone.app.contenttypes.interfaces import IEvent
from plone.app.contenttypes.interfaces import INewsItem
from plone.base.interfaces import IImageScalesAdapter
from plone.formwidget.geolocation.geolocation import Geolocation
from plone.restapi.interfaces import ISerializeToJsonSummary
from plone.restapi.serializer.converters import json_compatible
from Products.CMFPlone.utils import safe_hasattr
Expand Down Expand Up @@ -115,6 +116,35 @@ def get_taxonomy_information_by_type(res, context):
return res


def extract_geolocation(context, res):
"""
Extracts geolocation information from the provided context or res.
"""
# Check if latitude and longitude are already present in res
latitude = res.get("latitude", 0)
longitude = res.get("longitude", 0)

if latitude and longitude:
return {"latitude": latitude, "longitude": longitude}

# Check if context is an ICatalogBrain and has latitude and longitude
if ICatalogBrain.providedBy(context):
mamico marked this conversation as resolved.
Show resolved Hide resolved
latitude = context.latitude
longitude = context.longitude
if latitude and longitude:
return {"latitude": latitude, "longitude": longitude}

# Check if context has a geolocation attribute with latitude and longitude
geolocation = getattr(context, "geolocation", None)
if isinstance(geolocation, Geolocation):
latitude = geolocation.latitude
longitude = geolocation.longitude
if latitude and longitude:
return {"latitude": latitude, "longitude": longitude}

return None


@implementer(ISerializeToJsonSummary)
@adapter(Interface, IDesignPloneContenttypesLayer)
class DefaultJSONSummarySerializer(BaseSerializer):
Expand All @@ -132,14 +162,7 @@ def __call__(self, force_all_metadata=False, force_images=False):
if "geolocation" in metadata_fields or self.show_all_metadata_fields:
# backward compatibility for some block templates
if "geolocation" not in res:
res["geolocation"] = None
latitude = res.get("latitude", 0)
longitude = res.get("longitude", 0)
if latitude and longitude:
res["geolocation"] = {
"latitude": latitude,
"longitude": longitude,
}
res["geolocation"] = extract_geolocation(self.context, res)

res["id"] = self.context.id

Expand Down
29 changes: 29 additions & 0 deletions src/design/plone/contenttypes/tests/test_ct_luogo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.app.testing import TEST_USER_ID
from plone.formwidget.geolocation import Geolocation
from plone.restapi.testing import RelativeSession
from Products.CMFPlone.utils import getToolByName
from transaction import commit
Expand Down Expand Up @@ -205,3 +206,31 @@ def test_venue_news(self):
res["related_news"][1]["@id"],
self.news.absolute_url(),
)

def test_venue_serializers(self):
venue1 = api.content.create(container=self.portal, type="Venue", title="venue1")
venue1.geolocation = Geolocation(44.35, 11.70)
venue2 = api.content.create(container=self.portal, type="Venue", title="venue2")
venue2.geolocation = Geolocation(44.35, 11.70)
intids = getUtility(IIntIds)
venue_rel = RelationValue(intids.getId(venue2))
venue1.luoghi_correlati = [venue_rel]
commit()
response = self.api_session.post(
"/@querystring-search",
json={
"metadata_fields": "_all",
"query": [
{
"i": "portal_type",
"o": "plone.app.querystring.operation.selection.is",
"v": ["Venue"],
}
],
},
)
items = response.json()["items"]
for item in items:
if item["id"] in ["venue1", "venue2"]:
self.assertEqual(item["geolocation"]["latitude"], 44.35)
self.assertEqual(item["geolocation"]["longitude"], 11.7)
Loading