Skip to content

Commit 3245c42

Browse files
committed
1 parent 2d31945 commit 3245c42

8 files changed

+41
-13
lines changed

granary/source.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -647,28 +647,32 @@ def postprocess_activity(activity, mentions=False):
647647

648648
@staticmethod
649649
def postprocess_object(obj, mentions=False):
650-
"""Does source-independent post-processing of an object, in place.
650+
"""Does source-independent post-processing of an AS1 object, in place.
651651
652652
* Populates ``location.position`` based on latitude and longitude.
653653
* Optionally interprets HTML links in content with text starting with ``@``,
654654
eg ``@user`` or ``@user.com`` or ``@[email protected]``, as @-mentions
655655
and adds ``mention`` tags for them.
656656
657657
Args:
658-
obj (dict)
658+
obj (dict): AS1 object
659659
mentions (boolean): whether to detect @-mention links and convert them to
660660
mention tags
661661
662662
Returns:
663663
dict: ``obj``, modified in place
664664
"""
665665
loc = obj.get('location')
666-
if loc:
666+
if loc and isinstance(loc, dict) and 'position' not in loc:
667667
lat = loc.get('latitude')
668668
lon = loc.get('longitude')
669-
if lat and lon and not loc.get('position'):
670-
# ISO 6709 location string. details: http://en.wikipedia.org/wiki/ISO_6709
671-
loc['position'] = '%0+10.6f%0+11.6f/' % (lat, lon)
669+
if lat is not None and lon is not None:
670+
try:
671+
# ISO 6709 location string. details: http://en.wikipedia.org/wiki/ISO_6709
672+
loc['position'] = '%0+2f%0+2f/' % (float(lat), float(lon))
673+
except ValueError:
674+
# couldn't convert lat or lon to float
675+
pass
672676

673677
if mentions:
674678
# @-mentions to mention tags

granary/tests/test_as2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def test_lat_lon_float(self):
374374
'displayName': 'California',
375375
'longitude': -76.507450,
376376
'latitude': 38.3004,
377-
'position': '+38.300400-076.507450/',
377+
'position': '+38.300400-76.507450/',
378378
},
379379
}, as2.to_as1({
380380
'@context': ['https://www.w3.org/ns/activitystreams'],

granary/tests/test_microformats2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ def test_to_as1_with_location_hcard(self):
752752
'objectType': 'place',
753753
'latitude': 50.820641,
754754
'longitude': -0.149522,
755-
'position': '+50.820641-000.149522/',
755+
'position': '+50.820641-0.149522/',
756756
'displayName': 'Timeless Coffee Roasters',
757757
'url': 'https://kylewm.com/venues/timeless-coffee-roasters-oakland-california',
758758
}, obj['location'])
@@ -797,7 +797,7 @@ def _test_to_as1_with_location(self, props):
797797
self.assertEqual({
798798
'latitude': 50.820641,
799799
'longitude': -0.149522,
800-
'position': '+50.820641-000.149522/',
800+
'position': '+50.820641-0.149522/',
801801
'objectType': 'place',
802802
}, obj.get('location'))
803803

granary/tests/test_source.py

+24
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,30 @@ def test_postprocess_object_mention_existing_tag(self):
385385
self.assert_equals(expected, Source.postprocess_object(obj))
386386
self.assert_equals(expected, Source.postprocess_object(obj, mentions=True))
387387

388+
def test_postprocess_object_location(self):
389+
obj = {
390+
'location': {
391+
'latitude': -1.23,
392+
'longitude': 4.56,
393+
},
394+
}
395+
obj_with_pos = { # because postprocess_object modifies obj
396+
'location': {
397+
'latitude': -1.23,
398+
'longitude': 4.56,
399+
'position': '-1.230000+4.560000/',
400+
},
401+
}
402+
403+
self.assert_equals(obj_with_pos, Source.postprocess_object(obj_with_pos))
404+
self.assert_equals(obj_with_pos, Source.postprocess_object(obj))
405+
406+
def test_postprocess_object_location_not_dict(self):
407+
obj = {
408+
'location': 'asdf',
409+
}
410+
self.assert_equals(obj, Source.postprocess_object(obj))
411+
388412
def test_html_to_text_empty(self):
389413
self.assertEqual('', html_to_text(None))
390414
self.assertEqual('', html_to_text(''))

granary/tests/testdata/note.as-from-mf2.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
"objectType": "place",
2626
"latitude": 50.820641,
2727
"longitude": -0.149522,
28-
"position": "+50.820641-000.149522/"
28+
"position": "+50.820641-0.149522/"
2929
}
3030
}

granary/tests/testdata/note.as.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"url": "https://maps.google.com/maps?q=32.4004416,-98.9852672",
2121
"latitude": 50.820641,
2222
"longitude": -0.149522,
23-
"position": "+50.820641-000.149522/"
23+
"position": "+50.820641-0.149522/"
2424
},
2525
"tags": [{
2626
"url": "http://my/link",

granary/tests/testdata/note.as2.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"url": "https://maps.google.com/maps?q=32.4004416,-98.9852672",
2626
"latitude": 50.820641,
2727
"longitude": -0.149522,
28-
"position": "+50.820641-000.149522/"
28+
"position": "+50.820641-0.149522/"
2929
},
3030
"tag": [{
3131
"url": "http://my/link",

granary/tests/testdata/note.mf2.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"name": ["Carcassonne, Aude"],
3131
"url": ["https://maps.google.com/maps?q=32.4004416,-98.9852672"],
3232
"latitude": ["+50.820641"],
33-
"longitude": ["-000.149522"]
33+
"longitude": ["-0.149522"]
3434
}
3535
}]
3636
}

0 commit comments

Comments
 (0)