from django import template

from .. import perms
from ..settings import get_user_attr


register = template.Library()


@register.filter
def is_masquerading(user):
    info = getattr(user, get_user_attr())
    return info['is_masquerading']


@register.filter
def can_masquerade(user):
    return perms.can_masquerade(user)


@register.filter
def can_masquerade_as(user, masquerade_user):
    return perms.can_masquerade_as(user, masquerade_user)

from django import template

from .. import perms
from ..settings import get_user_attr, is_enabled


register = template.Library()


@register.filter
def is_masquerading(user):
    if not is_enabled():
        return False
    info = getattr(user, get_user_attr(), None)
    return info['is_masquerading']


@register.filter
def can_masquerade(user):
    return perms.can_masquerade(user)


@register.filter
def can_masquerade_as(user, masquerade_user):
    return perms.can_masquerade_as(user, masquerade_user)

Make is_masquerading template tag more robust
Make is_masquerading template tag more robust

When masquerading is not enabled, immediately return False to avoid
checking for a request attribute that won't be present.

  from django import template
  
  from .. import perms
- from ..settings import get_user_attr
+ from ..settings import get_user_attr, is_enabled
  
  
  register = template.Library()
  
  
  @register.filter
  def is_masquerading(user):
+     if not is_enabled():
+         return False
-     info = getattr(user, get_user_attr())
+     info = getattr(user, get_user_attr(), None)
      return info['is_masquerading']
  
  
  @register.filter
  def can_masquerade(user):
      return perms.can_masquerade(user)
  
  
  @register.filter
  def can_masquerade_as(user, masquerade_user):
      return perms.can_masquerade_as(user, masquerade_user)
  
Make is_masquerading template tag more robust
## Code Before:
from django import template

from .. import perms
from ..settings import get_user_attr


register = template.Library()


@register.filter
def is_masquerading(user):
    info = getattr(user, get_user_attr())
    return info['is_masquerading']


@register.filter
def can_masquerade(user):
    return perms.can_masquerade(user)


@register.filter
def can_masquerade_as(user, masquerade_user):
    return perms.can_masquerade_as(user, masquerade_user)

## Instruction:
Make is_masquerading template tag more robust
## Code After:
from django import template

from .. import perms
from ..settings import get_user_attr, is_enabled


register = template.Library()


@register.filter
def is_masquerading(user):
    if not is_enabled():
        return False
    info = getattr(user, get_user_attr(), None)
    return info['is_masquerading']


@register.filter
def can_masquerade(user):
    return perms.can_masquerade(user)


@register.filter
def can_masquerade_as(user, masquerade_user):
    return perms.can_masquerade_as(user, masquerade_user)
