コード例 #1
0
def new_export(request):
    """
    Renders a html with only the form for creating a new export run

    * It uses the Django-validation bud adds a custom validation rule:
      there has to be at least one scenario selected. Otherwis an error
      is added to the error list.
    * If the form information is valid, a new export run is created and saved,
      Also the needed information for the genereation of the waterdepth map is
      saved in a CSV-file.
    """
    if not (request.user.is_authenticated()
            and request.user.has_perm('exporttool.can_create')):
        return HttpResponse(_("No permission to create export"))

    if request.method == 'POST':
        form = ExportRunForm(request.POST)
        # necessary to call 'is_valid()' before adding custom errors
        valid = form.is_valid()
        scenario_ids = json.loads(request.REQUEST.get('scenarioIds'))

        try:
            setting_max_scenarios = Setting.objects.get(
                key='MAX_SCENARIOS_PER_EXPORT').value
            max_scenarios = int(setting_max_scenarios)
        except Setting.DoesNotExist:
            max_scenarios = DEFAULT_MAX_SCENARIOS_PER_EXPORT

        if not scenario_ids:
            form._errors['scenarios'] = form.error_class(
                [u"U heeft geen scenario's geselecteerd."])
            valid = False
        elif len(scenario_ids) > max_scenarios:
            form._errors['scenarios'] = form.error_class([
                u"Er zijn maximaal {} scenario's per export toegestaan.".
                format(max_scenarios)
            ])
            valid = False

        if valid:
            new_export_run = ExportRun(
                export_type=ExportRun.EXPORT_TYPE_WATER_DEPTH_MAP,
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description'],
                owner=request.user,
                creation_date=datetime.datetime.now(),
                gridsize=form.cleaned_data['gridsize'],
                export_max_waterdepth=form.
                cleaned_data['export_max_waterdepth'],
                export_max_flowvelocity=form.
                cleaned_data['export_max_flowvelocity'],
                export_possibly_flooded=form.
                cleaned_data['export_possibly_flooded'],
                export_arrival_times=form.cleaned_data['export_arrival_times'],
                export_period_of_increasing_waterlevel=form.
                cleaned_data['export_period_of_increasing_waterlevel'],
                export_inundation_sources=form.
                cleaned_data['export_inundation_sources'],
                export_scenario_data=form.cleaned_data['export_scenario_data'],
                public=form.cleaned_data['public'])
            new_export_run.save()
            new_export_run.scenarios = Scenario.objects.filter(
                pk__in=scenario_ids)
            new_export_run.start()

            return HttpResponse('redirect_in_js_to_' +
                                reverse('flooding_tools_export_index'))
    else:
        form = ExportRunForm()

    return render_to_response('export/exports_new.html', {'form': form})
コード例 #2
0
ファイル: views.py プロジェクト: lizardsystem/flooding-lib
def new_export(request):
    """
    Renders a html with only the form for creating a new export run

    * It uses the Django-validation bud adds a custom validation rule:
      there has to be at least one scenario selected. Otherwis an error
      is added to the error list.
    * If the form information is valid, a new export run is created and saved,
      Also the needed information for the genereation of the waterdepth map is
      saved in a CSV-file.
    """
    if not (request.user.is_authenticated() and
            request.user.has_perm('exporttool.can_create')):
        return HttpResponse(_("No permission to create export"))

    if request.method == 'POST':
        form = ExportRunForm(request.POST)
        # necessary to call 'is_valid()' before adding custom errors
        valid = form.is_valid()
        scenario_ids = json.loads(request.REQUEST.get('scenarioIds'))

        try:
            setting_max_scenarios = Setting.objects.get(
                key='MAX_SCENARIOS_PER_EXPORT').value
            max_scenarios = int(setting_max_scenarios)
        except Setting.DoesNotExist:
            max_scenarios = DEFAULT_MAX_SCENARIOS_PER_EXPORT

        if not scenario_ids:
            form._errors['scenarios'] = form.error_class(
                [u"U heeft geen scenario's geselecteerd."])
            valid = False
        elif len(scenario_ids) > max_scenarios:
            form._errors['scenarios'] = form.error_class(
                [u"Er zijn maximaal {} scenario's per export toegestaan."
                 .format(max_scenarios)])
            valid = False

        if valid:
            new_export_run = ExportRun(
                export_type=ExportRun.EXPORT_TYPE_WATER_DEPTH_MAP,
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description'],
                owner=request.user,
                creation_date=datetime.datetime.now(),
                gridsize=form.cleaned_data['gridsize'],
                export_max_waterdepth=form.cleaned_data['export_max_waterdepth'],
                export_max_flowvelocity=form.cleaned_data['export_max_flowvelocity'],
                export_possibly_flooded=form.cleaned_data['export_possibly_flooded'],
                export_arrival_times=form.cleaned_data['export_arrival_times'],
                export_period_of_increasing_waterlevel=
                form.cleaned_data['export_period_of_increasing_waterlevel'],
                export_inundation_sources=form.cleaned_data['export_inundation_sources'],
                export_scenario_data=form.cleaned_data['export_scenario_data'],
                public=form.cleaned_data['public']
            )
            new_export_run.save()
            new_export_run.scenarios = Scenario.objects.filter(
                pk__in=scenario_ids)
            new_export_run.start()

            return HttpResponse(
                'redirect_in_js_to_' + reverse('flooding_tools_export_index'))
    else:
        form = ExportRunForm()

    return render_to_response('export/exports_new.html',
                              {'form': form})
コード例 #3
0
ファイル: views.py プロジェクト: pombredanne/flooding-lib
def new_export(request):
    """
    Renders a html with only the form for creating a new export run

    * It uses the Django-validation bud adds a custom validation rule:
      there has to be at least one scenario selected. Otherwis an error
      is added to the error list.
    * If the form information is valid, a new export run is created and saved,
      Also the needed information for the genereation of the waterdepth map is
      saved in a CSV-file.
    """
    if not (request.user.is_authenticated() and
            request.user.has_perm('exporttool.can_create')):
        return HttpResponse(_("No permission to create export"))

    if request.method == 'POST':
        form = ExportRunForm(request.POST)
        # necessary to call 'is_valid()' before adding custom errors
        valid = form.is_valid()
        scenario_ids = simplejson.loads(request.REQUEST.get('scenarioIds'))
        if not scenario_ids:
            form._errors['scenarios'] = form.error_class(
                [u"U heeft geen scenario's geselecteerd."])
            valid = False

        if valid:
            new_export_run = ExportRun(
                export_type=ExportRun.EXPORT_TYPE_WATER_DEPTH_MAP,
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description'],
                owner=request.user,
                creation_date=datetime.datetime.now(),
                export_max_waterdepth=form.cleaned_data['export_max_waterdepth'],
                export_max_flowvelocity=form.cleaned_data['export_max_flowvelocity'],
                export_possibly_flooded=form.cleaned_data['export_possibly_flooded']
                )
            new_export_run.save()
            new_export_run.scenarios = Scenario.objects.filter(
                pk__in=scenario_ids)

            # This part is obsolete, soon to be removed if the task is finished.
            if (new_export_run.export_type ==
                ExportRun.EXPORT_TYPE_WATER_DEPTH_MAP):

                # Choices here are gridmaxwaterdepth and gridmaxflowvelocity
                export_result_type = ResultType.objects.get(name='gridmaxwaterdepth').id
                #export_result_type = 1  # TODO: get the id from ResultType table.

                # Get the EXPORT_FOLDER settings (from the settings of
                # the export-tool)
                export_folder = Setting.objects.get(key='EXPORT_FOLDER').value
                csv_file_location = os.path.join(
                    export_folder, str(new_export_run.id) + '.csv')
                text_file_location = os.path.join(
                    export_folder, str(new_export_run.id) + '.txt')
                new_export_run.create_csv_file_for_gis_operation(
                    export_result_type, csv_file_location)
                new_export_run.create_general_file_for_gis_operation(
                    text_file_location)

            # Make a workflow for the export and run it
            workflow_template = WorkflowTemplate.objects.get(code='3')
            result = start_workflow(
                new_export_run.id,
                workflow_template.id, log_level='INFO',
                scenario_type='flooding_exportrun')
            #print 'ActionWorkflowExportRun: %r' % result

            return HttpResponse(
                'redirect_in_js_to_' + reverse('flooding_tools_export_index'))
    else:
        form = ExportRunForm()

    return render_to_response('export/exports_new.html',
                              {'form': form})