import hashlib
import six
import random


def generate_activation_key(user):
    """
    The activation key for the ``User`` will be a
    SHA1 hash, generated from a combination of the ``User``'s
    pk and a random salt.
    """
    salt = hashlib.sha1(six.text_type(random.random())
                        .encode('ascii')).hexdigest()[:5]
    salt = salt.encode('ascii')
    user_pk = str(user.pk)
    if isinstance(user_pk, six.text_type):
        user_pk = user_pk.encode('utf-8')
    activation_key = hashlib.sha1(salt + user_pk).hexdigest()
    # UserActivationKey.objects.update_or_create(
    #     user=user,
    #     defaults={'activation_key': activation_key}
    # )
    return activation_key