Exemplo n.º 1
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}),
             },
         }
Exemplo n.º 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
         return {
             'type': 'alert-success',
             'text': 'Successfully cloned %s into your '\
                     '<a href="%s">profile</a>.' % \
                     (survey.id_string, reverse(profile,
                         kwargs={'username': to_username}))
             }
Exemplo n.º 3
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
         return {
             'type': 'alert-success',
             'text': _(u'Successfully cloned %(id_string)s into your '
                       u'%(profile_url)s') % {
                           'id_string': survey.id_string,
                           'profile_url': u'<a href="%s">profile</a>.' %
                           reverse(profile,
                                   kwargs={'username': to_username})
                       }
         }
Exemplo n.º 4
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)
                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 cleaned_xls_file and not settings.TESTING_MODE:
                    #We need to save it here so if the file already exists we get the _N filename
                    cleaned_xls_file = default_storage.save(\
                        cleaned_xls_file.name, \
                        ContentFile(cleaned_xls_file.read()))

            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)
Exemplo n.º 5
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)
Exemplo n.º 6
0
def clone_xlsform(request, username):
    """
    Copy a public/Shared form to a users list of forms.
    Eliminates the need to download Excel File and upload again.
    """
    to_username = request.user.username
    context = RequestContext(request)
    context.message = {'type': None, 'text': '....'}

    try:
        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, id_string + '_cloned.xls', 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
            context.message = {
                'type': 'success',
                'text': 'Successfully cloned %s into your '\
                        '<a href="%s">profile</a>.' % \
                        (survey.id_string, reverse(profile,
                            kwargs={'username': to_username}))
                }
    except (PyXFormError, XLSFormError) as e:
        context.message = {
            'type': 'error',
            'text': unicode(e),
            }
    except IntegrityError as e:
        context.message = {
            'type': 'error',
            'text': 'Form with this id already exists.',
            }
    if request.is_ajax():
        return HttpResponse(simplejson.dumps(context.message), \
                        mimetype='application/json')
    else:
        return HttpResponse(context.message['text'])
Exemplo n.º 7
0
 def publish(self, user):
     if self.is_valid():
         cleaned_xls_file = self.cleaned_data['xls_file']
         if not cleaned_xls_file:
             cleaned_url = self.cleaned_data['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)
             xls_data = ContentFile(urllib2.urlopen(cleaned_url).read())
             cleaned_xls_file = default_storage.save(cleaned_xls_file, xls_data)
         return DataDictionary.objects.create(
             user=user,
             xls=cleaned_xls_file
             )
Exemplo n.º 8
0
 def publish(self, user, id_string=None):
     if self.is_valid():
         cleaned_xls_file = self.cleaned_data['xls_file']
         if not cleaned_xls_file:
             cleaned_url = self.cleaned_data['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)
Exemplo n.º 9
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
         return {
             "type": "alert-success",
             "text": "Successfully cloned %s into your "
             '<a href="%s">profile</a>.' % (survey.id_string, reverse(profile, kwargs={"username": to_username})),
         }
Exemplo n.º 10
0
 def publish(self, user):
     if self.is_valid():
         cleaned_xls_file = self.cleaned_data['xls_file']
         if not cleaned_xls_file:
             cleaned_url = self.cleaned_data['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)
         return DataDictionary.objects.create(user=user,
                                              xls=cleaned_xls_file)
Exemplo n.º 11
0
 def publish(self, user, id_string=None):
     if self.is_valid():
         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)
Exemplo n.º 12
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})}
         }