Skip to content

Commit

Permalink
Merge 05df99b into 15ca483
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaël UTARD committed Aug 8, 2019
2 parents 15ca483 + 05df99b commit cdba2b1
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 54 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
**Bug fixes**

- Fix missing pictograms for mobile app
- Fix sync_mobile command for itinerancy


2.27.12 (2019-07-22)
Expand Down
49 changes: 35 additions & 14 deletions geotrek/api/management/commands/sync_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,34 @@ def sync_geojson(self, lang, viewset, name, zipfile=None, params={}, type_view={
self.sync_view(lang, view, name, params=params, headers=headers, zipfile=zipfile, fix2028=True, **kwargs)

def sync_trek_pois(self, lang, trek):
params = {'format': 'geojson'}
params = {'format': 'geojson', 'root_pk': trek.pk}
view = TrekViewSet.as_view({'get': 'pois'})
name = os.path.join(lang, str(trek.pk), 'pois.geojson')
self.sync_view(lang, view, name, params=params, pk=trek.pk)
# Sync POIs of children too
for child in trek.children:
name = os.path.join(lang, str(trek.pk), 'pois', '{}.geojson'.format(child.pk))
self.sync_view(lang, view, name, params=params, pk=child.pk)

def sync_trek_touristic_contents(self, lang, trek):
params = {'format': 'geojson'}
params = {'format': 'geojson', 'root_pk': trek.pk}
view = TrekViewSet.as_view({'get': 'touristic_contents'})
name = os.path.join(lang, str(trek.pk), 'touristic_contents.geojson')
self.sync_view(lang, view, name, params=params, pk=trek.pk)
# Sync contents of children too
for child in trek.children:
name = os.path.join(lang, str(trek.pk), 'touristic_contents', '{}.geojson'.format(child.pk))
self.sync_view(lang, view, name, params=params, pk=child.pk)

def sync_trek_touristic_events(self, lang, trek):
params = {'format': 'geojson'}
params = {'format': 'geojson', 'root_pk': trek.pk}
view = TrekViewSet.as_view({'get': 'touristic_events'})
name = os.path.join(lang, str(trek.pk), 'touristic_events.geojson')
self.sync_view(lang, view, name, params=params, pk=trek.pk)
# Sync events of children too
for child in trek.children:
name = os.path.join(lang, str(trek.pk), 'touristic_events', '{}.geojson'.format(child.pk))
self.sync_view(lang, view, name, params=params, pk=child.pk)

def sync_file(self, name, src_root, url, directory='', zipfile=None):
url = url.strip('/')
Expand Down Expand Up @@ -228,11 +240,7 @@ def sync_flatpage(self, lang):
def sync_trekking(self, lang):
self.sync_geojson(lang, TrekViewSet, 'treks.geojson', type_view={'get': 'list'})
treks = trekking_models.Trek.objects.existing().order_by('pk')
treks = treks.filter(
Q(**{'published_{lang}'.format(lang=lang): True})
| Q(**{'trek_parents__parent__published_{lang}'.format(lang=lang): True,
'trek_parents__parent__deleted': False})
)
treks = treks.filter(**{'published_{lang}'.format(lang=lang): True})

if self.portal:
treks = treks.filter(Q(portal__name__in=self.portal) | Q(portal=None))
Expand All @@ -243,6 +251,13 @@ def sync_trekking(self, lang):
self.sync_trek_pois(lang, trek)
self.sync_trek_touristic_contents(lang, trek)
self.sync_trek_touristic_events(lang, trek)
# Sync detail of children too
for child in trek.children:
self.sync_geojson(
lang, TrekViewSet,
'{pk}/treks/{child_pk}.geojson'.format(pk=trek.pk, child_pk=child.pk),
pk=child.pk, type_view={'get': 'retrieve'}, params={'root_pk': trek.pk},
)

def sync_settings_json(self, lang):
self.sync_json(lang, SettingsView, 'settings')
Expand Down Expand Up @@ -289,18 +304,24 @@ def sync_trek_by_pk_media(self, trek):
url_media = '/{}{}'.format(trek.pk, settings.MEDIA_URL)
self.sync_file(trek.get_elevation_chart_url_png(lang), settings.MEDIA_ROOT,
url_media, directory=url_trek, zipfile=trekid_zipfile)
# Sync media of children too
for child in trek.children:
for picture, resized in child.resized_pictures:
self.sync_media_file(resized, prefix=trek.pk, directory=url_trek, zipfile=trekid_zipfile)
for desk in child.information_desks.all():
self.sync_media_file(desk.resized_picture, prefix=trek.pk, directory=url_trek, zipfile=trekid_zipfile)
for lang in self.languages:
child.prepare_elevation_chart(lang, self.referer)
url_media = '/{}{}'.format(trek.pk, settings.MEDIA_URL)
self.sync_file(child.get_elevation_chart_url_png(lang), settings.MEDIA_ROOT,
url_media, directory=url_trek, zipfile=trekid_zipfile)

self.close_zip(trekid_zipfile, zipname_trekid)

def sync_treks_media(self):
treks = trekking_models.Trek.objects.existing().order_by('pk')
treks = trekking_models.Trek.objects.existing().filter(published=True).order_by('pk')
if self.portal:
treks = treks.filter(Q(portal__name__in=self.portal) | Q(portal=None))
treks = treks.filter(
Q(**{'published': True})
| Q(**{'trek_parents__parent__published': True,
'trek_parents__parent__deleted': False})
)

for trek in treks:
self.sync_trek_by_pk_media(trek)
Expand Down
6 changes: 3 additions & 3 deletions geotrek/api/mobile/serializers/tourism.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_pictures(self, obj):
'author': picture.author,
'title': picture.title,
'legend': picture.legend,
'url': os.path.join('/', str(self.context['trek_pk']), settings.MEDIA_URL[1:], thdetail.name),
'url': os.path.join('/', str(self.context['root_pk']), settings.MEDIA_URL[1:], thdetail.name),
})
return serialized

Expand All @@ -44,7 +44,7 @@ def get_pictures(self, obj):
'author': picture.author,
'title': picture.title,
'legend': picture.legend,
'url': os.path.join('/', str(self.context['trek_pk']), settings.MEDIA_URL[1:], thdetail.name),
'url': os.path.join('/', str(self.context['root_pk']), settings.MEDIA_URL[1:], thdetail.name),
})
return serialized

Expand All @@ -71,4 +71,4 @@ class Meta:
def get_picture(self, obj):
if not obj.resized_picture:
return None
return '/{trek_id}{url}'.format(trek_id=self.context['trek_pk'], url=obj.resized_picture.url),
return '/{trek_id}{url}'.format(trek_id=self.context['root_pk'], url=obj.resized_picture.url),
31 changes: 16 additions & 15 deletions geotrek/api/mobile/serializers/trekking.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_pictures(self, obj):
'author': picture.author,
'title': picture.title,
'legend': picture.legend,
'url': os.path.join('/', str(self.context['trek_pk']), settings.MEDIA_URL[1:], thdetail.name),
'url': os.path.join('/', str(self.context['root_pk']), settings.MEDIA_URL[1:], thdetail.name),
})
return serialized

Expand Down Expand Up @@ -69,9 +69,13 @@ class Meta:
geo_field = 'geometry'

class TrekListSerializer(TrekBaseSerializer):
first_picture = serializers.ReadOnlyField(source='resized_picture_mobile')
first_picture = serializers.SerializerMethodField(read_only=True)
geometry = geo_serializers.GeometryField(read_only=True, precision=7, source='start_point', )

def get_first_picture(self, obj):
root_pk = self.context.get('root_pk') or obj.pk
return obj.resized_picture_mobile(root_pk)

class Meta(TrekBaseSerializer.Meta):
fields = (
'id', 'pk', 'first_picture', 'name', 'departure', 'accessibilities', 'route', 'departure_city',
Expand All @@ -81,29 +85,25 @@ class Meta(TrekBaseSerializer.Meta):

class TrekDetailSerializer(TrekBaseSerializer):
geometry = geo_serializers.GeometryField(read_only=True, precision=7, source='geom2d_transformed')
pictures = serializers.ReadOnlyField(source='serializable_pictures_mobile')
pictures = serializers.SerializerMethodField(read_only=True)
arrival_city = serializers.SerializerMethodField(read_only=True)
information_desks = serializers.SerializerMethodField()
parking_location = serializers.SerializerMethodField(read_only=True)
profile = serializers.SerializerMethodField(read_only=True)
points_reference = serializers.SerializerMethodField()
children = serializers.SerializerMethodField()
parents = serializers.SerializerMethodField()

def get_pictures(self, obj):
root_pk = self.context.get('root_pk') or obj.pk
return obj.serializable_pictures_mobile(root_pk)

def get_children(self, obj):
children = obj.children.all().annotate(length_2d_m=Length('geom'),
start_point=Transform(StartPoint('geom'), settings.API_SRID),
end_point=Transform(EndPoint('geom'), settings.API_SRID))
serializer_children = TrekListSerializer(children, many=True)
serializer_children = TrekListSerializer(children, many=True, context={'root_pk': obj.pk})
return serializer_children.data

def get_parents(self, obj):
parents = obj.parents.all().annotate(length_2d_m=Length('geom'),
start_point=Transform(StartPoint('geom'), settings.API_SRID),
end_point=Transform(EndPoint('geom'), settings.API_SRID))
serializer_parents = TrekListSerializer(parents, many=True)
return serializer_parents.data

def get_points_reference(self, obj):
if not obj.points_reference:
return None
Expand All @@ -127,12 +127,13 @@ def get_geometry(self, obj):

def get_information_desks(self, obj):
return [
InformationDeskSerializer(information_desk, context={'trek_pk': obj.pk}).data
InformationDeskSerializer(information_desk, context={'root_pk': obj.pk}).data
for information_desk in obj.information_desks.all()
]

def get_profile(self, obj):
return os.path.join("/", str(obj.pk), settings.MEDIA_URL.lstrip('/'), obj.get_elevation_chart_url_png())
root_pk = self.context.get('root_pk') or obj.pk
return os.path.join("/", str(root_pk), settings.MEDIA_URL.lstrip('/'), obj.get_elevation_chart_url_png())

class Meta(TrekBaseSerializer.Meta):
auto_bbox = True
Expand All @@ -142,5 +143,5 @@ class Meta(TrekBaseSerializer.Meta):
'difficulty', 'length', 'ascent', 'descent', 'route', 'is_park_centered', 'parking_location',
'min_elevation', 'max_elevation', 'themes', 'networks', 'practice', 'difficulty',
'geometry', 'pictures', 'information_desks', 'cities', 'departure_city', 'arrival_city',
'points_reference', 'districts', 'ambiance', 'children', 'parents'
'points_reference', 'districts', 'ambiance', 'children',
)
12 changes: 9 additions & 3 deletions geotrek/api/mobile/views/trekking.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,36 @@ def get_queryset(self, *args, **kwargs):
| Q(**{'trek_parents__parent__published_{lang}'.format(lang=lang): True,
'trek_parents__parent__deleted': False})).distinct()

def get_serializer_context(self):
return {'root_pk': self.request.GET.get('root_pk')}

@decorators.detail_route(methods=['get'])
def pois(self, request, *args, **kwargs):
trek = self.get_object()
root_pk = self.request.GET.get('root_pk') or trek.pk
qs = trek.pois.filter(published=True).select_related('topo_object', 'type', )\
.prefetch_related('topo_object__aggregations', 'attachments') \
.annotate(geom2d_transformed=Transform(F('geom'), settings.API_SRID)) \
.order_by('pk')
data = api_serializers_trekking.POIListSerializer(qs, many=True, context={'trek_pk': trek.pk}).data
data = api_serializers_trekking.POIListSerializer(qs, many=True, context={'root_pk': root_pk}).data
return response.Response(data)

@decorators.detail_route(methods=['get'])
def touristic_contents(self, request, *args, **kwargs):
trek = self.get_object()
root_pk = self.request.GET.get('root_pk') or trek.pk
qs = trek.touristic_contents.filter(published=True).prefetch_related('attachments') \
.annotate(geom2d_transformed=Transform(F('geom'), settings.API_SRID)) \
.order_by('pk')
data = api_serializers_tourism.TouristicContentListSerializer(qs, many=True, context={'trek_pk': trek.pk}).data
data = api_serializers_tourism.TouristicContentListSerializer(qs, many=True, context={'root_pk': root_pk}).data
return response.Response(data)

@decorators.detail_route(methods=['get'])
def touristic_events(self, request, *args, **kwargs):
trek = self.get_object()
root_pk = self.request.GET.get('root_pk') or trek.pk
qs = trek.trek.touristic_events.filter(published=True).prefetch_related('attachments') \
.annotate(geom2d_transformed=Transform(F('geom'), settings.API_SRID)) \
.order_by('pk')
data = api_serializers_tourism.TouristicEventListSerializer(qs, many=True, context={'trek_pk': trek.pk}).data
data = api_serializers_tourism.TouristicEventListSerializer(qs, many=True, context={'root_pk': root_pk}).data
return response.Response(data)
11 changes: 1 addition & 10 deletions geotrek/api/tests/test_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
'access', 'advised_parking', 'advice', 'difficulty', 'length', 'ascent', 'descent', 'route', 'duration',
'is_park_centered', 'min_elevation', 'max_elevation', 'themes', 'networks', 'practice', 'pictures',
'information_desks', 'departure_city', 'arrival_city', 'parking_location', 'profile', 'points_reference',
'ambiance', 'children', 'parents'
'ambiance', 'children',
])


Expand Down Expand Up @@ -186,9 +186,6 @@ def test_trek_children_detail_parent_published(self):
self.assertEqual(sorted(json_response_1.get('properties').keys()),
TREK_DETAIL_PROPERTIES_GEOJSON_STRUCTURE)
self.assertEqual('Child_not_published', json_response_1.get('properties').get('name'))
self.assertEqual([self.trek_parent.pk, ],
[child.get('properties').get('id') for child in json_response_1.get('properties').
get('parents').get('features')])

# Published
response = self.get_treks_detail(self.trek_child_published.pk, 'fr')
Expand All @@ -199,9 +196,6 @@ def test_trek_children_detail_parent_published(self):
self.assertEqual(sorted(json_response_2.get('properties').keys()),
TREK_DETAIL_PROPERTIES_GEOJSON_STRUCTURE)
self.assertEqual('Child_published', json_response_2.get('properties').get('name'))
self.assertEqual([self.trek_parent.pk, ],
[child.get('properties').get('id') for child in json_response_1.get('properties').
get('parents').get('features')])

def test_trek_children_detail_parent_not_published(self):
# Not published => we don't got the detail because the parent is not published
Expand All @@ -220,9 +214,6 @@ def test_trek_children_detail_parent_not_published(self):
self.assertEqual(sorted(json_response_2.get('properties').keys()),
TREK_DETAIL_PROPERTIES_GEOJSON_STRUCTURE)
self.assertEqual('Child_published_2', json_response_2.get('properties').get('name'))
self.assertEqual([self.trek_parent_not_published.pk, ],
[child.get('properties').get('id') for child in json_response_2.get('properties').
get('parents').get('features')])

def test_trek_list(self):
response = self.get_treks_list('fr')
Expand Down
10 changes: 7 additions & 3 deletions geotrek/api/tests/test_sync_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from geotrek.common.utils.testdata import get_dummy_uploaded_image_svg, get_dummy_uploaded_image, get_dummy_uploaded_file
from geotrek.flatpages.factories import FlatPageFactory
from geotrek.flatpages.models import FlatPage
from geotrek.trekking.models import Trek, POI
from geotrek.trekking.factories import TrekWithPublishedPOIsFactory, PracticeFactory
from geotrek.trekking.models import Trek, POI, OrderedTrekChild
from geotrek.trekking.factories import TrekFactory, TrekWithPublishedPOIsFactory, PracticeFactory
from geotrek.tourism.factories import InformationDeskFactory, InformationDeskTypeFactory


Expand Down Expand Up @@ -322,6 +322,8 @@ def setUpClass(cls):
cls.trek_1 = TrekWithPublishedPOIsFactory.create()
cls.trek_2 = TrekWithPublishedPOIsFactory.create(portals=(cls.portal_a,))
cls.trek_3 = TrekWithPublishedPOIsFactory.create(portals=(cls.portal_b,))
cls.trek_4 = TrekFactory.create()
OrderedTrekChild.objects.create(parent=cls.trek_1, child=cls.trek_4, order=1)

cls.attachment_1 = AttachmentFactory.create(content_object=cls.trek_1,
attachment_file=get_dummy_uploaded_image())
Expand Down Expand Up @@ -353,9 +355,11 @@ def test_sync_treks_by_pk(self):
with open(os.path.join('tmp', 'en', '{pk}'.format(pk=str(self.trek_1.pk)),
'trek.geojson'), 'r') as f:
trek_geojson = json.load(f)
self.assertEqual(len(trek_geojson['properties']), 35)
self.assertEqual(len(trek_geojson['properties']), 34)

self.assertIn('en/{pk}/trek.geojson'.format(pk=str(self.trek_1.pk)), output.getvalue())
self.assertIn('en/{pk}/treks/{child_pk}.geojson'.format(pk=self.trek_1.pk, child_pk=self.trek_4.pk),
output.getvalue())

def test_sync_treks_with_portal(self):
output = BytesIO()
Expand Down
10 changes: 4 additions & 6 deletions geotrek/common/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,14 @@ def serializable_pictures(self):
})
return serialized

@property
def serializable_pictures_mobile(self):
def serializable_pictures_mobile(self, root_pk):
serialized = []
for picture, thdetail in self.resized_pictures:
serialized.append({
'author': picture.author,
'title': picture.title,
'legend': picture.legend,
'url': os.path.join('/', str(picture.object_id), settings.MEDIA_URL[1:], thdetail.name),
'url': os.path.join('/', str(root_pk), settings.MEDIA_URL[1:], thdetail.name),
})
return serialized

Expand Down Expand Up @@ -166,9 +165,8 @@ def thumbnail(self):
return thumbnail
return None

@property
def resized_picture_mobile(self):
pictures = self.serializable_pictures_mobile
def resized_picture_mobile(self, root_pk):
pictures = self.serializable_pictures_mobile(root_pk)
if pictures:
return pictures[0]
return None
Expand Down

0 comments on commit cdba2b1

Please sign in to comment.