def has_permission(request):
    user = request.user
    if user:
        if user.is_superuser:
            return True

        if user.is_staff and user.groups.filter(name__iexact='djedi').exists():
            return True

    return False


def get_username(request):
    user = request.user
    if hasattr(user, 'get_username'):
        return user.get_username()
    else:
        return user.username

import logging

_log = logging.getLogger(__name__)


def has_permission(request):
    user = getattr(request, 'user', None)
    if user:
        if user.is_superuser:
            return True

        if user.is_staff and user.groups.filter(name__iexact='djedi').exists():
            return True
    else:
        _log.warning("Request does not have `user` attribute. Make sure that "
                     "Djedi middleware is used after AuthenticationMiddleware")

    return False


def get_username(request):
    user = request.user
    if hasattr(user, 'get_username'):
        return user.get_username()
    else:
        return user.username

Handle wrong order of middleware.
Handle wrong order of middleware.

+ import logging
+ 
+ _log = logging.getLogger(__name__)
+ 
+ 
  def has_permission(request):
-     user = request.user
+     user = getattr(request, 'user', None)
      if user:
          if user.is_superuser:
              return True
  
          if user.is_staff and user.groups.filter(name__iexact='djedi').exists():
              return True
+     else:
+         _log.warning("Request does not have `user` attribute. Make sure that "
+                      "Djedi middleware is used after AuthenticationMiddleware")
  
      return False
  
  
  def get_username(request):
      user = request.user
      if hasattr(user, 'get_username'):
          return user.get_username()
      else:
          return user.username
  
Handle wrong order of middleware.
## Code Before:
def has_permission(request):
    user = request.user
    if user:
        if user.is_superuser:
            return True

        if user.is_staff and user.groups.filter(name__iexact='djedi').exists():
            return True

    return False


def get_username(request):
    user = request.user
    if hasattr(user, 'get_username'):
        return user.get_username()
    else:
        return user.username

## Instruction:
Handle wrong order of middleware.
## Code After:
import logging

_log = logging.getLogger(__name__)


def has_permission(request):
    user = getattr(request, 'user', None)
    if user:
        if user.is_superuser:
            return True

        if user.is_staff and user.groups.filter(name__iexact='djedi').exists():
            return True
    else:
        _log.warning("Request does not have `user` attribute. Make sure that "
                     "Djedi middleware is used after AuthenticationMiddleware")

    return False


def get_username(request):
    user = request.user
    if hasattr(user, 'get_username'):
        return user.get_username()
    else:
        return user.username
