from __future__ import unicode_literals

from django.core.urlresolvers import NoReverseMatch, reverse


def local_site_reverse(viewname, request=None, local_site_name=None,
                       args=None, kwargs=None, *func_args, **func_kwargs):
    """Reverses a URL name, returning a working URL.

    This works much like Django's reverse(), but handles returning a
    localsite version of a URL when invoked with a request within a localsite.
    """
    if request or local_site_name:
        if request and not local_site_name:
            local_site_name = getattr(request, '_local_site_name', None)

        if local_site_name:
            if args:
                new_args = [local_site_name] + args
                new_kwargs = kwargs
            else:
                new_args = args
                new_kwargs = {
                    'local_site_name': local_site_name,
                }

                if kwargs:
                    new_kwargs.update(kwargs)

            try:
                return reverse(viewname, args=new_args, kwargs=new_kwargs,
                               *func_args, **func_kwargs)
            except NoReverseMatch:
                # We'll try it again without those arguments.
                pass

    return reverse(viewname, args=args, kwargs=kwargs,
                   *func_args, **func_kwargs)

from __future__ import unicode_literals

from django.core.urlresolvers import NoReverseMatch, reverse


def local_site_reverse(viewname, request=None, local_site_name=None,
                       local_site=None, args=None, kwargs=None,
                       *func_args, **func_kwargs):
    """Reverses a URL name, returning a working URL.

    This works much like Django's reverse(), but handles returning a
    localsite version of a URL when invoked with a request within a localsite.
    """
    assert not (local_site_name and local_site)

    if request or local_site_name or local_site:
        if local_site:
            local_site_name = local_site.name
        elif request and not local_site_name:
            local_site_name = getattr(request, '_local_site_name', None)

        if local_site_name:
            if args:
                new_args = [local_site_name] + args
                new_kwargs = kwargs
            else:
                new_args = args
                new_kwargs = {
                    'local_site_name': local_site_name,
                }

                if kwargs:
                    new_kwargs.update(kwargs)

            try:
                return reverse(viewname, args=new_args, kwargs=new_kwargs,
                               *func_args, **func_kwargs)
            except NoReverseMatch:
                # We'll try it again without those arguments.
                pass

    return reverse(viewname, args=args, kwargs=kwargs,
                   *func_args, **func_kwargs)

Allow local_site_reverse to take an actual LocalSite.
Allow local_site_reverse to take an actual LocalSite.

local_site_reverse was able to take a LocalSite's name, or a request
object, but if you actually had a LocalSite (or None), you'd have to
write your own conditional to extract the name and pass it.

Now, local_site_reverse can take a LocalSite. This saves a database
query, if one is already available, and simplifies calling code.

Reviewed at https://reviews.reviewboard.org/r/6302/

  from __future__ import unicode_literals
  
  from django.core.urlresolvers import NoReverseMatch, reverse
  
  
  def local_site_reverse(viewname, request=None, local_site_name=None,
+                        local_site=None, args=None, kwargs=None,
-                        args=None, kwargs=None, *func_args, **func_kwargs):
+                        *func_args, **func_kwargs):
      """Reverses a URL name, returning a working URL.
  
      This works much like Django's reverse(), but handles returning a
      localsite version of a URL when invoked with a request within a localsite.
      """
+     assert not (local_site_name and local_site)
+ 
-     if request or local_site_name:
+     if request or local_site_name or local_site:
+         if local_site:
+             local_site_name = local_site.name
-         if request and not local_site_name:
+         elif request and not local_site_name:
              local_site_name = getattr(request, '_local_site_name', None)
  
          if local_site_name:
              if args:
                  new_args = [local_site_name] + args
                  new_kwargs = kwargs
              else:
                  new_args = args
                  new_kwargs = {
                      'local_site_name': local_site_name,
                  }
  
                  if kwargs:
                      new_kwargs.update(kwargs)
  
              try:
                  return reverse(viewname, args=new_args, kwargs=new_kwargs,
                                 *func_args, **func_kwargs)
              except NoReverseMatch:
                  # We'll try it again without those arguments.
                  pass
  
      return reverse(viewname, args=args, kwargs=kwargs,
                     *func_args, **func_kwargs)
  
Allow local_site_reverse to take an actual LocalSite.
## Code Before:
from __future__ import unicode_literals

from django.core.urlresolvers import NoReverseMatch, reverse


def local_site_reverse(viewname, request=None, local_site_name=None,
                       args=None, kwargs=None, *func_args, **func_kwargs):
    """Reverses a URL name, returning a working URL.

    This works much like Django's reverse(), but handles returning a
    localsite version of a URL when invoked with a request within a localsite.
    """
    if request or local_site_name:
        if request and not local_site_name:
            local_site_name = getattr(request, '_local_site_name', None)

        if local_site_name:
            if args:
                new_args = [local_site_name] + args
                new_kwargs = kwargs
            else:
                new_args = args
                new_kwargs = {
                    'local_site_name': local_site_name,
                }

                if kwargs:
                    new_kwargs.update(kwargs)

            try:
                return reverse(viewname, args=new_args, kwargs=new_kwargs,
                               *func_args, **func_kwargs)
            except NoReverseMatch:
                # We'll try it again without those arguments.
                pass

    return reverse(viewname, args=args, kwargs=kwargs,
                   *func_args, **func_kwargs)

## Instruction:
Allow local_site_reverse to take an actual LocalSite.
## Code After:
from __future__ import unicode_literals

from django.core.urlresolvers import NoReverseMatch, reverse


def local_site_reverse(viewname, request=None, local_site_name=None,
                       local_site=None, args=None, kwargs=None,
                       *func_args, **func_kwargs):
    """Reverses a URL name, returning a working URL.

    This works much like Django's reverse(), but handles returning a
    localsite version of a URL when invoked with a request within a localsite.
    """
    assert not (local_site_name and local_site)

    if request or local_site_name or local_site:
        if local_site:
            local_site_name = local_site.name
        elif request and not local_site_name:
            local_site_name = getattr(request, '_local_site_name', None)

        if local_site_name:
            if args:
                new_args = [local_site_name] + args
                new_kwargs = kwargs
            else:
                new_args = args
                new_kwargs = {
                    'local_site_name': local_site_name,
                }

                if kwargs:
                    new_kwargs.update(kwargs)

            try:
                return reverse(viewname, args=new_args, kwargs=new_kwargs,
                               *func_args, **func_kwargs)
            except NoReverseMatch:
                # We'll try it again without those arguments.
                pass

    return reverse(viewname, args=args, kwargs=kwargs,
                   *func_args, **func_kwargs)
