def _create_enketo_urls(request, xform): """ Generate enketo urls for a form :param request: :param xform: :return: enketo urls """ form_url = get_form_url(request, xform.user.username, settings.ENKETO_PROTOCOL, xform_pk=xform.pk, generate_consistent_urls=True) data = {} try: enketo_urls = get_enketo_urls(form_url, xform.id_string) offline_url = enketo_urls.get("offline_url") MetaData.enketo_url(xform, offline_url) data['offline_url'] = offline_url if 'preview_url' in enketo_urls: preview_url = enketo_urls.get("preview_url") MetaData.enketo_preview_url(xform, preview_url) data['preview_url'] = preview_url if 'single_url' in enketo_urls: single_url = enketo_urls.get("single_url") MetaData.enketo_single_submit_url(xform, single_url) data['single_url'] = single_url except ConnectionError as e: logging.exception("Connection Error: %s" % e) except EnketoError as e: logging.exception("Enketo Error: %s" % e.message) return data
def enketo(self, request, *args, **kwargs): self.object = self.get_object() data = {} if isinstance(self.object, XForm): raise ParseError(_(u"Data id not provided.")) elif (isinstance(self.object, Instance)): if request.user.has_perm("change_xform", self.object.xform): return_url = request.query_params.get('return_url') form_url = get_form_url(request, self.object.xform.user.username, xform_pk=self.object.xform.id) if not return_url: raise ParseError(_(u"return_url not provided.")) try: data = get_enketo_urls(form_url, self.object.xform.id_string, instance_id=self.object.uuid, instance_xml=self.object.xml, return_url=return_url) if "edit_url" in data: data["url"] = data.pop("edit_url") except EnketoError as e: raise ParseError(text(e)) else: raise PermissionDenied(_(u"You do not have edit permissions.")) self.etag_data = data return Response(data=data)
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 handle(self, *args, **options): request = HttpRequest() server_name = options.get('server_name') server_port = options.get('server_port') protocol = options.get('protocol') generate_consistent_urls = options.get('generate_consistent_urls') if not server_name or not server_port or not protocol: raise CommandError( 'please provide a server_name, a server_port and a protocol') if server_name not in ['ona.io', 'stage.ona.io', 'localhost']: raise CommandError('server name provided is not valid') if protocol not in ['http', 'https']: raise CommandError('protocol provided is not valid') # required for generation of enketo url request.META['HTTP_HOST'] = '%s:%s' % (server_name, server_port)\ if server_port != '80' else server_name # required for generation of enketo preview url request.META['SERVER_NAME'] = server_name request.META['SERVER_PORT'] = server_port resultset = MetaData.objects.filter( Q(data_type='enketo_url') | Q(data_type='enketo_preview_url')) for meta_data in resultset: username = meta_data.content_object.user.username id_string = meta_data.content_object.id_string data_type = meta_data.data_type data_value = meta_data.data_value xform = meta_data.content_object xform_pk = xform.pk with open('/tmp/enketo_url', 'a') as f: form_url = get_form_url( request, username=username, id_string=id_string, xform_pk=xform_pk, generate_consistent_urls=generate_consistent_urls) enketo_urls = get_enketo_urls(form_url, id_string) if data_type == 'enketo_url': _enketo_url = (enketo_urls.get('offline_url') or enketo_urls.get('url')) MetaData.enketo_url(xform, _enketo_url) elif data_type == 'enketo_preview_url': _enketo_preview_url = (enketo_urls.get('preview_url')) MetaData.enketo_preview_url(xform, _enketo_preview_url) f.write('%s : %s \n' % (id_string, data_value)) self.stdout.write('%s: %s' % (data_type, meta_data.data_value)) self.stdout.write("enketo urls update complete!!")
def test_enketo_remote_server(self): if not self._running_enketo(): raise SkipTest with HTTMock(enketo_mock): server_url = "https://testserver.com/bob" form_id = "test_%s" % re.sub(re.compile("\."), "_", str( time())) # noqa url = get_enketo_urls(server_url, form_id) self.assertIsInstance(url['url'], basestring) self.assertIsNone(URLValidator()(url['url']))
def test_enketo_url_with_http_protocol_on_formlist(self): if not self._running_enketo(): raise SkipTest with HTTMock(enketo_mock_http): server_url = "http://testserver.com/bob" form_id = "test_%s" % re.sub(re.compile("\."), "_", str( time())) # noqa url = get_enketo_urls(server_url, form_id) self.assertIn("http:", url['url']) self.assertIsInstance(url['url'], basestring) self.assertIsNone(URLValidator()(url['url']))
def enter_data(request, username, id_string): """ Redirects to Enketo webform view. """ owner = get_object_or_404(User, username__iexact=username) xform = get_form({ 'user__username__iexact': username, 'id_string__iexact': id_string }) if not has_edit_permission(xform, owner, request, xform.shared): return HttpResponseForbidden(_(u'Not shared.')) form_url = get_form_url(request, username, settings.ENKETO_PROTOCOL) try: enketo_urls = get_enketo_urls(form_url, xform.id_string) url = enketo_urls.get('url') if not url: return HttpResponseRedirect( reverse('form-show', kwargs={ 'username': username, 'id_string': id_string })) return HttpResponseRedirect(url) except EnketoError as e: data = {} owner = User.objects.get(username__iexact=username) data['profile'], __ = UserProfile.objects.get_or_create(user=owner) data['xform'] = xform data['content_user'] = owner data['form_view'] = True data['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) return render(request, "profile.html", data) return HttpResponseRedirect( reverse('form-show', kwargs={ 'username': username, 'id_string': id_string }))
def enketo(self, request, **kwargs): """Expose enketo urls.""" survey_type = self.kwargs.get('survey_type') or \ request.GET.get('survey_type') self.object = self.get_object() form_url = get_form_url(request, self.object.user.username, protocol=settings.ENKETO_PROTOCOL, xform_pk=self.object.pk, generate_consistent_urls=True) data = {'message': _(u"Enketo not properly configured.")} http_status = status.HTTP_400_BAD_REQUEST try: # pass default arguments to enketo_url to prepopulate form fields request_vars = request.GET defaults = generate_enketo_form_defaults(self.object, **request_vars) enketo_urls = get_enketo_urls(form_url, self.object.id_string, **defaults) offline_url = enketo_urls.get('offline_url') preview_url = enketo_urls.get('preview_url') single_submit_url = enketo_urls.get('single_url') except EnketoError as e: data = {'message': _(u"Enketo error: %s" % e)} else: if survey_type == 'single': http_status = status.HTTP_200_OK data = {"single_submit_url": single_submit_url} else: http_status = status.HTTP_200_OK data = { "enketo_url": offline_url, "enketo_preview_url": preview_url, "single_submit_url": single_submit_url } return Response(data, http_status)
def handle(self, *args, **options): request = HttpRequest() server_name = options.get('server_name') server_port = options.get('server_port') protocol = options.get('protocol') username = options.get('username') id_string = options.get('id_string') generate_consistent_urls = options.get('generate_consistent_urls') if not server_name or not server_port or not protocol: raise CommandError( 'please provide a server_name, a server_port and a protocol') if protocol not in ['http', 'https']: raise CommandError('protocol provided is not valid') # required for generation of enketo url request.META['HTTP_HOST'] = '%s:%s' % (server_name, server_port)\ if server_port != '80' else server_name # required for generation of enketo preview url request.META['SERVER_NAME'] = server_name request.META['SERVER_PORT'] = server_port if username and id_string: try: xform = XForm.objects.get(user__username=username, id_string=id_string) form_url = get_form_url( request, username, protocol=protocol, xform_pk=xform.pk, generate_consistent_urls=generate_consistent_urls) id_string = xform.id_string enketo_urls = get_enketo_urls(form_url, id_string) _url = (enketo_urls.get('offline_url') or enketo_urls.get('url')) _preview_url = enketo_urls.get('preview_url') self.stdout.write('enketo url: %s | preview url: %s' % (_url, _preview_url)) self.stdout.write("enketo urls generation completed!!") except XForm.DoesNotExist: self.stdout.write( "No xform matching the provided username and id_string") elif username and id_string is None: xforms = XForm.objects.filter(user__username=username) num_of_xforms = xforms.count() if xforms: for xform in queryset_iterator(xforms): form_url = get_form_url( request, username, protocol=protocol, xform_pk=xform.pk, generate_consistent_urls=generate_consistent_urls) id_string = xform.id_string enketo_urls = get_enketo_urls(form_url, id_string) _url = (enketo_urls.get('offline_url') or enketo_urls.get('url')) _preview_url = enketo_urls.get('preview_url') num_of_xforms -= 1 self.stdout.write( 'enketo url: %s | preview url: %s | remaining: %s' % (_url, _preview_url, num_of_xforms)) self.stdout.write("enketo urls generation completed!!") else: self.stdout.write("Username doesn't own any form") else: xforms = XForm.objects.all() num_of_xforms = xforms.count() for xform in queryset_iterator(xforms): username = xform.user.username id_string = xform.id_string form_url = get_form_url( request, username, protocol=protocol, xform_pk=xform.pk, generate_consistent_urls=generate_consistent_urls) enketo_urls = get_enketo_urls(form_url, id_string) _url = (enketo_urls.get('offline_url') or enketo_urls.get('url')) _preview_url = enketo_urls.get('preview_url') num_of_xforms -= 1 self.stdout.write( 'enketo url: %s | preview url: %s | remaining: %s' % (_url, _preview_url, num_of_xforms)) self.stdout.write("enketo urls generation completed!!")