Example #1
0
    def publish(self, user, id_string=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']
                # "Note that any text-based field - such as CharField or
                # EmailField - always cleans the input into a Unicode string"
                # (https://docs.djangoproject.com/en/1.8/ref/forms/api/#django.forms.Form.cleaned_data).
                csv_data = csv_data.encode('utf-8')
                # requires that csv forms have a settings with an id_string or
                # form_id
                _sheets = csv_to_dict(StringIO(csv_data))
                try:
                    _settings = _sheets['settings'][0]
                    if 'id_string' in _settings:
                        _name = '%s.csv' % _settings['id_string']
                    else:
                        _name = '%s.csv' % _settings['form_id']
                except (KeyError, IndexError) as e:
                    raise ValueError('CSV XLSForms must have a settings sheet'
                                     ' and id_string or form_id')

                cleaned_xls_file = \
                    default_storage.save(
                        upload_to(None, _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)
            # publish the xls
            return publish_xls_form(cleaned_xls_file, user, id_string)
Example #2
0
 def set_form():
     form_owner = request.POST.get("username")
     id_string = request.POST.get("id_string")
     xform = XForm.objects.get(user__username=form_owner, id_string=id_string)
     if len(id_string) > 0 and id_string[0].isdigit():
         id_string = "_" + id_string
     path = xform.xls.name
     if default_storage.exists(path):
         xls_file = upload_to(None, "%s%s.xls" % (id_string, XForm.CLONED_SUFFIX), to_username)
         xls_data = default_storage.open(path)
         xls_file = default_storage.save(xls_file, xls_data)
         context.message = u"%s-%s" % (form_owner, xls_file)
         survey = DataDictionary.objects.create(user=request.user, xls=xls_file).survey
         # log to cloner's account
         audit = {}
         audit_log(
             Actions.FORM_CLONED,
             request.user,
             request.user,
             _("Cloned form '%(id_string)s'.") % {"id_string": survey.id_string},
             audit,
             request,
         )
         clone_form_url = reverse(
             show, kwargs={"username": to_username, "id_string": xform.id_string + XForm.CLONED_SUFFIX}
         )
         return {
             "type": "alert-success",
             "text": _(u"Successfully cloned to %(form_url)s into your " u"%(profile_url)s")
             % {
                 "form_url": u'<a href="%(url)s">%(id_string)s</a> '
                 % {"id_string": survey.id_string, "url": clone_form_url},
                 "profile_url": u'<a href="%s">profile</a>.' % reverse(profile, kwargs={"username": to_username}),
             },
         }
Example #3
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)
Example #4
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"].strip()
                    or self.cleaned_data["dropbox_xls_url"]
                    or self.cleaned_data["csv_url"]
                )

                cleaned_xls_file = urlparse(cleaned_url)
                cleaned_xls_file = "_".join(cleaned_xls_file.path.split("/")[-2:])
                name, extension = os.path.splitext(cleaned_xls_file)
                if extension not in [".xls", ".xlsx", ".csv"]:
                    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)
Example #5
0
 def save(self, **kwargs):
     user = User.objects.get(username=self.username)
     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
     )
Example #6
0
    def publish(self, user, id_string=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)
            # publish the xls
            return publish_xls_form(cleaned_xls_file, user, id_string)
Example #7
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
     )
Example #8
0
 def set_form():
     form_owner = request.POST.get('username')
     id_string = request.POST.get('id_string')
     xform = XForm.objects.get(user__username=form_owner,
                               id_string=id_string)
     if len(id_string) > 0 and id_string[0].isdigit():
         id_string = '_' + id_string
     path = xform.xls.name
     if default_storage.exists(path):
         xls_file = upload_to(None, '%s%s.xls' % (
                              id_string, XForm.CLONED_SUFFIX), to_username)
         xls_data = default_storage.open(path)
         xls_file = default_storage.save(xls_file, xls_data)
         context.message = u'%s-%s' % (form_owner, xls_file)
         survey = DataDictionary.objects.create(
             user=request.user,
             xls=xls_file
         ).survey
         # log to cloner's account
         audit = {}
         audit_log(
             Actions.FORM_CLONED, request.user, request.user,
             _("Cloned form '%(id_string)s'.") %
             {
                 'id_string': survey.id_string,
             }, audit, request)
         clone_form_url = reverse(
             show, kwargs={
                 'username': to_username,
                 'id_string': xform.id_string + XForm.CLONED_SUFFIX})
         return {
             'type': 'alert-success',
             'text': _(u'Successfully cloned to %(form_url)s into your '
                       u'%(profile_url)s') %
             {'form_url': u'<a href="%(url)s">%(id_string)s</a> ' % {
              'id_string': survey.id_string,
              'url': clone_form_url
              },
                 'profile_url': u'<a href="%s">profile</a>.' %
                 reverse(profile, kwargs={'username': to_username})}
         }
Example #9
0
    def publish(self, user, id_string=None, created_by=None):
        """
        Publish XLSForm.
        """
        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.
            cleaned_xls_file = None
            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))
            if 'xls_file' in self.cleaned_data and\
                    self.cleaned_data['xls_file']:
                cleaned_xls_file = self.cleaned_data['xls_file']
            if 'floip_file' in self.cleaned_data and\
                    self.cleaned_data['floip_file']:
                cleaned_xls_file = self.cleaned_data['floip_file']

            cleaned_url = (self.cleaned_data['xls_url'].strip()
                           or self.cleaned_data['dropbox_xls_url']
                           or self.cleaned_data['csv_url'])

            if cleaned_url:
                cleaned_xls_file = urlparse(cleaned_url)
                cleaned_xls_file = \
                    '_'.join(cleaned_xls_file.path.split('/')[-2:])
                name, extension = os.path.splitext(cleaned_xls_file)

                if extension not in VALID_FILE_EXTENSIONS and name:
                    response = requests.get(cleaned_url)
                    if response.headers.get('content-type') in \
                            VALID_XLSFORM_CONTENT_TYPES and \
                            response.status_code < 400:
                        cleaned_xls_file = get_filename(response)

                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

            cleaned_xml_file = self.cleaned_data['xml_file']
            if cleaned_xml_file:
                return publish_xml_form(cleaned_xml_file, user, project,
                                        id_string, created_by or user)

            if cleaned_xls_file is None:
                raise forms.ValidationError(
                    _(u"XLSForm not provided, expecting either of these"
                      " params: 'xml_file', 'xls_file', 'xls_url', 'csv_url',"
                      " 'dropbox_xls_url', 'text_xls_form', 'floip_file'"))
            # publish the xls
            return publish_xls_form(cleaned_xls_file, user, project, id_string,
                                    created_by or user)
Example #10
0
    def publish(self, user, id_string=None, created_by=None):
        """
        Publish XLSForm.
        """
        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.
            cleaned_xls_file = None
            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.encode()))
            if 'xls_file' in self.cleaned_data and\
                    self.cleaned_data['xls_file']:
                cleaned_xls_file = self.cleaned_data['xls_file']
            if 'floip_file' in self.cleaned_data and\
                    self.cleaned_data['floip_file']:
                cleaned_xls_file = self.cleaned_data['floip_file']

            cleaned_url = (
                self.cleaned_data['xls_url'].strip() or
                self.cleaned_data['dropbox_xls_url'] or
                self.cleaned_data['csv_url'])

            if cleaned_url:
                cleaned_xls_file = urlparse(cleaned_url)
                cleaned_xls_file = \
                    '_'.join(cleaned_xls_file.path.split('/')[-2:])
                name, extension = os.path.splitext(cleaned_xls_file)

                if extension not in VALID_FILE_EXTENSIONS and name:
                    response = requests.get(cleaned_url)
                    if response.headers.get('content-type') in \
                            VALID_XLSFORM_CONTENT_TYPES and \
                            response.status_code < 400:
                        cleaned_xls_file = get_filename(response)

                cleaned_xls_file = \
                    upload_to(None, cleaned_xls_file, user.username)
                self.validate(cleaned_url)
                xls_data = ContentFile(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

            cleaned_xml_file = self.cleaned_data['xml_file']
            if cleaned_xml_file:
                return publish_xml_form(cleaned_xml_file, user, project,
                                        id_string, created_by or user)

            if cleaned_xls_file is None:
                raise forms.ValidationError(
                    _(u"XLSForm not provided, expecting either of these"
                      " params: 'xml_file', 'xls_file', 'xls_url', 'csv_url',"
                      " 'dropbox_xls_url', 'text_xls_form', 'floip_file'"))
            # publish the xls
            return publish_xls_form(cleaned_xls_file, user, project,
                                    id_string, created_by or user)