Esempio n. 1
0
 def save(self, **kwargs):
     user = User.objects.get(username=self.username)
     project = get_user_default_project(user)
     xls_file_path = upload_to(
         None, '%s%s.xls' % (self.xform.id_string, XForm.CLONED_SUFFIX),
         self.username)
     xls_data = default_storage.open(self.xform.xls.name)
     xls_file = default_storage.save(xls_file_path, xls_data)
     self.cloned_form = DataDictionary.objects.create(user=user,
                                                      xls=xls_file,
                                                      project=project)
Esempio n. 2
0
 def save(self, **kwargs):
     user = User.objects.get(username=self.username)
     project = get_user_default_project(user)
     xls_file_path = upload_to(None, '%s%s.xls' % (
                               self.xform.id_string,
                               XForm.CLONED_SUFFIX),
                               self.username)
     xls_data = default_storage.open(self.xform.xls.name)
     xls_file = default_storage.save(xls_file_path, xls_data)
     self.cloned_form = DataDictionary.objects.create(
         user=user,
         xls=xls_file,
         project=project
     )
Esempio n. 3
0
    def handle(self, *args, **options):
        try:
            xls_filepath = args[0]
        except IndexError:
            raise CommandError(_("You must provide the path to the xls file."))
        # make sure path exists
        if not os.path.exists(xls_filepath):
            raise CommandError(
                _("The xls file '%s' does not exist.") %
                xls_filepath)

        try:
            username = args[1]
        except IndexError:
            raise CommandError(_(
                "You must provide the username to publish the form to."))
        # make sure user exists
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            raise CommandError(_("The user '%s' does not exist.") % username)

        # wasteful but we need to get the id_string beforehand
        survey = create_survey_from_xls(xls_filepath)

        # check if a form with this id_string exists for this user
        form_already_exists = XForm.objects.filter(
            user=user, id_string=survey.id_string).count() > 0

        # id_string of form to replace, if any
        id_string = None
        if form_already_exists:
            if 'replace' in options and options['replace']:
                id_string = survey.id_string
                self.stdout.write(_("Form already exist, replacing ..\n"))
            else:
                raise CommandError(_(
                    "The form with id_string '%s' already exists, use the -r "
                    "option to replace it.") % survey.id_string)
        else:
            self.stdout.write(_("Form does NOT exist, publishing ..\n"))

        project = get_user_default_project(user)

        # publish
        xls_file = django_file(
            xls_filepath, 'xls_file', 'application/vnd.ms-excel')
        publish_xls_form(xls_file, user, project, id_string)
        self.stdout.write(_("Done..\n"))
Esempio n. 4
0
    def handle(self, *args, **options):
        try:
            xls_filepath = args[0]
        except IndexError:
            raise CommandError(_("You must provide the path to the xls file."))
        # make sure path exists
        if not os.path.exists(xls_filepath):
            raise CommandError(
                _("The xls file '%s' does not exist.") % xls_filepath)

        try:
            username = args[1]
        except IndexError:
            raise CommandError(
                _("You must provide the username to publish the form to."))
        # make sure user exists
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            raise CommandError(_("The user '%s' does not exist.") % username)

        # wasteful but we need to get the id_string beforehand
        survey = create_survey_from_xls(xls_filepath)

        # check if a form with this id_string exists for this user
        form_already_exists = XForm.objects.filter(
            user=user, id_string=survey.id_string).count() > 0

        # id_string of form to replace, if any
        id_string = None
        if form_already_exists:
            if 'replace' in options and options['replace']:
                id_string = survey.id_string
                self.stdout.write(_("Form already exist, replacing ..\n"))
            else:
                raise CommandError(
                    _("The form with id_string '%s' already exists, use the -r "
                      "option to replace it.") % survey.id_string)
        else:
            self.stdout.write(_("Form does NOT exist, publishing ..\n"))

        project = get_user_default_project(user)

        # publish
        xls_file = django_file(xls_filepath, 'xls_file',
                               'application/vnd.ms-excel')
        publish_xls_form(xls_file, user, project, id_string)
        self.stdout.write(_("Done..\n"))
Esempio n. 5
0
    def publish(self, user, id_string=None, created_by=None):
        if self.is_valid():
            # If a text (csv) representation of the xlsform is present,
            # this will save the file and pass it instead of the 'xls_file'
            # field.
            if 'text_xls_form' in self.cleaned_data\
               and self.cleaned_data['text_xls_form'].strip():
                csv_data = self.cleaned_data['text_xls_form']

                # assigning the filename to a random string (quick fix)
                import random
                rand_name = "uploaded_form_%s.csv" % ''.join(
                    random.sample("abcdefghijklmnopqrstuvwxyz0123456789", 6))

                cleaned_xls_file = \
                    default_storage.save(
                        upload_to(None, rand_name, user.username),
                        ContentFile(csv_data))
            else:
                cleaned_xls_file = self.cleaned_data['xls_file']

            if not cleaned_xls_file:
                cleaned_url = self.cleaned_data['xls_url']
                if cleaned_url.strip() == u'':
                    cleaned_url = self.cleaned_data['dropbox_xls_url']
                cleaned_xls_file = urlparse(cleaned_url)
                cleaned_xls_file = \
                    '_'.join(cleaned_xls_file.path.split('/')[-2:])
                if cleaned_xls_file[-4:] != '.xls':
                    cleaned_xls_file += '.xls'
                cleaned_xls_file = \
                    upload_to(None, cleaned_xls_file, user.username)
                self.validate(cleaned_url)
                xls_data = ContentFile(urllib2.urlopen(cleaned_url).read())
                cleaned_xls_file = \
                    default_storage.save(cleaned_xls_file, xls_data)

            project = self.cleaned_data['project']

            if project is None:
                project = get_user_default_project(user)
            else:
                project = self._project

            # publish the xls
            return publish_xls_form(cleaned_xls_file, user, project, id_string,
                                    created_by or user)
Esempio n. 6
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)
Esempio n. 7
0
 def test_publish_xml_xlsform_download(self):
     count = XForm.objects.count()
     path = os.path.join(
         self.this_directory, '..', '..', 'api', 'tests', 'fixtures',
         'forms', 'contributions', 'contributions.xml')
     f = open(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)
     response = self.client.get(reverse(download_xlsform, kwargs={
         'username': self.user.username,
         'id_string': 'contributions'
     }), follow=True)
     self.assertContains(response, 'No XLS file for your form ')
    def setUp(self):
        self.user = User.objects.create(username="******")
        profile, c = UserProfile.objects.get_or_create(user=self.user)
        profile.require_auth = False
        profile.save()
        self.project = get_user_default_project(self.user)

        absolute_path = get_absolute_path("forms")
        open_forms = open_all_files(absolute_path)
        self.json = '{"default_language": "default", ' \
                    '"id_string": "Water_2011_03_17", "children": [], ' \
                    '"name": "Water_2011_03_17", ' \
                    '"title": "Water_2011_03_17", "type": "survey"}'
        for path, open_file in open_forms.items():
            XForm.objects.create(
                xml=open_file.read(), user=self.user, json=self.json,
                require_auth=False, project=self.project)
            open_file.close()

        self._create_water_translated_form()
    def setUp(self):
        self.user = User.objects.create(username="******")
        profile, c = UserProfile.objects.get_or_create(user=self.user)
        profile.require_auth = False
        profile.save()
        self.project = get_user_default_project(self.user)

        absolute_path = get_absolute_path("forms")
        open_forms = open_all_files(absolute_path)
        self.json = '{"default_language": "default", ' \
                    '"id_string": "Water_2011_03_17", "children": [], ' \
                    '"name": "Water_2011_03_17", ' \
                    '"title": "Water_2011_03_17", "type": "survey"}'
        for path, open_file in open_forms.items():
            XForm.objects.create(xml=open_file.read(),
                                 user=self.user,
                                 json=self.json,
                                 require_auth=False,
                                 project=self.project)
            open_file.close()

        self._create_water_translated_form()
Esempio n. 10
0
    def setUp(self):
        self.user = User.objects.create(
            username="******", email="*****@*****.**")
        self.project = UserProfile.objects.create(user=self.user)
        self.user.set_password("pass")
        self.project = get_user_default_project(self.user)
        self.xform1 = DataDictionary()
        self.xform1.user = self.user
        self.xform1.project = self.project
        self.xform1.json = '{"id_string": "yes_or_no", "children": [{"name": '\
                           '"yesno", "label": "Yes or no?", "type": "text"}],'\
                           ' "name": "yes_or_no", "title": "yes_or_no", "type'\
                           '": "survey"}'.strip()
        self.xform2 = DataDictionary()
        self.xform2.user = self.user
        self.xform2.project = self.project
        self.xform2.json = '{"id_string": "start_time", "children": [{"name":'\
                           '"start_time", "type": "start"}], "name": "start_t'\
                           'ime", "title": "start_time", "type": "survey"}'\
                           .strip()

        self._get_xml_for_form(self.xform1)
        self._get_xml_for_form(self.xform2)
Esempio n. 11
0
 def __init__(self, xml_file, user):
     self.xml_file = xml_file
     self.user = user
     self.project = get_user_default_project(user)