def test_floip_descriptor_to_xform(): """ Test FloipSurvey - converting a flow result descriptor to an XForm xml. """ survey = FloipSurvey('data/flow-results-example-1.json') with codecs.open('data/flow-results-example-1.xml') as xform_file: assert survey.xml() == xform_file.read()
def test_xform_to_floip_descriptor(): """ Test FloipSurvey - converting a flow result descriptor to an XForm xml. """ survey = FloipSurvey('data/flow-results-example-1.json') with codecs.open('data/flow-results-example-1.xml') as xform_file: assert survey.xml() == xform_file.read() with codecs.open('data/flow-results-example-1.json') as descriptor_file: package = survey_to_floip_package( survey.survey_dict(), survey.descriptor['id'], survey.descriptor['created'], survey.descriptor['modified'], 'data/flow-results-example-1-data.json') assert package.descriptor == json.load(descriptor_file) assert package.valid is True
def test_floip_descriptor_to_xform_questions_as_list(): # pylint: disable=C0103 """ Test FloipSurvey - converting a flow result descriptor to an XForm xml when resource questions is a list. """ survey = FloipSurvey('data/flow-results-example-2.json') with codecs.open('data/flow-results-example-1.xml') as xform_file: assert survey.xml() == xform_file.read() with codecs.open('data/flow-results-example-1.json') as descriptor_file: package = survey_to_floip_package( survey.survey_dict(), survey.descriptor['id'], survey.descriptor['created'], survey.descriptor['modified'], 'data/flow-results-example-1-data.json') assert package.descriptor == json.load(descriptor_file) assert package.valid is True
def process_xlsform(xls, default_name): """ Process XLSForm file and return the survey dictionary for the XLSForm. """ # FLOW Results package is a JSON file. if xls.name.endswith('json'): return FloipSurvey(xls).survey.to_json_dict() file_object = None if xls.name.endswith('csv'): # a csv file gets closed in pyxform, make a copy xls.seek(0) file_object = BytesIO() file_object.write(xls.read()) file_object.seek(0) xls.seek(0) try: return parse_file_to_json(xls.name, file_object=file_object or xls) except csv.Error as e: if is_newline_error(e): xls.seek(0) file_object = StringIO(u'\n'.join(xls.read().splitlines())) return parse_file_to_json(xls.name, default_name=default_name, file_object=file_object) raise e
def save(self, *args, **kwargs): skip_xls_read = kwargs.get('skip_xls_read') if self.xls and not skip_xls_read: default_name = None \ if not self.pk else self.survey.xml_instance().tagName survey_dict = process_xlsform(self.xls, default_name) if has_external_choices(survey_dict): self.has_external_choices = True survey = create_survey_element_from_dict(survey_dict) survey = check_version_set(survey) if get_columns_with_hxl(survey.get('children')): self.has_hxl_support = True # if form is being replaced, don't check for id_string uniqueness if self.pk is None: new_id_string = self.get_unique_id_string( survey.get('id_string')) self._id_string_changed = \ new_id_string != survey.get('id_string') survey['id_string'] = new_id_string # For flow results packages use the user defined id/uuid if self.xls.name.endswith('json'): self.uuid = FloipSurvey(self.xls).descriptor.get('id') if self.uuid: check_xform_uuid(self.uuid) elif self.id_string != survey.get('id_string'): raise XLSFormError( _((u"Your updated form's id_string '%(new_id)s' must match " "the existing forms' id_string '%(old_id)s'." % { 'new_id': survey.get('id_string'), 'old_id': self.id_string }))) elif default_name and default_name != survey.get('name'): survey['name'] = default_name else: survey['id_string'] = self.id_string self.json = survey.to_json() self.xml = survey.to_xml() self.version = survey.get('version') self.last_updated_at = timezone.now() self.title = survey.get('title') self._mark_start_time_boolean() set_uuid(self) self._set_uuid_in_xml() self._set_hash() if 'skip_xls_read' in kwargs: del kwargs['skip_xls_read'] super(DataDictionary, self).save(*args, **kwargs)
def save(self, *args, **kwargs): skip_xls_read = kwargs.get('skip_xls_read') if self.xls and not skip_xls_read: default_name = None \ if not self.pk else self.survey.xml_instance().tagName try: if self.xls.name.endswith('csv'): # csv file gets closed in pyxform, make a copy self.xls.seek(0) file_object = io.BytesIO() file_object.write(self.xls.read()) file_object.seek(0) self.xls.seek(0) else: file_object = self.xls if self.xls.name.endswith('json'): survey_dict = FloipSurvey(self.xls).survey.to_json_dict() else: survey_dict = parse_file_to_json(self.xls.name, file_object=file_object) except csv.Error as e: newline_error = u'new-line character seen in unquoted field '\ u'- do you need to open the file in universal-newline '\ u'mode?' if newline_error == unicode(e): self.xls.seek(0) file_obj = StringIO(u'\n'.join( self.xls.read().splitlines())) survey_dict = parse_file_to_json(self.xls.name, default_name=default_name, file_object=file_obj) else: raise e if has_external_choices(survey_dict): self.survey_dict = survey_dict self.has_external_choices = True survey = create_survey_element_from_dict(survey_dict) survey = self._check_version_set(survey) if get_columns_with_hxl(survey.get('children')): self.has_hxl_support = True # if form is being replaced, don't check for id_string uniqueness if self.pk is None: new_id_string = self.get_unique_id_string( survey.get('id_string')) self._id_string_changed = \ new_id_string != survey.get('id_string') survey['id_string'] = new_id_string elif self.id_string != survey.get('id_string'): raise XLSFormError( _((u"Your updated form's id_string '%(new_id)s' must match " "the existing forms' id_string '%(old_id)s'." % { 'new_id': survey.get('id_string'), 'old_id': self.id_string }))) elif default_name and default_name != survey.get('name'): survey['name'] = default_name else: survey['id_string'] = self.id_string self.json = survey.to_json() self.xml = survey.to_xml() self.version = survey.get('version') self.last_updated_at = timezone.now() self.title = survey.get('title') self._mark_start_time_boolean() self._set_hash() set_uuid(self) self._set_uuid_in_xml() if 'skip_xls_read' in kwargs: del kwargs['skip_xls_read'] super(DataDictionary, self).save(*args, **kwargs)
def cli(descriptor): """ Outputs the XForm of a given FlOIP results data package descriptor. """ survey = FloipSurvey(descriptor) click.echo(survey.xml())