Esempio n. 1
0
def create_author_secret_key_and_send_auth_email(sender, instance: Author,
                                                 **kwargs):
    """
    Happens before an Author instance is saved - generates a secret key
    from uuid.uuid5's SHA1 hash with the namespace as a uuid1 generated
    from instance.pk and name as the username.

    TODO set up celery to send authentication email
         in the background so that the server can
         respond with something for the time being.
    """

    # Generate secret key.
    namespace = uuid.uuid1(instance.pk)
    secret_key = uuid.uuid5(namespace, instance.username)

    if not (instance.secret_key
            or connection.settings_dict['NAME'].startswith('test_')):
        # Since Author does not have a previous
        # secret key, it means that this is a new
        # user and hence has to be sent an email
        # with the confirmation link to be verified.
        # The second half of this is for testing
        # purposes - you don't want to email all
        # the fake authors that are created during testing.
        # This is also why the secret_key is generated
        # outside of this if clause because we NEED the
        # secret_key field to be populated regardless.

        subject, from_email, to = 'Welcome To The Medialist', settings.EMAIL_HOST_USER, instance.email
        html_content = render_to_string(
            'email/email-confirmation.html', {
                'activation_url':
                'http://' + 'localhost:8000' + reverse(
                    'author:verify', kwargs={'secret_key': str(secret_key)})
            })
        text_content = strip_tags(html_content)

        instance.email_user(subject,
                            text_content,
                            from_email,
                            html_message=html_content)

    instance.secret_key = secret_key