def edit_data(request, username, id_string, data_id): """ Redirects to Enketo webform to edit a submission with the data_id. """ context = RequestContext(request) owner = User.objects.get(username__iexact=username) xform_kwargs = { 'id_string__iexact': id_string, 'user__username__iexact': username } xform = get_form(xform_kwargs) 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('form-show', kwargs={ 'username': username, 'id_string': id_string })) url = '%sdata/edit_url' % settings.ENKETO_URL # see commit 220f2dad0e for tmp file creation injected_xml = inject_instanceid(instance.xml, instance.uuid) return_url = request.build_absolute_uri( reverse('submission-instance', kwargs={ 'username': username, 'id_string': id_string }) + "#/" + text(instance.id)) form_url = get_form_url(request, username, settings.ENKETO_PROTOCOL) try: url = get_enketo_urls(form_url, xform.id_string, instance_xml=injected_xml, instance_id=instance.uuid, return_url=return_url) except EnketoError as 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) else: if url: url = url['edit_url'] context.enketo = url return HttpResponseRedirect(url) return HttpResponseRedirect( reverse('form-show', kwargs={ 'username': username, 'id_string': id_string }))
def edit_data(request, username, id_string, data_id): """ Redirects to Enketo webform to edit a submission with the data_id. """ context = RequestContext(request) owner = User.objects.get(username__iexact=username) xform_kwargs = { 'id_string__iexact': id_string, 'user__username__iexact': username } xform = get_form(xform_kwargs) 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( 'form-show', kwargs={'username': username, 'id_string': id_string})) url = '%sdata/edit_url' % settings.ENKETO_URL # see commit 220f2dad0e for tmp file creation injected_xml = inject_instanceid(instance.xml, instance.uuid) return_url = request.build_absolute_uri( reverse( 'submission-instance', kwargs={'username': username, 'id_string': id_string}) + "#/" + text(instance.id)) form_url = get_form_url(request, username, settings.ENKETO_PROTOCOL) try: url = enketo_url( form_url, xform.id_string, instance_xml=injected_xml, instance_id=instance.uuid, return_url=return_url) except EnketoError as 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) else: if url: context.enketo = url return HttpResponseRedirect(url) return HttpResponseRedirect( reverse( 'form-show', kwargs={'username': username, 'id_string': id_string}))
def edit_data(request, username, id_string, data_id): context = RequestContext(request) owner = User.objects.get(username__iexact=username) xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=id_string) instance = get_object_or_404(Instance, pk=data_id, xform=xform) #if not has_edit_permission(xform, owner, request, xform.shared): if not check_form_permissions(xform, str(request.user), 'can_edit'): return HttpResponseForbidden(_(u'Not shared.')) if not hasattr(settings, 'ENKETO_URL'): return HttpResponseRedirect( reverse('onadata.apps.main.views.show', kwargs={ 'username': username, 'id_string': id_string })) url = '%sdata/edit_url' % settings.ENKETO_URL # see commit 220f2dad0e for tmp file creation injected_xml = inject_instanceid(instance.xml, instance.uuid) return_url = request.build_absolute_uri( reverse('onadata.apps.viewer.views.instance', kwargs={ 'username': username, 'id_string': id_string }) + "#/" + str(instance.id)) form_url = _get_form_url(request, username, settings.ENKETO_PROTOCOL) try: url = enketo_url(form_url, xform.id_string, instance_xml=injected_xml, instance_id=instance.uuid, return_url=return_url) except Exception as 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) else: if url: context.enketo = url return HttpResponseRedirect(url) return HttpResponseRedirect( reverse('onadata.apps.main.views.show', kwargs={ 'username': username, 'id_string': id_string }))
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)
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)
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)
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( 'onadata.apps.main.views.show', kwargs={'username': username, 'id_string': id_string})) url = '%sdata/edit_url' % settings.ENKETO_URL # see commit 220f2dad0e for tmp file creation injected_xml = inject_instanceid(instance.xml, instance.uuid) return_url = request.build_absolute_uri( reverse( 'onadata.apps.viewer.views.instance', kwargs={ 'username': username, 'id_string': id_string} ) + "#/" + str(instance.id)) form_url = _get_form_url(request, username) 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)
def edit_data(request, username, id_string, data_id): context = RequestContext(request) owner = User.objects.get(username__iexact=username) xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=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("onadata.apps.main.views.show", kwargs={"username": username, "id_string": id_string}) ) url = "%sdata/edit_url" % settings.ENKETO_URL # see commit 220f2dad0e for tmp file creation injected_xml = inject_instanceid(instance.xml, instance.uuid) return_url = request.build_absolute_uri( reverse("onadata.apps.viewer.views.instance", kwargs={"username": username, "id_string": id_string}) + "#/" + str(instance.id) ) form_url = _get_form_url(request, username, settings.ENKETO_PROTOCOL) try: url = enketo_url( form_url, xform.id_string, instance_xml=injected_xml, instance_id=instance.uuid, return_url=return_url ) except Exception as 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) else: if url: context.enketo = url return HttpResponseRedirect(url) return HttpResponseRedirect( reverse("onadata.apps.main.views.show", kwargs={"username": username, "id_string": id_string}) )