Skip to content

Commit

Permalink
Merge 15e67fa into 767c3d9
Browse files Browse the repository at this point in the history
  • Loading branch information
LePetitTim committed Jul 8, 2019
2 parents 767c3d9 + 15e67fa commit ed47801
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion geotrek/altimetry/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from django.contrib.gis.geos import GEOSGeometry
from django.utils import translation
from django.utils.translation import ugettext as _
from django.contrib.gis.geos import LineString
from django.conf import settings
Expand Down Expand Up @@ -61,7 +62,7 @@ def altimetry_limits(cls, profile):
return ceil_elevation, floor_elevation

@classmethod
def profile_svg(cls, profile):
def profile_svg(cls, profile, language):
"""
Plot the altimetric graph in SVG using PyGal.
Most of the job done here is dedicated to preparing
Expand All @@ -86,6 +87,8 @@ def profile_svg(cls, profile):
style.colors = (settings.ALTIMETRIC_PROFILE_COLOR,)
style.font_family = settings.ALTIMETRIC_PROFILE_FONT
line_chart = pygal.XY(fill=True, style=style, **config)
if language:
translation.activate(language)
line_chart.x_title = _("Distance (m)")
line_chart.y_title = _("Altitude (m)")
line_chart.show_minor_x_labels = False
Expand All @@ -94,6 +97,7 @@ def profile_svg(cls, profile):
line_chart.truncate_label = 50
line_chart.range = [floor_elevation, ceil_elevation]
line_chart.no_data_text = _(u"Altimetry data not available")
translation.deactivate()
line_chart.add('', [(int(v[0]), int(v[3])) for v in profile])
return line_chart.render()

Expand Down
12 changes: 7 additions & 5 deletions geotrek/altimetry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ def get_elevation_area(self):
def get_elevation_limits(self):
return AltimetryHelper.altimetry_limits(self.get_elevation_profile())

def get_elevation_profile_svg(self):
return AltimetryHelper.profile_svg(self.get_elevation_profile())
def get_elevation_profile_svg(self, language=None):
return AltimetryHelper.profile_svg(self.get_elevation_profile(), language)

def get_elevation_chart_url(self):
def get_elevation_chart_url(self, language=None):
"""Generic url. Will fail if there is no such url defined
for the required model (see core.Path and trekking.Trek)
"""
app_label = self._meta.app_label
model_name = self._meta.model_name
return reverse('%s:%s_profile_svg' % (app_label, model_name), kwargs={'lang': get_language(), 'pk': self.pk})
if not language:
language = get_language()
return reverse('%s:%s_profile_svg' % (app_label, model_name), kwargs={'lang': language, 'pk': self.pk})

def get_elevation_chart_url_png(self, language=None):
"""Path to the PNG version of elevation chart. Relative to MEDIA_URL/MEDIA_ROOT.
Expand Down Expand Up @@ -93,7 +95,7 @@ def prepare_elevation_chart(self, language, rooturl):
if is_file_newer(path, self.date_update):
return False
# Download converted chart as png using convertit
source = smart_urljoin(rooturl, self.get_elevation_chart_url())
source = smart_urljoin(rooturl, self.get_elevation_chart_url(language))
convertit_download(source,
path,
from_type=HttpSVGResponse.content_type,
Expand Down
4 changes: 3 additions & 1 deletion geotrek/altimetry/tests/test_elevation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test.utils import override_settings
from django.utils import translation

from geotrek.core.models import Path
from geotrek.core.factories import TopologyFactory
Expand Down Expand Up @@ -114,7 +115,8 @@ def test_elevation_svg_output(self):
geom = LineString((1.5, 2.5, 8), (2.5, 2.5, 10),
srid=settings.SRID)
profile = AltimetryHelper.elevation_profile(geom)
svg = AltimetryHelper.profile_svg(profile)
language = translation.get_language()
svg = AltimetryHelper.profile_svg(profile, language)
self.assertIn('Generated with pygal', svg)
self.assertIn(settings.ALTIMETRIC_PROFILE_BACKGROUND, svg)
self.assertIn(settings.ALTIMETRIC_PROFILE_COLOR, svg)
Expand Down
8 changes: 7 additions & 1 deletion geotrek/altimetry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ class ElevationChart(LastModifiedMixin, BaseDetailView):
def dispatch(self, *args, **kwargs):
return super(ElevationChart, self).dispatch(*args, **kwargs)

def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(*args, object=self.object)
context['lang'] = kwargs.get('lang')
return self.render_to_response(context)

def render_to_response(self, context, **response_kwargs):
return HttpSVGResponse(self.get_object().get_elevation_profile_svg(),
return HttpSVGResponse(self.get_object().get_elevation_profile_svg(context['lang']),
**response_kwargs)


Expand Down

0 comments on commit ed47801

Please sign in to comment.