try:
    from hashlib import md5
except ImportError:
    from md5 import md5

from django.http import HttpResponseBadRequest

from secretballot.middleware import SecretBallotIpUseragentMiddleware


class SecretBallotUserIpUseragentMiddleware(SecretBallotIpUseragentMiddleware):

    def generate_token(self, request):
        if request.user.is_authenticated():
            return request.user.username
        else:
            try:
                s = u''.join((request.META['REMOTE_ADDR'], request.META['HTTP_USER_AGENT']))
                return md5(s).hexdigest()
            except KeyError:
                return None

try:
    from hashlib import md5
except ImportError:
    from md5 import md5

from django.http import HttpResponseBadRequest

from secretballot.middleware import SecretBallotIpUseragentMiddleware


class SecretBallotUserIpUseragentMiddleware(SecretBallotIpUseragentMiddleware):

    def generate_token(self, request):
        if request.user.is_authenticated():
            return request.user.username
        else:
            try:
                s = ''.join((request.META['REMOTE_ADDR'], request.META['HTTP_USER_AGENT']))
                return md5(s.encode('utf-8')).hexdigest()
            except KeyError:
                return None

Fix hashing for Python 3
Fix hashing for Python 3

  try:
      from hashlib import md5
  except ImportError:
      from md5 import md5
  
  from django.http import HttpResponseBadRequest
  
  from secretballot.middleware import SecretBallotIpUseragentMiddleware
  
  
  class SecretBallotUserIpUseragentMiddleware(SecretBallotIpUseragentMiddleware):
  
      def generate_token(self, request):
          if request.user.is_authenticated():
              return request.user.username
          else:
              try:
-                 s = u''.join((request.META['REMOTE_ADDR'], request.META['HTTP_USER_AGENT']))
+                 s = ''.join((request.META['REMOTE_ADDR'], request.META['HTTP_USER_AGENT']))
-                 return md5(s).hexdigest()
+                 return md5(s.encode('utf-8')).hexdigest()
              except KeyError:
                  return None
  
Fix hashing for Python 3
## Code Before:
try:
    from hashlib import md5
except ImportError:
    from md5 import md5

from django.http import HttpResponseBadRequest

from secretballot.middleware import SecretBallotIpUseragentMiddleware


class SecretBallotUserIpUseragentMiddleware(SecretBallotIpUseragentMiddleware):

    def generate_token(self, request):
        if request.user.is_authenticated():
            return request.user.username
        else:
            try:
                s = u''.join((request.META['REMOTE_ADDR'], request.META['HTTP_USER_AGENT']))
                return md5(s).hexdigest()
            except KeyError:
                return None

## Instruction:
Fix hashing for Python 3
## Code After:
try:
    from hashlib import md5
except ImportError:
    from md5 import md5

from django.http import HttpResponseBadRequest

from secretballot.middleware import SecretBallotIpUseragentMiddleware


class SecretBallotUserIpUseragentMiddleware(SecretBallotIpUseragentMiddleware):

    def generate_token(self, request):
        if request.user.is_authenticated():
            return request.user.username
        else:
            try:
                s = ''.join((request.META['REMOTE_ADDR'], request.META['HTTP_USER_AGENT']))
                return md5(s.encode('utf-8')).hexdigest()
            except KeyError:
                return None
