Esempio n. 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")
Esempio n. 2
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",
    )
Esempio n. 3
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
Esempio n. 4
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)
Esempio n. 5
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
Esempio n. 6
0
    def _ss_structure_to_pyxform_survey(ss_structure, kwargs, warnings=None):
        # using existing methods from the builder
        imported_survey_json = workbook_to_json(ss_structure, warnings=warnings)
        # ideally, 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", "data")
        survey.title = kwargs.get("title")
        survey.id_string = kwargs.get("id_string")

        return survey
Esempio n. 7
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")
Esempio n. 8
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)
Esempio n. 9
0
def parse_xlsform(fp):
    warnings = []
    json_survey = xls2json.workbook_to_json(xls_to_dict(fp), None, 'default',
                                            warnings)
    survey = builder.create_survey_element_from_dict(json_survey)
    return survey.xml().toprettyxml(indent='  ')
Esempio n. 10
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)
Esempio n. 11
0
                        j['group'] = g
                        l.append(add_question(j))

            else:
                i['group'] = g
                l.append(add_question(i))
    return l


if __name__ == '__main__':
    XLS_FILE = 'data/codebook_xls.xls'
    # we can convert the xls to a better shaped json with pyxform
    from pyxform.xls2json_backends import xls_to_dict
    workbook_dict = xls_to_dict(XLS_FILE)
    from pyxform.xls2json import workbook_to_json
    workbook_json = workbook_to_json(workbook_dict,
                                     form_name=None,
                                     default_language=u"default",
                                     warnings=None)

    # cast the questions into a list of object, each with type, name, label, group and choices
    l = feed_workbook(workbook_json['children'])

    rows = ""
    for i in l:
        rows = rows + i.to_html()

    import html_utils as h
    html = h.html_head + h.html_body_header + ' '.join(
        rows.split()) + h.html_body_tail
Esempio n. 12
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)
Esempio n. 13
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)