Пример #1
0
def json_workbook(request):
    error = None
    warningsList = []

    if not (os.access(SERVER_TMP_DIR, os.F_OK)):
        os.mkdir(SERVER_TMP_DIR)

    # Make a randomly generated directory to prevent name collisions
    temp_dir = tempfile.mkdtemp(dir=SERVER_TMP_DIR)
    form_name = request.POST.get("name", "form")
    output_filename = form_name + ".xml"
    out_path = os.path.join(temp_dir, output_filename)
    if os.access(out_path, os.F_OK):
        os.remove(out_path)
    try:
        json_survey = xls2json.workbook_to_json(
            json.loads(request.POST["workbookJson"]), form_name=form_name, warnings=warningsList
        )
        survey = pyxform.create_survey_element_from_dict(json_survey)
        survey.print_xform_to_file(out_path, warnings=warningsList)
    except Exception as e:
        error = str(e)
    return HttpResponse(
        json.dumps(
            {"dir": os.path.split(temp_dir)[-1], "name": output_filename, "error": error, "warnings": warningsList},
            indent=4,
        ),
        mimetype="application/json",
    )
Пример #2
0
 def _ss_structure_to_pyxform_survey(self, ss_structure, kwargs):
     # using existing methods from the builder
     imported_survey_json = workbook_to_json(ss_structure)
     # ieadlly, when all these tests are working, this would be
     # refactored as well
     survey = create_survey_element_from_dict(imported_survey_json)
     survey.name = kwargs.get("name")
     survey.title = kwargs.get("title")
     survey.id_string = kwargs.get("id_string")
     return survey
Пример #3
0
def json_workbook(request):
    error = None
    warningsList = []
    #Make a randomly generated directory to prevent name collisions
    temp_dir = tempfile.mkdtemp(dir=SERVER_TMP_DIR)
    form_name = request.POST.get('name', 'form')
    output_filename = form_name + '.xml'
    out_path = os.path.join(temp_dir, output_filename)
    fo = open(out_path, "wb+")
    fo.close()
    try:
        json_survey = xls2json.workbook_to_json(json.loads(request.POST['workbookJson']), form_name=form_name, warnings=warningsList)
        survey = pyxform.create_survey_element_from_dict(json_survey)
        survey.print_xform_to_file(out_path, warnings=warningsList)
    except Exception as e:
        error = str(e)
    return HttpResponse(json.dumps({
        'dir': os.path.split(temp_dir)[-1],
        'name' : output_filename,
        'error': error,
        'warnings': warningsList,
    }, indent=4), mimetype="application/json")
Пример #4
0
def create_survey_from_csv_text(csv_text, default_name='KoBoFormSurvey', default_language=u'default', warnings=None, ):
    workbook_dict = xls2json_backends.csv_to_dict(StringIO.StringIO(csv_text.encode("utf-8")))
    dict_repr = xls2json.workbook_to_json(workbook_dict, default_name, default_language, warnings)
    dict_repr[u'name'] = dict_repr[u'id_string']
    return builder.create_survey_element_from_dict(dict_repr)
Пример #5
0
def create_survey_from_ss_struct(ss_struct, default_name='KoBoFormSurvey', default_language=u'default', warnings=None, ):
    dict_repr = xls2json.workbook_to_json(ss_struct, default_name, default_language, warnings)
    dict_repr[u'name'] = dict_repr[u'id_string']
    return builder.create_survey_element_from_dict(dict_repr)
Пример #6
0
def formversion_pyxform(data):
    content = flatten_content(data)
    imported_survey_json = workbook_to_json(content)
    return create_survey_element_from_dict(imported_survey_json)