Exemple #1
0
def restore_backup_from_xml_file(xml_instance_path, username):
    # check if its a valid xml instance
    file_name = os.path.basename(xml_instance_path)
    xml_file = django_file(
        xml_instance_path,
        field_name="xml_file",
        content_type="text/xml")
    media_files = []
    try:
        date_created = _date_created_from_filename(file_name)
    except ValueError as e:
        sys.stderr.write(
            "Couldn't determine date created from filename: '%s'\n" %
            file_name)
        date_created = datetime.now()

    sys.stdout.write("Creating instance from '%s'\n" % file_name)
    try:
        create_instance(
            username, xml_file, media_files,
            date_created_override=date_created)
        return 1
    except Exception as e:
        sys.stderr.write(
            "Could not restore %s, create instance said: %s\n" %
            (file_name, e))
        return 0
Exemple #2
0
 def _submit_at_hour(self, hour):
     st_xml = '<?xml version=\'1.0\' ?><start_time id="start_time"><st'\
              'art_time>2012-01-11T%d:00:00.000+00</start_time></start'\
              '_time>' % hour
     try:
         create_instance(self.user.username, TempFileProxy(st_xml), [])
     except DuplicateInstance:
         pass
Exemple #3
0
    def _tutorial_form_submission(self):
        tutorial_folder = os.path.join(
            os.path.dirname(__file__),
            '..', 'fixtures', 'forms', 'tutorial')
        self._publish_xls_file_and_set_xform(os.path.join(tutorial_folder,
                                                          'tutorial.xls'))
        instance_paths = [os.path.join(tutorial_folder, 'instances', i)
                          for i in ['1.xml', '2.xml', '3.xml']]
        for path in instance_paths:
            create_instance(self.user.username, open(path), [])

        self.assertEqual(self.xform.instances.count(), 3)
Exemple #4
0
    def callback(xform_fs):
        """
        This callback is passed an instance of a XFormInstanceFS.
        See xform_fs.py for more info.
        """
        with django_file(xform_fs.path,
                         field_name="xml_file",
                         content_type="text/xml") as xml_file:
            images = [
                django_file(jpg, field_name="image", content_type="image/jpeg")
                for jpg in xform_fs.photos
            ]
            # TODO: if an instance has been submitted make sure all the
            # files are in the database.
            # there shouldn't be any instances with a submitted status in the
            # import.
            instance = create_instance(user.username, xml_file, images, status)

            for i in images:
                i.close()

            if instance:
                return 1
            else:
                return 0
Exemple #5
0
 def _contributions_form_submissions(self):
     count = XForm.objects.count()
     path = os.path.join(os.path.dirname(__file__),
                         '..', 'fixtures', 'forms', 'contributions')
     form_path = os.path.join(path, 'contributions.xml')
     f = open(form_path)
     xml_file = ContentFile(f.read())
     f.close()
     xml_file.name = 'contributions.xml'
     project = get_user_default_project(self.user)
     self.xform = publish_xml_form(xml_file, self.user, project)
     self.assertTrue(XForm.objects.count() > count)
     instances_path = os.path.join(path, 'instances')
     for uuid in os.listdir(instances_path):
         s_path = os.path.join(instances_path, uuid, 'submission.xml')
         create_instance(self.user.username, open(s_path), [])
     self.assertEqual(self.xform.instances.count(), 6)
Exemple #6
0
def generate_instance(username, xml_file, media_files, uuid=None):
    ''' Process an XForm submission as if done via HTTP

        :param IO xml_file: file-like object containing XML XForm
        :param string username: username of the Form's owner
        :param list media_files: a list of UploadedFile objects
        :param string uuid: an optionnal uuid for the instance.

        :returns a (status, message) tuple. '''

    try:
        instance = create_instance(
            username,
            xml_file,
            media_files,
            uuid=uuid
        )
    except InstanceInvalidUserError:
        return {'code': SMS_SUBMISSION_REFUSED,
                'text': _(u"Username or ID required.")}
    except InstanceEmptyError:
        return {'code': SMS_INTERNAL_ERROR,
                'text': _(u"Received empty submission. "
                          u"No instance was created")}
    except FormInactiveError:
        return {'code': SMS_SUBMISSION_REFUSED,
                'text': _(u"Form is not active")}
    except XForm.DoesNotExist:
        return {'code': SMS_SUBMISSION_REFUSED,
                'text': _(u"Form does not exist on this account")}
    except ExpatError:
        return {'code': SMS_INTERNAL_ERROR,
                'text': _(u"Improperly formatted XML.")}
    except DuplicateInstance:
        return {'code': SMS_SUBMISSION_REFUSED,
                'text': _(u"Duplicate submission")}

    if instance is None:
        return {'code': SMS_INTERNAL_ERROR,
                'text': _(u"Unable to create submission.")}

    user = User.objects.get(username=username)

    audit = {
        "xform": instance.xform.id_string
    }
    audit_log(Actions.SUBMISSION_CREATED,
              user, instance.xform.user,
              _("Created submission on form %(id_string)s.") %
              {"id_string": instance.xform.id_string}, audit, HttpRequest())

    xml_file.close()
    if len(media_files):
        [_file.close() for _file in media_files]

    return {'code': SMS_SUBMISSION_ACCEPTED,
            'text': _(u"[SUCCESS] Your submission has been accepted."),
            'id': get_sms_instance_id(instance)}
    def _upload_instance(self, xml_file, instance_dir_path, files):
        xml_doc = clean_and_parse_xml(xml_file.read())
        xml = StringIO()
        de_node = xml_doc.documentElement
        for node in de_node.firstChild.childNodes:
            xml.write(node.toxml())
        new_xml_file = ContentFile(xml.getvalue())
        new_xml_file.content_type = 'text/xml'
        xml.close()
        attachments = []

        for attach in de_node.getElementsByTagName('mediaFile'):
            filename_node = attach.getElementsByTagName('filename')
            filename = filename_node[0].childNodes[0].nodeValue
            if filename in files:
                file_obj = default_storage.open(
                    os.path.join(instance_dir_path, filename))
                mimetype, encoding = mimetypes.guess_type(file_obj.name)
                media_obj = django_file(file_obj, 'media_files[]', mimetype)
                attachments.append(media_obj)

        create_instance(self.user.username, new_xml_file, attachments)
Exemple #8
0
    def callback(xform_fs):
        """
        This callback is passed an instance of a XFormInstanceFS.
        See xform_fs.py for more info.
        """
        with django_file(xform_fs.path, field_name="xml_file",
                         content_type="text/xml") as xml_file:
            images = [django_file(jpg, field_name="image",
                      content_type="image/jpeg") for jpg in xform_fs.photos]
            # TODO: if an instance has been submitted make sure all the
            # files are in the database.
            # there shouldn't be any instances with a submitted status in the
            # import.
            instance = create_instance(user.username, xml_file, images, status)

            for i in images:
                i.close()

            if instance:
                return 1
            else:
                return 0
Exemple #9
0
 def _submit_simple_yes(self):
     create_instance(self.user.username, TempFileProxy(
         '<?xml version=\'1.0\' ?><yes_or_no id="yes_or_no"><yesno>Yes<'
         '/yesno></yes_or_no>'), [])
Exemple #10
0
def generate_instance(username, xml_file, media_files, uuid=None):
    ''' Process an XForm submission as if done via HTTP

        :param IO xml_file: file-like object containing XML XForm
        :param string username: username of the Form's owner
        :param list media_files: a list of UploadedFile objects
        :param string uuid: an optionnal uuid for the instance.

        :returns a (status, message) tuple. '''

    try:
        instance = create_instance(username, xml_file, media_files, uuid=uuid)
    except InstanceInvalidUserError:
        return {
            'code': SMS_SUBMISSION_REFUSED,
            'text': _(u"Username or ID required.")
        }
    except InstanceEmptyError:
        return {
            'code': SMS_INTERNAL_ERROR,
            'text': _(u"Received empty submission. "
                      u"No instance was created")
        }
    except FormInactiveError:
        return {
            'code': SMS_SUBMISSION_REFUSED,
            'text': _(u"Form is not active")
        }
    except XForm.DoesNotExist:
        return {
            'code': SMS_SUBMISSION_REFUSED,
            'text': _(u"Form does not exist on this account")
        }
    except ExpatError:
        return {
            'code': SMS_INTERNAL_ERROR,
            'text': _(u"Improperly formatted XML.")
        }
    except DuplicateInstance:
        return {
            'code': SMS_SUBMISSION_REFUSED,
            'text': _(u"Duplicate submission")
        }

    if instance is None:
        return {
            'code': SMS_INTERNAL_ERROR,
            'text': _(u"Unable to create submission.")
        }

    user = User.objects.get(username=username)

    audit = {"xform": instance.xform.id_string}
    audit_log(
        Actions.SUBMISSION_CREATED, user, instance.xform.user,
        _("Created submission on form %(id_string)s.") %
        {"id_string": instance.xform.id_string}, audit, HttpRequest())

    xml_file.close()
    if len(media_files):
        [_file.close() for _file in media_files]

    return {
        'code': SMS_SUBMISSION_ACCEPTED,
        'text': _(u"[SUCCESS] Your submission has been accepted."),
        'id': get_sms_instance_id(instance)
    }