Introduce two hidden fields in MapRenderingJobForm to store paper_width_mm and paper_height_mm. Those two fields are updated by the JS code when the selected paper size changes. They are then sent to the server as part of the wizard form and then stored in the database.
It allows to fix the handling of paper sizes for "Best fit" renderings. Signed-off-by: Thomas Petazzoni <[email protected]> --- www/maposmatic/forms.py | 13 +++++-------- www/media/map_rendering_form.js | 27 +++++++++++++++++---------- www/templates/maposmatic/new.html | 1 + 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/www/maposmatic/forms.py b/www/maposmatic/forms.py index 81af48b..69f1a5a 100644 --- a/www/maposmatic/forms.py +++ b/www/maposmatic/forms.py @@ -67,6 +67,8 @@ class MapRenderingJobForm(forms.ModelForm): papersize = forms.ChoiceField(choices=(), widget=forms.RadioSelect) paperorientation = forms.ChoiceField(choices=ORIENTATION, widget=forms.RadioSelect) + paper_width_mm = forms.IntegerField(widget=forms.HiddenInput) + paper_height_mm = forms.IntegerField(widget=forms.HiddenInput) maptitle = forms.CharField(max_length=256, required=False) bbox = widgets.AreaField(label=_("Area"), fields=(forms.FloatField(), forms.FloatField(), @@ -119,14 +121,9 @@ class MapRenderingJobForm(forms.ModelForm): city = cleaned_data.get("administrative_city") title = cleaned_data.get("maptitle") - for p in renderers.Renderer.PAPER_SIZES: - if p[0] == cleaned_data.get("papersize"): - w, h = p[2], p[1] - if cleaned_data.get("paperorientation") == 'landscape': - w, h = p[2], p[1] - cleaned_data["paper_width_mm"] = w - cleaned_data["paper_height_mm"] = h - break + if cleaned_data.get("paperorientation") == 'landscape': + cleaned_data["paper_width_mm"], cleaned_data["paper_height_mm"] = \ + cleaned_data.get("paper_height_mm"), cleaned_data.get("paper_width_mm") if title == '': msg = _(u"Map title required") diff --git a/www/media/map_rendering_form.js b/www/media/map_rendering_form.js index 88c3358..907d7cb 100644 --- a/www/media/map_rendering_form.js +++ b/www/media/map_rendering_form.js @@ -61,15 +61,18 @@ function getPaperDef(paperlist, paper) return null; } -/* This function updates the landscape/portrait selectors according to - * the portraitOk/landscapeOk booleans telling whether portrait and - * landscape are possible. */ -function filterAllowedOrientations(portraitOk, landscapeOk) +/* This function : + * - updates the landscape/portrait selectors according to the + * portraitOk/landscapeOk booleans telling whether portrait and + * landscape are possible. + * - updates the hidden fields paper_width_mm and paper_height_mm + */ +function handlePaperSizeClick(width_mm, height_mm, portrait_ok, landscape_ok) { landscape = $("input[value='landscape']"); portrait = $("input[value='portrait']"); - if (landscapeOk) { + if (landscape_ok) { landscape.attr("disabled", ""); landscape.attr("checked", "checked"); landscape.parent().parent().removeClass("disabled"); @@ -79,9 +82,9 @@ function filterAllowedOrientations(portraitOk, landscapeOk) landscape.parent().parent().addClass("disabled"); } - if (portraitOk) { + if (portrait_ok) { portrait.attr("disabled", ""); - if (! landscapeOk) + if (! landscape_ok) portrait.attr("checked", "checked"); portrait.parent().parent().removeClass("disabled"); } @@ -89,12 +92,15 @@ function filterAllowedOrientations(portraitOk, landscapeOk) portrait.attr("disabled", "disabled"); portrait.parent().parent().addClass("disabled"); } + + $("#id_paper_width_mm").val(width_mm); + $("#id_paper_height_mm").val(height_mm); } -function bindPaperClickCallback(fn, portraitOk, landscapeOk) +function bindPaperClickCallback(fn, width_mm, height_mm, portrait_ok, landscape_ok) { return (function(e) { - fn(portraitOk, landscapeOk); + fn(width_mm, height_mm, portrait_ok, landscape_ok); }); } @@ -109,7 +115,8 @@ function filterAllowedPaper(paperlist) paperDef = getPaperDef(paperlist, paper); if (paperDef != null) { $('label', item).bind('click', - bindPaperClickCallback(filterAllowedOrientations, + bindPaperClickCallback(handlePaperSizeClick, + paperDef[1], paperDef[2], paperDef[3], paperDef[4])); $(item).show(); } diff --git a/www/templates/maposmatic/new.html b/www/templates/maposmatic/new.html index 860fafa..11f13c1 100644 --- a/www/templates/maposmatic/new.html +++ b/www/templates/maposmatic/new.html @@ -95,6 +95,7 @@ <td id="papersizeselection">{{ form.papersize }}</td> <td id="paperorientationselection">{{ form.paperorientation }}</td></tr> </table> + {{ form.paper_width_mm }}{{ form.paper_height_mm }} </div> <div id="step-stylesheet" class="wizardstep"> -- 1.7.0.4
