def summarize_survey_structure(ss_structure, _type): valid_ss_structure = convert_any_kobo_features_to_xlsform_survey_structure( ss_structure) survey = create_survey_from_ss_struct(valid_ss_structure) if _type == 'question': question_type = survey.children[0].type label = survey.children[0].label if question_type == 'calculate': label = survey.children[0].calculation if isinstance(label, dict): _langs = label.keys() if 'default' in _langs: _langs.remove('default') label = label[_langs[0]] return { 'type': 'select_multiple' if question_type == 'select all that apply' else question_type, 'label': label, 'options': [option.label for option in survey.children[0].children] } else: return {'form_id': survey.id_string}
def convert_survey(surv, choices=[], sheets={}): sheets.update({ 'survey': surv }) if len(choices) > 0: sheets.update({ 'choices': choices }) return convert_any_kobo_features_to_xlsform_survey_structure(sheets)
def validate_kobo_xlsform(posted_file, warnings=[]): ''' used on import of survey_draft xls files, this will convert any kobo structures to valid xlsform rules and then build the xform to ensure the survey passes through pyxform's validator. ''' unvalidated_csv_xlsform = _smart_unicode(convert_xls_to_csv_string(posted_file)) ss_struct = convert_csv_to_ss_structure(unvalidated_csv_xlsform) valid_ss_structure = convert_any_kobo_features_to_xlsform_survey_structure(ss_struct) return create_survey_from_ss_struct(valid_ss_structure, warnings=warnings).to_xml()
def validate_kobo_xlsform(posted_file, warnings=[]): ''' used on import of survey_draft xls files, this will convert any kobo structures to valid xlsform rules and then build the xform to ensure the survey passes through pyxform's validator. ''' unvalidated_csv_xlsform = _smart_unicode( convert_xls_to_csv_string(posted_file)) ss_struct = convert_csv_to_ss_structure(unvalidated_csv_xlsform) valid_ss_structure = convert_any_kobo_features_to_xlsform_survey_structure( ss_struct) return create_survey_from_ss_struct(valid_ss_structure, warnings=warnings).to_xml()
def publish_survey_draft(request, pk, format=None): if not kobocat_integration._is_enabled(): return Response({'error': 'KoBoCat Server not specified'}, status=status.HTTP_503_SERVICE_UNAVAILABLE) try: survey_draft = SurveyDraft.objects.get(pk=pk, user=request.user) except SurveyDraft.DoesNotExist: return Response({'error': 'SurveyDraft not found'}, status=status.HTTP_404_NOT_FOUND) # convert csv to ss_struct ss_struct = pyxform_utils.convert_csv_to_ss_structure(survey_draft.body) form_id_string = request.data.get('id_string', False) # set the form_id based on the payload if 'settings' not in ss_struct: ss_struct['settings'] = [{}] elif len(ss_struct['settings']) == 0: ss_struct['settings'].append({}) if form_id_string: ss_struct['settings'][0]['form_id'] = form_id_string # convert kobo-specific data structures into valid xlsform (e.g. score, rank) xlsform_ss_struct = convert_any_kobo_features_to_xlsform_survey_structure( ss_struct) valid_xlsform_csv_repr = pyxform_utils.convert_ss_structure_to_csv( xlsform_ss_struct) (token, is_new) = Token.objects.get_or_create(user=request.user) headers = {u'Authorization': 'Token ' + token.key} payload = {u'text_xls_form': valid_xlsform_csv_repr} try: url = kobocat_integration._kobocat_url('/api/v1/forms', internal=True) response = requests.post(url, headers=headers, data=payload) status_code = response.status_code resp = response.json() except Exception, e: resp = {'status_code': 504, 'detail': str(e)} status_code = 504
def summarize_survey_structure(ss_structure, _type): valid_ss_structure = convert_any_kobo_features_to_xlsform_survey_structure(ss_structure) survey = create_survey_from_ss_struct(valid_ss_structure) if _type == 'question': question_type = survey.children[0].type label = survey.children[0].label if question_type == 'calculate': label = survey.children[0].calculation if isinstance(label, dict): _langs = label.keys() if 'default' in _langs: _langs.remove('default') label = label[_langs[0]] return { 'type': 'select_multiple' if question_type == 'select all that apply' else question_type, 'label': label, 'options': [option.label for option in survey.children[0].children] } else: return {'form_id': survey.id_string}
def publish_survey_draft(request, pk, format=None): if not kobocat_integration._is_enabled(): return Response({'error': 'KoBoCat Server not specified'}, status=status.HTTP_503_SERVICE_UNAVAILABLE) try: survey_draft = SurveyDraft.objects.get(pk=pk, user=request.user) except SurveyDraft.DoesNotExist: return Response({'error': 'SurveyDraft not found'}, status=status.HTTP_404_NOT_FOUND) # convert csv to ss_struct ss_struct = pyxform_utils.convert_csv_to_ss_structure(survey_draft.body) form_id_string = request.DATA.get('id_string', False) # set the form_id based on the payload if 'settings' not in ss_struct: ss_struct['settings'] = [{}] elif len(ss_struct['settings']) == 0: ss_struct['settings'].append({}) if form_id_string: ss_struct['settings'][0]['form_id'] = form_id_string # convert kobo-specific data structures into valid xlsform (e.g. score, rank) xlsform_ss_struct = convert_any_kobo_features_to_xlsform_survey_structure(ss_struct) valid_xlsform_csv_repr = pyxform_utils.convert_ss_structure_to_csv(xlsform_ss_struct) _set_necessary_permissions(request.user) (token, is_new) = Token.objects.get_or_create(user=request.user) headers = {u'Authorization':'Token ' + token.key} payload = {u'text_xls_form': valid_xlsform_csv_repr} try: url = kobocat_integration._kobocat_url('/api/v1/forms', internal=True) response = requests.post(url, headers=headers, data=payload) status_code = response.status_code resp = response.json() except Exception, e: resp = {'status_code': 504, 'detail': str(e)} status_code = 504
def convert_csv_to_valid_xlsform_unicode_csv(csv_str): ss_struct = convert_csv_to_ss_structure(csv_str) valid_ss_structure = convert_any_kobo_features_to_xlsform_survey_structure( ss_struct) return unicode(convert_ss_structure_to_csv(valid_ss_structure), 'utf-8')
def convert_survey(surv, choices=[], sheets={}): sheets.update({'survey': surv}) if len(choices) > 0: sheets.update({'choices': choices}) return convert_any_kobo_features_to_xlsform_survey_structure(sheets)
def convert_csv_to_valid_xlsform_unicode_csv(csv_str): ss_struct = convert_csv_to_ss_structure(csv_str) valid_ss_structure = convert_any_kobo_features_to_xlsform_survey_structure(ss_struct) return unicode(convert_ss_structure_to_csv(valid_ss_structure), 'utf-8')