def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
    """
    If versioning is being used then we pass any `reverse` calls through
    to the versioning scheme instance, so that the resulting URL can be modified if needed.
    """
    url = None

    # Substitute reverse function by scheme's one if versioning enabled
    scheme = getattr(request, 'versioning_scheme', None)
    if scheme is not None:
        def reverse_url(*a, **kw):
            try:
                return scheme.reverse(*a, **kw)
            except NoReverseMatch:
                # In case the versioning scheme reversal fails, fallback to the default implementation
                return _reverse(*a, **kw)
    else:
        reverse_url = _reverse

    try:
        # Resolving URL normally
        url = reverse_url(viewname, args, kwargs, request, format, **extra)
    except NoReverseMatch:
        if request and ':' not in viewname:
            # Retrieving current namespace through request
            try:
                current_namespace = request.resolver_match.namespace
            except AttributeError:
                try:
                    current_namespace = resolve(request.path).namespace
                except Http404:
                    current_namespace = None

            if current_namespace:
                try:
                    # Trying to resolve URL with current namespace
                    viewname_to_try = '{namespace}:{viewname}'.format(namespace=current_namespace, viewname=viewname)
                    url = reverse_url(viewname_to_try, args, kwargs, request, format, **extra)
                except NoReverseMatch:
                    # Trying to resolve URL with other namespaces
                    # (Could be wrong if views have the same name in different namespaces)
                    urlpatterns = urls.import_module(settings.ROOT_URLCONF).urlpatterns
                    namespaces = [urlpattern.namespace for urlpattern in urlpatterns
                                  if getattr(urlpattern, 'namespace', current_namespace) != current_namespace]

                    # Remove duplicates but preserve order of elements
                    from collections import OrderedDict
                    for namespace in OrderedDict.fromkeys(namespaces):
                        try:
                            viewname_to_try = '{namespace}:{viewname}'.format(namespace=namespace, viewname=viewname)
                            url = reverse_url(viewname_to_try, args, kwargs, request, format, **extra)
                            break
                        except NoReverseMatch:
                            continue
        # Raise exception if everything else fails
        if not url:
            raise
    return preserve_builtin_query_params(url, request)
Beispiel #2
0
from django.utils.simplejson import dumps
import time
from urls import urlpatterns
from django.db.models import Q
from django.conf.urls import url, import_module
from django.conf import settings
from django.http import HttpResponse


root_urls = import_module(settings.ROOT_URLCONF)
lookups = dict()


class AutoComplete(object):
    query_set = None
    search_fields = []
    url_search_parameters = None
    requires_auth = False

    __name__ = "__undefined__"

    def search_term(self, term):
        query = None
        queryset = self.query_set
        fields = self.search_fields
        for field in fields:
            kwargs = {field + "__contains": term}
            if query is None:
                query = Q(**kwargs)
            else:
                query = query | Q(**kwargs)
Beispiel #3
0
def reverse(viewname,
            args=None,
            kwargs=None,
            request=None,
            format=None,
            **extra):
    """
    If versioning is being used then we pass any `reverse` calls through
    to the versioning scheme instance, so that the resulting URL can be modified if needed.
    """
    url = None

    # Substitute reverse function by scheme's one if versioning enabled
    scheme = getattr(request, 'versioning_scheme', None)
    if scheme is not None:

        def reverse_url(*a, **kw):
            try:
                return scheme.reverse(*a, **kw)
            except NoReverseMatch:
                # In case the versioning scheme reversal fails, fallback to the default implementation
                return _reverse(*a, **kw)
    else:
        reverse_url = _reverse

    try:
        # Resolving URL normally
        url = reverse_url(viewname, args, kwargs, request, format, **extra)
    except NoReverseMatch:
        if request and ':' not in viewname:
            # Retrieving current namespace through request
            try:
                current_namespace = request.resolver_match.namespace
            except AttributeError:
                try:
                    current_namespace = resolve(request.path).namespace
                except Http404:
                    current_namespace = None

            if current_namespace:
                try:
                    # Trying to resolve URL with current namespace
                    viewname_to_try = '{namespace}:{viewname}'.format(
                        namespace=current_namespace, viewname=viewname)
                    url = reverse_url(viewname_to_try, args, kwargs, request,
                                      format, **extra)
                except NoReverseMatch:
                    # Trying to resolve URL with other namespaces
                    # (Could be wrong if views have the same name in different namespaces)
                    urlpatterns = urls.import_module(
                        settings.ROOT_URLCONF).urlpatterns
                    namespaces = [
                        urlpattern.namespace for urlpattern in urlpatterns
                        if getattr(urlpattern, 'namespace', current_namespace)
                        != current_namespace
                    ]

                    # Remove duplicates but preserve order of elements
                    from collections import OrderedDict
                    for namespace in OrderedDict.fromkeys(namespaces):
                        try:
                            viewname_to_try = '{namespace}:{viewname}'.format(
                                namespace=namespace, viewname=viewname)
                            url = reverse_url(viewname_to_try, args, kwargs,
                                              request, format, **extra)
                            break
                        except NoReverseMatch:
                            continue
        # Raise exception if everything else fails
        if not url:
            raise
    return preserve_builtin_query_params(url, request)