"""add passwords for users

Revision ID: 31d29fbffe44
Revises: 48c578b852fa
Create Date: 2016-01-20 23:33:36.893832

"""

# revision identifiers, used by Alembic.
revision = '31d29fbffe44'
down_revision = '48c578b852fa'

import random
from flask_security.utils import encrypt_password

from alembic import op
import sqlalchemy as sa

ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

def rand_alphabet():
    return encrypt_password(''.join(random.choice(ALPHABET) for i in range(16)))

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column(u'roles', sa.Column('description', sa.String(length=255), nullable=True))
    op.add_column(u'users', sa.Column('confirmed_at', sa.DateTime(), nullable=True))
    op.add_column(u'users', sa.Column('current_login_at', sa.DateTime(), nullable=True))
    op.add_column(u'users', sa.Column('current_login_ip', sa.String(length=255), nullable=True))
    op.add_column(u'users', sa.Column('last_login_at', sa.DateTime(), nullable=True))
    op.add_column(u'users', sa.Column('last_login_ip', sa.String(length=255), nullable=True))
    op.add_column(u'users', sa.Column('login_count', sa.Integer(), nullable=True))
    op.add_column(u'users', sa.Column(
        'password', sa.String(length=255), nullable=False,
        default=rand_alphabet(), server_default=rand_alphabet()
    ))
    ### end Alembic commands ###

    op.execute(sa.sql.text('''
        UPDATE users SET confirmed_at = now()
    '''))

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column(u'users', 'password')
    op.drop_column(u'users', 'login_count')
    op.drop_column(u'users', 'last_login_ip')
    op.drop_column(u'users', 'last_login_at')
    op.drop_column(u'users', 'current_login_ip')
    op.drop_column(u'users', 'current_login_at')
    op.drop_column(u'users', 'confirmed_at')
    op.drop_column(u'roles', 'description')
    ### end Alembic commands ###
