def new(request):
    """The map creation page and form."""

    papersize_buttons = ''

    if request.method == 'POST':
        form = forms.MapRenderingJobForm(request.POST, request.FILES)
        if form.is_valid():
            request.session['new_layout'] = form.cleaned_data.get('layout')
            request.session['new_stylesheet'] = form.cleaned_data.get('stylesheet')
            request.session['new_overlay'] = form.cleaned_data.get('overlay')
            request.session['new_paper_width_mm'] = form.cleaned_data.get('paper_width_mm')
            request.session['new_paper_height_mm'] = form.cleaned_data.get('paper_height_mm')

            job = form.save(commit=False)
            job.administrative_osmid = form.cleaned_data.get('administrative_osmid')
            job.stylesheet = form.cleaned_data.get('stylesheet')
            job.overlay = ",".join(form.cleaned_data.get('overlay'))
            job.layout = form.cleaned_data.get('layout')
            job.paper_width_mm = form.cleaned_data.get('paper_width_mm')
            job.paper_height_mm = form.cleaned_data.get('paper_height_mm')
            job.status = 0 # Submitted
            if www.settings.SUBMITTER_IP_LIFETIME != 0:
                job.submitterip = request.META['REMOTE_ADDR']
            else:
                job.submitterip = None

            job.submitteremail = form.cleaned_data.get('submitteremail')
            job.map_language = form.cleaned_data.get('map_language')
            job.index_queue_at_submission = (models.MapRenderingJob.objects
                                             .queue_size())
            job.nonce = helpers.generate_nonce(models.MapRenderingJob.NONCE_SIZE)

            job.save()

            files = request.FILES.getlist('uploadfile')
            for file in files:
                create_upload_file(job, file)

            return HttpResponseRedirect(reverse('map-by-id-and-nonce',
                                                args=[job.id, job.nonce]))
        else:
            LOG.debug("FORM NOT VALID")
    else:
        init_vals = request.GET.dict()
        oc = ocitysmap.OCitySMap(www.settings.OCITYSMAP_CFG_PATH)

        if not 'layout' in init_vals and 'new_layout' in request.session :
            init_vals['layout'] = request.session['new_layout']
        else:
           request.session['new_layout'] = oc.get_all_renderer_names()[0]

        if not 'stylesheet' in init_vals and 'new_stylesheet' in request.session:
            init_vals['stylesheet'] = request.session['new_stylesheet']
        else:
            request.session['new_stylesheet'] = oc.get_all_style_names()[0]

        if not 'overlay' in init_vals and 'new_overlay' in request.session:
            init_vals['overlay'] = request.session['new_overlay']

        if not 'paper_width_mm' in init_vals and 'new_paper_width_mm' in request.session:
            init_vals['paper_width_mm'] = request.session['new_paper_width_mm']

        if not 'paper_height_mm' in init_vals and 'new_paper_width_mm' in request.session:
            init_vals['paper_height_mm'] = request.session['new_paper_height_mm']

        form = forms.MapRenderingJobForm(initial=init_vals)

        _ocitysmap = ocitysmap.OCitySMap(www.settings.OCITYSMAP_CFG_PATH)

        # TODO: create tempates for these button lines ...
        papersize_buttons += "<p><button id='paper_best_fit' type='button' class='btn btn-primary papersize papersize_best_fit' onclick='set_papersize(0,0);'><i class='fas fa-square fa-2x'></i></button> <b>Best fit</b> (<span id='best_width'>?</span>&times;<span id='best_height'>?</span>mm²)</p>"
        for p in _ocitysmap.get_all_paper_sizes():
            if p[1] is not None:
                papersize_buttons += "<p>"
                if p[1] != p[2]:
                    papersize_buttons += "<button id='paper_{0}_{1}' type='button' class='btn btn-primary papersize papersize_{0}_{1}' onclick='set_papersize({0}, {1});'><i class='fas fa-portrait fa-2x'></i></button> ".format(p[1], p[2])
                    papersize_buttons += "<button id='paper_{0}_{1}' type='button' class='btn btn-primary papersize papersize_{0}_{1}' onclick='set_papersize({0}, {1});'><i class='fas fa-image fa-2x'></i></button> ".format(p[2], p[1])
                else:
                    papersize_buttons += "<button id='paper_{0}_{1}' disabled type='button' class='btn btn-primary papersize papersize_{0}_{1}' onclick='set_papersize({0}, {1});'><i class='fas fa-square fa-2x'></i></button> ".format(p[1], p[2])

                papersize_buttons += "<b>%s</b> (%s&times;%smm²)</p>" % (p[0], repr(p[1]), repr(p[2]))

        multisize_buttons = ''
        for p in _ocitysmap.get_all_paper_sizes('multipage'):
            if p[1] is not None:
                multisize_buttons += "<p>"
                if p[1] != p[2]:
                    multisize_buttons += "<button id='multipaper_{0}_{1}' type='button' class='btn btn-primary papersize papersize_{0}_{1}' onclick='set_papersize({0}, {1});'><i class='fas fa-portrait fa-2x'></i></button> ".format(p[1], p[2])
                    multisize_buttons += "<button id='multipaper_{0}_{1}' type='button' class='btn btn-primary papersize papersize_{0}_{1}' onclick='set_papersize({0}, {1});'><i class='fas fa-image fa-2x'></i></button> ".format(p[2], p[1])
                else:
                    multisize_buttons += "<button id='multipaper_{0}_{1}' disabled type='button' class='btn btn-primary papersize papersize_{0}_{1}' onclick='set_papersize({0}, {1});'><i class='fas fa-square fa-2x'></i></button> ".format(p[1], p[2])
                multisize_buttons += "<b>%s</b> (%s&times;%smm²)</p>" % (p[0], repr(p[1]), repr(p[2]))

        return render(request, 'maposmatic/new.html',
                      { 'form' : form ,
                        'papersize_suggestions': mark_safe(papersize_buttons),
                        'multipage_papersize_suggestions': mark_safe(multisize_buttons),
                      })