Example #1
0
 def test_dont_inject_instanceid_if_exists(self):
     xls_file_path = os.path.join(os.path.dirname(__file__), '..',
                                  'fixtures', 'tutorial', 'tutorial.xls')
     self._publish_xls_file_and_set_xform(xls_file_path)
     xml_file_path = os.path.join(
         os.path.dirname(__file__), '..', 'fixtures', 'tutorial',
         'instances', 'tutorial_2012-06-27_11-27-53_w_uuid.xml')
     self._make_submission(xml_file_path)
     instance = Instance.objects.order_by('id').reverse()[0]
     injected_xml_str = inject_instanceid(instance.xml, instance.uuid)
     # check that the xml is unmodified
     self.assertEqual(instance.xml, injected_xml_str)
Example #2
0
 def test_inject_instanceid(self):
     """
     Test that 1 and only 1 instance id exists or is injected
     """
     instance = Instance.objects.all().reverse()[0]
     xml_str = self.__load_fixture("..", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53.xml")
     # test that we dont have an instance id
     uuid = get_uuid_from_xml(xml_str)
     self.assertIsNone(uuid)
     injected_xml_str = inject_instanceid(xml_str, instance.uuid)
     # check that xml has the instanceid tag
     uuid = get_uuid_from_xml(injected_xml_str)
     self.assertEqual(uuid, instance.uuid)
Example #3
0
 def test_inject_instanceid(self):
     """
     Test that 1 and only 1 instance id exists or is injected
     """
     instance = Instance.objects.all().reverse()[0]
     xml_str = self.__load_fixture("..", "fixtures", "tutorial",
                                   "instances",
                                   "tutorial_2012-06-27_11-27-53.xml")
     # test that we dont have an instance id
     uuid = get_uuid_from_xml(xml_str)
     self.assertIsNone(uuid)
     injected_xml_str = inject_instanceid(xml_str, instance.uuid)
     # check that xml has the instanceid tag
     uuid = get_uuid_from_xml(injected_xml_str)
     self.assertEqual(uuid, instance.uuid)
Example #4
0
def edit_data(request, username, id_string, data_id):
    context = RequestContext(request)
    owner = User.objects.get(username=username)
    xform = get_object_or_404(XForm,
                              user__username=username,
                              id_string=id_string)
    instance = get_object_or_404(Instance, pk=data_id, xform=xform)
    if not has_edit_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden(_(u'Not shared.'))
    if not hasattr(settings, 'ENKETO_URL'):
        return HttpResponseRedirect(
            reverse('main.views.show',
                    kwargs={
                        'username': username,
                        'id_string': id_string
                    }))

    url = '%sdata/edit_url' % settings.ENKETO_URL
    # see commit 220f2dad0e for tmp file creation
    try:
        formhub_url = "http://%s/" % request.META['HTTP_HOST']
    except:
        formhub_url = "http://formhub.org/"
    injected_xml = inject_instanceid(instance.xml, instance.uuid)
    return_url = request.build_absolute_uri(
        reverse('odk_viewer.views.instance',
                kwargs={
                    'username': username,
                    'id_string': id_string
                }) + "#/" + str(instance.id))
    form_url = formhub_url + username
    if settings.TESTING_MODE:
        form_url = "https://testserver.com/bob"
    try:
        url = enketo_url(form_url,
                         xform.id_string,
                         instance_xml=injected_xml,
                         instance_id=instance.uuid,
                         return_url=return_url)
    except Exception, e:
        context.message = {
            'type': 'alert-error',
            'text': u"Enketo error, reason: %s" % e
        }
        messages.add_message(request,
                             messages.WARNING,
                             _("Enketo error: enketo replied %s") % e,
                             fail_silently=True)
Example #5
0
def edit_data(request, username, id_string, data_id):
    context = RequestContext(request)
    owner = User.objects.get(username=username)
    xform = get_object_or_404(
        XForm, user__username=username, id_string=id_string)
    instance = get_object_or_404(
        Instance, pk=data_id, xform=xform)
    if not has_edit_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden(_(u'Not shared.'))
    if not hasattr(settings, 'ENKETO_URL'):
        return HttpResponseRedirect(
            reverse(
                'main.views.show', kwargs={'username': username,
                                           'id_string': id_string}
            )
        )

    url = '%sdata/edit_url' % settings.ENKETO_URL
    # see commit 220f2dad0e for tmp file creation
    try:
        formhub_url = "http://%s/" % request.META['HTTP_HOST']
    except:
        formhub_url = "http://formhub.org/"
    injected_xml = inject_instanceid(instance.xml, instance.uuid)
    return_url = request.build_absolute_uri(
        reverse(
            'odk_viewer.views.instance',
            kwargs={
                'username': username,
                'id_string': id_string}
        ) + "#/" + str(instance.id))
    form_url = formhub_url + username
    
    if hasattr(settings, "TESTING_MODE") and settings.TESTING_MODE:
        form_url = "https://testserver.com/bob"

    try:
        url = enketo_url(
            form_url, xform.id_string, instance_xml=injected_xml,
            instance_id=instance.uuid, return_url=return_url
        )
    except Exception, e:
        context.message = {
            'type': 'alert-error',
            'text': u"Enketo error, reason: %s" % e}
        messages.add_message(
            request, messages.WARNING,
            _("Enketo error: enketo replied %s") % e, fail_silently=True)
Example #6
0
 def test_dont_inject_instanceid_if_exists(self):
     xls_file_path = os.path.join(os.path.dirname(__file__), "..", "fixtures", "tutorial", "tutorial.xls")
     self._publish_xls_file_and_set_xform(xls_file_path)
     xml_file_path = os.path.join(
         os.path.dirname(__file__),
         "..",
         "fixtures",
         "tutorial",
         "instances",
         "tutorial_2012-06-27_11-27-53_w_uuid.xml",
     )
     self._make_submission(xml_file_path)
     instance = Instance.objects.order_by("id").reverse()[0]
     injected_xml_str = inject_instanceid(instance.xml, instance.uuid)
     # check that the xml is unmodified
     self.assertEqual(instance.xml, injected_xml_str)
Example #7
0
 def test_inject_instanceid(self):
     """
     Test that 1 and only 1 instance id exists or is injected
     """
     instance = Instance.objects.all().reverse()[0]
     with open(
         os.path.join(
             os.path.dirname(__file__), "..", "fixtures", "tutorial",
             "instances", "tutorial_2012-06-27_11-27-53.xml"),
         "r") as xml_file:
         xml_str = xml_file.read()
     # test that we dont have an instance id
     uuid = get_uuid_from_xml(xml_str)
     self.assertIsNone(uuid)
     injected_xml_str = inject_instanceid(xml_str, instance.uuid)
     # check that xml has the instanceid tag
     uuid = get_uuid_from_xml(injected_xml_str)
     self.assertEqual(uuid, instance.uuid)
Example #8
0
def edit_data(request, username, id_string, data_id):
    owner = User.objects.get(username=username)
    xform = get_object_or_404(
        XForm, user__username=username, id_string=id_string)
    instance = get_object_or_404(
        Instance, pk=data_id, xform=xform)
    if not has_edit_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden(_(u'Not shared.'))
    if not hasattr(settings, 'ENKETO_URL'):
        return HttpResponseRedirect(
            reverse(
                'main.views.show', kwargs={'username': username,
                                           'id_string': id_string}
            )
        )

    url = '%sdata/edit_url' % settings.ENKETO_URL
    register_openers()
    response = None
    # see commit 220f2dad0e for tmp file creation
    try:
        formhub_url = "http://%s/" % request.META['HTTP_HOST']
    except:
        formhub_url = "http://formhub.org/"
    injected_xml = inject_instanceid(instance.xml, instance.uuid)
    values = {
        'format': 'json',
        'form_id': xform.id_string,
        'server_url': formhub_url + username,
        'instance': injected_xml,
        'instance_id': instance.uuid,
        'return_url': request.build_absolute_uri(
            reverse(
                'odk_viewer.views.instance',
                kwargs={
                    'username': username,
                    'id_string': id_string}
            ) + "#/" + str(instance.id))
    }
    data, headers = multipart_encode(values)
    headers['User-Agent'] = 'formhub'
    req = urllib2.Request(url, data, headers)
    try:
        response = urllib2.urlopen(req)
        response = json.loads(response.read())
        context = RequestContext(request)
        owner = User.objects.get(username=username)
        context.profile, created = \
            UserProfile.objects.get_or_create(user=owner)
        context.xform = xform
        context.content_user = owner
        context.form_view = True
        if 'edit_url' in response:
            audit = {
                "xform": xform.id_string,
                "data_id": data_id
            }
            audit_log(Actions.SUBMISSION_EDIT_REQUESTED, request.user, owner,
                _("Requested to edit data with id '%(data_id)s' on '%(id_string)s'.") %\
                {
                    'id_string': xform.id_string,
                    'data_id': data_id
                }, audit, request)
            context.enketo = response['edit_url']
            return HttpResponseRedirect(response['edit_url'])
        else:
            json_msg = response['reason']
            """
            return HttpResponse("<script>$('body')</script>")
            """
            context.message = {
                'type': 'alert-error',
                'text': "Enketo error, reason: " + (
                    response['reason'] and "Server not found.")
            }
            messages.add_message(request, messages.WARNING, json_msg)
            return render_to_response("profile.html", context_instance=context)

    except urllib2.URLError:
        # this will happen if we could not connect to enketo
        messages.add_message(
            request, messages.WARNING, _("Enketo error: Unable to open webform url."))
    except ValueError, e:
        messages.add_message(
            request, messages.WARNING, _("Enketo error: enketo replied %s") % e)
Example #9
0
def edit_data(request, username, id_string, data_id):
    owner = User.objects.get(username=username)
    xform = get_object_or_404(XForm,
                              user__username=username,
                              id_string=id_string)
    instance = get_object_or_404(Instance, pk=data_id, xform=xform)
    if not has_edit_permission(xform, owner, request, xform.shared):
        return HttpResponseForbidden(_(u'Not shared.'))
    if not hasattr(settings, 'ENKETO_URL'):
        return HttpResponseRedirect(
            reverse('main.views.show',
                    kwargs={
                        'username': username,
                        'id_string': id_string
                    }))

    url = '%sdata/edit_url' % settings.ENKETO_URL
    register_openers()
    response = None
    # see commit 220f2dad0e for tmp file creation
    try:
        formhub_url = "http://%s/" % request.META['HTTP_HOST']
    except:
        formhub_url = "http://formhub.org/"
    injected_xml = inject_instanceid(instance.xml, instance.uuid)
    values = {
        'format':
        'json',
        'form_id':
        xform.id_string,
        'server_url':
        formhub_url + username,
        'instance':
        injected_xml,
        'instance_id':
        instance.uuid,
        'return_url':
        request.build_absolute_uri(
            reverse('odk_viewer.views.instance',
                    kwargs={
                        'username': username,
                        'id_string': id_string
                    }) + "#/" + str(instance.id))
    }
    data, headers = multipart_encode(values)
    headers['User-Agent'] = 'formhub'
    req = urllib2.Request(url, data, headers)
    try:
        response = urllib2.urlopen(req)
        response = json.loads(response.read())
        context = RequestContext(request)
        owner = User.objects.get(username=username)
        context.profile, created = \
            UserProfile.objects.get_or_create(user=owner)
        context.xform = xform
        context.content_user = owner
        context.form_view = True
        if 'edit_url' in response:
            audit = {"xform": xform.id_string, "data_id": data_id}
            audit_log(
                Actions.SUBMISSION_EDIT_REQUESTED, request.user, owner,
                _("Requested to edit data with id "
                  "'%(data_id)s' on '%(id_string)s'.") % {
                      'id_string': xform.id_string,
                      'data_id': data_id
                  }, audit, request)
            context.enketo = response['edit_url']
            return HttpResponseRedirect(response['edit_url'])
        else:
            json_msg = response['reason']
            """
            return HttpResponse("<script>$('body')</script>")
            """
            context.message = {
                'type':
                'alert-error',
                'text':
                "Enketo error, reason: " +
                (response['reason'] and "Server not found.")
            }
            messages.add_message(request, messages.WARNING, json_msg)
            return render_to_response("profile.html", context_instance=context)

    except urllib2.URLError:
        # this will happen if we could not connect to enketo
        messages.add_message(request, messages.WARNING,
                             _("Enketo error: Unable to open webform url."))
    except ValueError, e:
        messages.add_message(request, messages.WARNING,
                             _("Enketo error: enketo replied %s") % e)