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
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
 def _set_uuid(self):
     # pylint: disable=no-member, attribute-defined-outside-init
     if self.xml and not self.uuid:
         # pylint: disable=no-member
         uuid = get_uuid_from_xml(self.xml)
         if uuid is not None:
             self.uuid = uuid
     set_uuid(self)
Exemple #5
0
 def _set_uuid(self):
     # pylint: disable=no-member, attribute-defined-outside-init
     if self.xml and not self.uuid:
         # pylint: disable=no-member
         uuid = get_uuid_from_xml(self.xml)
         if uuid is not None:
             self.uuid = uuid
     set_uuid(self)
Exemple #6
0
 def _set_uuid(self):
     # pylint: disable=E1101, E0203
     if self.xml and not self.uuid:
         # pylint: disable=E1101
         uuid = get_uuid_from_xml(self.xml)
         if uuid is not None:
             self.uuid = uuid
     set_uuid(self)
Exemple #7
0
 def save(self, *args, **kwargs):
     if self.xls:
         survey = create_survey_from_xls(self.xls)
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
 def save(self, *args, **kwargs):
     if self.xls:
         survey = create_survey_from_xls(self.xls)
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
Exemple #9
0
 def save(self, *args, **kwargs):
     if self.xls:
         # check if version is set
         survey = self._check_version_set(create_survey_from_xls(self.xls))
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self.version = survey.get('version')
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
Exemple #10
0
 def save(self, *args, **kwargs):
     if self.xls:
         # check if version is set
         survey = self._check_version_set(create_survey_from_xls(self.xls))
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self.version = survey.get('version')
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
Exemple #11
0
 def save(self, *args, **kwargs):
     if self.xls:
         survey = create_survey_from_xls(self.xls)
         survey.update({
             'name': survey.id_string,
         })
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self._mark_start_time_boolean()
         set_uuid(self)
         self.set_uuid_in_xml(id_string=survey.id_string)
     super().save(*args, **kwargs)
Exemple #12
0
    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)
Exemple #13
0
    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)
Exemple #14
0
 def save(self, *args, **kwargs):
     if self.xls:
         # check if version is set
         excel_reader = SurveyReader(self.xls)
         survey_dict = excel_reader.to_json_dict()
         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)
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self.version = survey.get('version')
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
 def save(self, *args, **kwargs):
     if self.xls:
         # check if version is set
         excel_reader = SurveyReader(self.xls)
         survey_dict = excel_reader.to_json_dict()
         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)
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self.version = survey.get('version')
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
Exemple #16
0
 def save(self, *args, **kwargs):
     if self.xls:
         default_name = None \
             if not self.pk else self.survey.xml_instance().tagName
         try:
             survey_dict = parse_file_to_json(self.xls.name,
                                              default_name=default_name,
                                              file_object=self.xls)
         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 form is being replaced, don't check for id_string uniqueness
         if self.pk is None:
             survey['id_string'] = self.get_unique_id_string(
                 survey.get('id_string'))
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self.version = survey.get('version')
         self.title = survey.get('title')
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     super(DataDictionary, self).save(*args, **kwargs)
 def save(self, *args, **kwargs):
     if self.xls:
         default_name = None \
             if not self.pk else self.survey.xml_instance().tagName
         try:
             survey_dict = parse_file_to_json(
                 self.xls.name, default_name=default_name,
                 file_object=self.xls)
         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 form is being replaced, don't check for id_string uniqueness
         if self.pk is None:
             survey['id_string'] = self.get_unique_id_string(
                 survey.get('id_string'))
         self.json = survey.to_json()
         self.xml = survey.to_xml()
         self.version = survey.get('version')
         self.title = survey.get('title')
         self._mark_start_time_boolean()
         set_uuid(self)
         self._set_uuid_in_xml()
     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)
Exemple #19
0
 def _set_uuid(self):
     if self.xml and not self.uuid:
         uuid = get_uuid_from_xml(self.xml)
         if uuid is not None:
             self.uuid = uuid
     set_uuid(self)
Exemple #20
0
 def save(self, *args, **kwargs):
     set_uuid(self)
     return super(MergedXForm, self).save(*args, **kwargs)
Exemple #21
0
 def _set_uuid(self):
     if self.xml and not self.uuid:
         uuid = get_uuid_from_xml(self.xml)
         if uuid is not None:
             self.uuid = uuid
     set_uuid(self)