Beispiel #1
0
def initialize_points_from_csv(request):
    """ Uses a file upload to create a series of points and add them to the request
    :param request: request that contains the file upload
    :return: request with initial_values set
    """
    file_path = handle_file_upload(request, is_temp_file=True, overwrite_ok=True)
    with open(file_path) as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read(1024))  # is this necessary?
        csvfile.seek(0)
        header = csv.Sniffer().has_header(csvfile.read(1024))
        csvfile.seek(0)
        if header:
            header = None  #DictReader will pull it off the first line
        else:
            header = ['x', 'y']
        reader = csv.DictReader(lowercase_header(csvfile), fieldnames=header, dialect=dialect)
        entries = [line for line in reader]  # ordered list
        try:
            (float(entries[0]['x']), float(entries[0]['y']))  # the header sneaks in when there's a mix of float and int
        except ValueError:
            entries = entries[1:]  # clip the header
        
        initial_values = {}
        for index, point in enumerate(entries):
            initial_values['relationalpoint_set-%i-id' % index] = ''
            initial_values['relationalpoint_set-%i-relational_function' % index] = ''
            initial_values['relationalpoint_set-%i-x' % index] = point['x'].strip()
            initial_values['relationalpoint_set-%i-y' % index] = point['y'].strip()
            initial_values['relationalpoint_set-%i-DELETE' % index] = ''  # these could be set to delete by the js
        initial_values['relationalpoint_set-TOTAL_FORMS'] = str(len(entries))
        initial_values['relationalpoint_set-INITIAL_FORMS'] = '0'
        initial_values['relationalpoint_set-MAX_NUM_FORMS'] = '1000'
        request.POST.update(initial_values)
    return request
Beispiel #2
0
def upload_population(request):
    from ADSMSettings.models import SmSession
    session = SmSession.objects.get()
    if 'GET' in request.method:
        json_response = {"status": session.population_upload_status, "percent": session.population_upload_percent*100} 
        return JsonResponse(json_response)

    session.set_population_upload_status("Processing file")
    if 'filename' in request.POST:
        file_path = workspace_path(request.POST.get('filename')) 
    else:
        try:
            file_path = handle_file_upload(request, is_temp_file=True, overwrite_ok=True)
        except FileExistsError:
            return JsonResponse({"status": "failed", 
                                 "message": "Cannot import file because a file with the same name already exists in the list below."})

    return parse_population(file_path, session)
Beispiel #3
0
def run_importer(request, new_name=None):
    param_path = handle_file_upload(request, 'parameters_xml', is_temp_file=True, overwrite_ok=True)  # we don't want param XMLs stored next to population XMLs
    popul_path = handle_file_upload(request, 'population_xml', is_temp_file=True, overwrite_ok=True)
    import_legacy_scenario(param_path, popul_path, new_name=new_name)
Beispiel #4
0
def upload_scenario(request):
    if '.sqlite' in request.FILES['file']._name:
        handle_file_upload(request)  #TODO: This can throw an error, but this method isn't used currently
        return redirect('/app/Workspace/')
    else:
        raise ValueError("You can only submit files with '.sqlite' in the file name.")  # ugly error should be caught by Javascript first