Exemple #1
0
    def enketo(self, request, **kwargs):
        self.object = self.get_object()
        form_url = get_form_url(request, self.object.user.username,
                                settings.ENKETO_PROTOCOL)

        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)
            url = enketo_url(form_url, self.object.id_string, **defaults)
            preview_url = get_enketo_preview_url(request,
                                                 self.object.user.username,
                                                 self.object.id_string)
        except EnketoError as e:
            data = {'message': _(u"Enketo error: %s" % e)}
        else:
            if url and preview_url:
                http_status = status.HTTP_200_OK
                data = {"enketo_url": url, "enketo_preview_url": preview_url}

        return Response(data, http_status)
    def get_enketo_preview_url(self, obj):
        if obj:
            _enketo_preview_url = cache.get(
                '{}{}'.format(ENKETO_PREVIEW_URL_CACHE, obj.pk))
            if _enketo_preview_url:
                return _enketo_preview_url

            try:
                metadata = MetaData.objects.get(
                    xform=obj, data_type="enketo_preview_url")
            except MetaData.DoesNotExist:
                request = self.context.get('request')
                preview_url = ""

                try:
                    preview_url = get_enketo_preview_url(request,
                                                         obj.user.username,
                                                         obj.id_string)
                    MetaData.enketo_preview_url(obj, preview_url)
                except EnketoError:
                    pass

                cache.set('{}{}'.format(ENKETO_PREVIEW_URL_CACHE, obj.pk),
                          preview_url)
                return preview_url

            _enketo_preview_url = metadata.data_value
            cache.set(
                '{}{}'.format(ENKETO_URL_CACHE, obj.pk), _enketo_preview_url)
            return _enketo_preview_url

        return None
Exemple #3
0
    def get_enketo_preview_url(self, obj):
        if obj:
            _enketo_preview_url = cache.get('{}{}'.format(
                ENKETO_PREVIEW_URL_CACHE, obj.pk))
            if _enketo_preview_url:
                return _enketo_preview_url

            try:
                metadata = MetaData.objects.get(xform=obj,
                                                data_type="enketo_preview_url")
            except MetaData.DoesNotExist:
                request = self.context.get('request')
                preview_url = ""

                try:
                    preview_url = get_enketo_preview_url(
                        request, obj.user.username, obj.id_string)
                    MetaData.enketo_preview_url(obj, preview_url)
                except EnketoError:
                    pass

                cache.set('{}{}'.format(ENKETO_PREVIEW_URL_CACHE, obj.pk),
                          preview_url)
                return preview_url

            _enketo_preview_url = metadata.data_value
            cache.set('{}{}'.format(ENKETO_URL_CACHE, obj.pk),
                      _enketo_preview_url)
            return _enketo_preview_url

        return None
Exemple #4
0
    def handle(self, *args, **kwargs):
        request = HttpRequest()
        server_name = kwargs.get('server_name')
        server_port = kwargs.get('server_port')
        protocol = kwargs.get('protocol')

        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
            with open('/tmp/enketo_url', 'a') as f:

                if data_type == 'enketo_url':
                    form_url = get_form_url(request,
                                            username,
                                            protocol=protocol)
                    _enketo_url = enketo_url(form_url, id_string)
                    MetaData.enketo_url(xform, _enketo_url)
                elif data_type == 'enketo_preview_url':
                    _enketo_preview_url = get_enketo_preview_url(
                        request, username, id_string)
                    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!!")
Exemple #5
0
    def get_enketo_preview_url(self, obj):
        if obj:
            _enketo_preview_url = cache.get('{}{}'.format(
                ENKETO_PREVIEW_URL_CACHE, obj.pk))
            if _enketo_preview_url:
                return _enketo_preview_url

            url = self._get_metadata(obj, 'enketo_preview_url')
            if url is None:
                try:
                    url = get_enketo_preview_url(self.context.get('request'),
                                                 obj.user.username,
                                                 obj.id_string)
                except:
                    return url
                else:
                    MetaData.enketo_preview_url(obj, url)

            return _set_cache(ENKETO_PREVIEW_URL_CACHE, url, obj)

        return None
    def enketo(self, request, **kwargs):
        self.object = self.get_object()
        form_url = _get_form_url(request, self.object.user.username)

        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)
            url = enketo_url(form_url, self.object.id_string, **defaults)
            preview_url = get_enketo_preview_url(request,
                                                 request.user.username,
                                                 self.object.id_string)
        except EnketoError:
            pass
        else:
            if url and preview_url:
                http_status = status.HTTP_200_OK
                data = {"enketo_url": url, "enketo_preview_url": preview_url}

        return Response(data, http_status)
    def handle(self, *args, **kwargs):
        request = HttpRequest()
        server_name = kwargs.get('server_name')
        server_port = kwargs.get('server_port')
        protocol = kwargs.get('protocol')
        username = kwargs.get('username')
        id_string = kwargs.get('id_string')

        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 ['api.ona.io', 'stage-api.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

        if username and id_string:
            form_url = get_form_url(request, username, protocol=protocol)
            try:
                xform = XForm.objects.get(user__username=username,
                                          id_string=id_string)
                id_string = xform.id_string
                _url = enketo_url(form_url, id_string)
                _preview_url = get_enketo_preview_url(request, username,
                                                      id_string)
                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:
            form_url = get_form_url(request, username, protocol=protocol)
            xforms = XForm.objects.filter(user__username=username)
            num_of_xforms = xforms.count()
            if xforms:
                for xform in queryset_iterator(xforms):
                    id_string = xform.id_string
                    _url = enketo_url(form_url, id_string)
                    _preview_url = get_enketo_preview_url(
                        request, username, id_string)
                    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)
                _url = enketo_url(form_url, id_string)
                _preview_url = get_enketo_preview_url(request, username,
                                                      id_string)
                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!!")