def publish_xml_form(xml_file, user, project, id_string=None, created_by=None): xml = xml_file.read() survey = create_survey_element_from_xml(xml) form_json = survey.to_json() if id_string: dd = DataDictionary.objects.get( user=user, id_string=id_string, project=project) dd.xml = xml dd.json = form_json dd._mark_start_time_boolean() set_uuid(dd) dd._set_uuid_in_xml() dd.save() return dd else: created_by = created_by or user dd = DataDictionary(created_by=created_by, user=user, xml=xml, json=form_json, project=project) dd._mark_start_time_boolean() set_uuid(dd) dd._set_uuid_in_xml(file_name=xml_file.name) dd.save() return dd
def publish_xml_form(xml_file, user, project, id_string=None, created_by=None): xml = xml_file.read() survey = create_survey_element_from_xml(xml) form_json = survey.to_json() if id_string: dd = DataDictionary.objects.get(user=user, id_string=id_string, project=project) dd.xml = xml dd.json = form_json dd._mark_start_time_boolean() set_uuid(dd) dd._set_uuid_in_xml() dd.save() return dd else: created_by = created_by or user dd = DataDictionary(created_by=created_by, user=user, xml=xml, json=form_json, project=project) dd._mark_start_time_boolean() set_uuid(dd) dd._set_uuid_in_xml(file_name=xml_file.name) dd.save() return dd
def publish_xml_form(xml_file, user, project, id_string=None, created_by=None): xml = xml_file.read() if isinstance(xml, bytes): xml = xml.decode('utf-8') survey = create_survey_element_from_xml(xml) form_json = survey.to_json() if id_string: dd = DataDictionary.objects.get(user=user, id_string=id_string, project=project) dd.xml = xml dd.json = form_json dd._mark_start_time_boolean() set_uuid(dd) dd._set_uuid_in_xml() dd._set_hash() dd.save() else: created_by = created_by or user dd = DataDictionary(created_by=created_by, user=user, xml=xml, json=form_json, project=project) dd._mark_start_time_boolean() set_uuid(dd) dd._set_uuid_in_xml(file_name=xml_file.name) dd._set_hash() dd.save() # Create an XFormVersion object for the published XLSForm create_xform_version(dd, user) return dd
def get_survey(self): if not hasattr(self, "_survey"): try: builder = SurveyElementBuilder() self._survey = builder.create_survey_element_from_json(self.json) except ValueError: xml = bytes(bytearray(self.xml, encoding="utf-8")) self._survey = create_survey_element_from_xml(xml) return self._survey
def get_survey(self): if not hasattr(self, "_survey"): try: builder = SurveyElementBuilder() self._survey = \ builder.create_survey_element_from_json(self.json) except ValueError: xml = b(bytearray(self.xml, encoding='utf-8')) self._survey = create_survey_element_from_xml(xml) return self._survey
def build_survey_from_history(xform, __version__): if not XformHistory.objects.filter(xform=xform, version=__version__).exists(): return False history = XformHistory.objects.get(xform=xform, version=__version__) try: builder = SurveyElementBuilder() survey = builder.create_survey_element_from_json(history.json) except ValueError: xml = bytes(bytearray(history.xml, encoding='utf-8')) survey = create_survey_element_from_xml(xml) return survey
def handle(self, *args, **options): path = args[0] try: username = args[1] except IndexError: raise CommandError("You must provide the username to publish the forms to.") # make sure user exists try: user = User.objects.get(username=username) except User.DoesNotExist: raise CommandError("The user '{}' does not exist.".format(username)) for form in glob.glob( os.path.join(path, "*") ): print('Publishing form "{}"...'.format(form)) f = open(form) xml = f.read() id_string = get_id_string_from_xml_str(xml) # check if a form with this id_string exists for this user form_already_exists = XForm.objects.filter(user=user, id_string=id_string).count() > 0 if form_already_exists: if options.has_key('replace') and options['replace']: XForm.objects.filter(user=user, id_string=id_string).delete() else: raise CommandError('form "{}" is already defined, and --replace was not specified.'.format( id_string)) if not XForm.can_be_replaced(): raise CommandError('form "{}" has submissions of file -- cannot replace'.format(id_string)) try: survey = create_survey_element_from_xml(xml) except (AssertionError, TypeError) as e: print(repr(e.args)) # pass on the error message (works in Python 2 and 3) else: form_json = survey.to_json() XForm.objects.get_or_create(xml=xml, downloadable=True, user=user, id_string=id_string, json=form_json) print(' (Done).') f.close() print()
def test_load_from_dump(self): for filename, survey in iter(self.surveys.items()): survey.json_dump() survey_from_dump = create_survey_element_from_xml(survey.to_xml()) self.assertXFormEqual( survey.to_xml(), survey_from_dump.to_xml())
def test_load_from_dump(self): for filename, survey in iter(self.surveys.items()): survey.json_dump() survey_from_dump = create_survey_element_from_xml(survey.to_xml()) self.assertXFormEqual(survey.to_xml(), survey_from_dump.to_xml())