Exemple #1
0
def job_new(request):
    error = flash = None
    if request.method == 'POST':
        form = ConfigurationForm(request.POST, request.FILES)
        if form.is_valid():
            # Handle input
            input_source = form.cleaned_data['input_source']
            if input_source == 'dataset':
                input = form.cleaned_data['input']
            elif input_source == 'path':
                input = Dataset(path=form.cleaned_data['input_path'],
                                owner=request.user)
                input.save()
            else:
                if input_source == 'upload':
                    # Read upload
                    input_data = form.cleaned_data['input_file'].read()
                else:
                    # Read text field
                    input_data = form.cleaned_data['input_text']

                name = form.cleaned_data['name']

                # Attempt to store on backend
                try:
                    with client.connect() as service:
                        real_path = service.storeDataset(name, input_data)
                except InternalException as ex:
                    error = WMRBackendInternalError(ex)
                except TException as ex:
                    error = WMRThriftError(ex)
                else:
                    input = Dataset(path=real_path, owner=request.user)
                    input.save()

                # Stop if upload failed
                if error:
                    return render_to_response(
                        'wmr/job_new.html',
                        RequestContext(request, {
                            'form': form,
                            'error': error,
                        }))

            # Handle mapper
            mapper_source = form.cleaned_data['mapper_source']
            if mapper_source == 'upload':
                mapper = form.cleaned_data['mapper_file'].read()
            else:
                mapper = form.cleaned_data['mapper']

            # Handle reducer
            reducer_source = form.cleaned_data['reducer_source']
            if reducer_source == 'upload':
                reducer = form.cleaned_data['reducer_file'].read()
            else:
                reducer = form.cleaned_data['reducer']

            # Create configuration object
            config = form.save(commit=False)
            config.owner = request.user
            config.input = input
            config.mapper = mapper
            config.reducer = reducer

            if request.POST['submit'] == "Save":
                # Save (or update) configuration
                saved_config = None
                try:
                    saved_config = SavedConfiguration.objects.get(
                        name=config.name, owner=request.user)
                except SavedConfiguration.MultipleObjectsReturned:
                    pass
                except SavedConfiguration.DoesNotExist:
                    saved_config = None

                if saved_config:
                    # Update existing configuration
                    saved_config.update_from_other(config)
                    saved_config.touch()
                    saved_config.save()
                    flash = "Configuration successfully updated."
                else:
                    # Copy to SavedConfiguration and save
                    saved_config = SavedConfiguration()
                    saved_config.owner = request.user
                    saved_config.update_from_other(config)
                    saved_config.save()
                    flash = "Configuration succesfully saved."

# if the input source was not the dataset, the data was either already in
# or put in a data path. Set the input field to that path.
# (This may not be the best way to do that, but it worked.)
                if input_source != 'dataset':
                    request.POST['input_source'] = 'path'
                    request.POST['input_path'] = input.path

            else:
                # Save configuration
                config.save()

                # Determine whether job is test
                test = (request.POST['submit'] == 'Test')

                # Submit job to backend
                thrift_request = config.to_thrift_request(test)
                try:
                    with client.connect() as service:
                        backend_id = service.submit(thrift_request)
                except (ValidationException, CompilationException,
                        PermissionException, QuotaException,
                        ForbiddenTestJobException) as ex:
                    error = WMRError(ex)
                except NotFoundException as ex:
                    error = WMRError(ex, title='Input Not Found')
                except InternalException as ex:
                    error = WMRBackendInternalError(ex)
                except TException as ex:
                    error = WMRThriftError(ex)
                else:
                    # Save job and redirect to view page
                    job = Job(config=config,
                              owner=request.user,
                              test=test,
                              backend_id=backend_id)
                    job.save()

                    return HttpResponseRedirect(
                        reverse(job_view, args=[job.id]))
    else:

        config = input = None

        if 'config' in request.GET:
            config_id = request.GET['config']
            try:
                config = Configuration.objects.get(pk=config_id)
            except Configuration.DoesNotExist as ex:
                error = WMRError(ex, title='Configuration not found')
            else:
                input = config.input

        elif 'output_from' in request.GET:
            from_id = request.GET['output_from']
            from_config = None
            try:
                from_config = Configuration.objects.get(id=from_id)
            except Configuration.DoesNotExist as ex:
                error = WMRError(ex, title='Configuration not found')
            else:
                config = Configuration()
                config.language = from_config.language
                config.name = from_config.name + "-phase"

                input_path = request.GET.get('input_path', None)
                if input_path:
                    input = Dataset(path=input_path, owner=request.user)
                    # Don't save it--just use its path below

        else:
            input_id = request.GET.get('input', None)
            try:
                input = Dataset.objects.get(pk=input_id)
            except Dataset.DoesNotExist:
                input = None

        # Determine correct form field for input
        formdata = {}
        if input:
            if input.public:
                formdata = {
                    'input_source': 'dataset',
                    'input': input.id,
                }
            else:
                formdata = {
                    'input_source': 'path',
                    'input_path': input.path,
                }

        form = ConfigurationForm(instance=config, initial=formdata)

    return render_to_response(
        'wmr/job_new.html',
        RequestContext(request, {
            'form': form,
            'error': error,
            'flash': flash,
        }))