# Path: _15thnight/models.py
# class User(Model):
#     """
#     User Model.
# 
#     Required parameters:
#         - email, password, phone_number
#     """
#     __tablename__ = "users"
#     id = Column(Integer, primary_key=True)
#     created_at = Column(DateTime, default=datetime.utcnow)
#     updated = Column(DateTime, default=datetime.utcnow)
#     name = Column(String(255), nullable=False)
#     organization = Column(String(255), nullable=False)
#     email = Column(String(255), nullable=False, unique=True)
#     password = Column(Text, nullable=False)
#     phone_number = Column(String(20), nullable=False)
#     role = Column(Enum('admin', 'advocate', 'provider'), default='advocate')
#     # Services only apply to providers
#     services = relationship(
#         "Service", secondary="user_services", backref="providers")
#     reset_token = Column(String(255))
#     reset_created_at = Column(DateTime)
# 
#     def __init__(self, name, organization, email, password, phone_number,
#                  services, role):
#         self.name = name
#         self.organization = organization
#         self.email = email.lower()
#         self.set_password(password)
#         self.phone_number = phone_number
#         self.role = role
#         self.services = services
# 
#     def check_password(self, password):
#         """Check a user's password (includes salt)."""
#         return check_password_hash(self.password, password)
# 
#     def generate_reset_token(self):
#         self.reset_token = str(uuid.uuid4())
#         self.reset_created_at = datetime.utcnow()
# 
#     def get_alerts(self):
#         return Alert.query.filter(Alert.user == self) \
#             .order_by(desc(Alert.created_at)).all()
# 
#     def get_id(self):
#         """Get the User id in unicode or ascii."""
#         try:
#             return unicode(self.id)
#         except NameError:
#             return str(self.id)
# 
#     def set_password(self, password):
#         """Using pbkdf2:sha512, hash 'password'."""
#         self.password = generate_password_hash(
#             password=password,
#             method='pbkdf2:sha512',
#             salt_length=128
#         )
# 
#     @property
#     def is_admin(self):
#         return self.role == 'admin'
# 
#     @property
#     def is_advocate(self):
#         return self.role == 'advocate'
# 
#     @property
#     def is_provider(self):
#         return self.role == 'provider'
# 
#     @property
#     def is_authenticated(self):
#         """Authenticaition check."""
#         return True
# 
#     @property
#     def is_active(self):
#         """Active check."""
#         return True
# 
#     @property
#     def is_anonymous(self):
#         """Anonimity check."""
#         return False
# 
#     @classmethod
#     def get_users(cls):
#         return cls.query.order_by(desc(cls.created_at)).all()
# 
#     @classmethod
#     def providers_with_services(cls, services):
#         """Return a list of users in the passed in services."""
#         return cls.query \
#             .filter(cls.role == 'provider') \
#             .join(cls.services) \
#             .filter(Service.id.in_(services)) \
#             .distinct() \
#             .all()
# 
#     @classmethod
#     def get_by_email(cls, email):
#         """Return user based on email."""
#         return cls.query.filter(cls.email == email).first()
# 
#     def to_json(self):
#         return dict(
#             id=self.id,
#             name=self.name,
#             organization=self.organization,
#             email=self.email,
#             role=self.role,
#             phone_number=self.phone_number,
#             created_at=self.created_at,
#             services=[dict(name=s.name, id=s.id) for s in self.services]
#         )
# 
#     def __repr__(self):
#         """Return <User: %(email)."""
#         return '<User %s>' % (self.email)


from alembic import op
from _15thnight.models import User
import sqlalchemy as sa
Revises: 3e7e65c44fc2
Create Date: 2016-08-22 22:27:02.630331

"""

# revision identifiers, used by Alembic.
revision = '7ee0d022e8d6'
down_revision = '3e7e65c44fc2'
branch_labels = None
depends_on = None



def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        'users', sa.Column('name', sa.String(length=255), nullable=True))
    op.add_column(
        'users',
        sa.Column('organization', sa.String(length=255), nullable=True))
    op.execute(
        User.__table__.update()
        .where(User.__table__.c.name == None)
        .values(**{'name': ''})
    )
    op.alter_column(
        'users', 'name', existing_type=sa.String(length=255),
        nullable=False)
    op.execute(
        User.__table__.update()

# Path: _15thnight/models.py
# class User(Model):
#     """
#     User Model.
# 
#     Required parameters:
#         - email, password, phone_number
#     """
#     __tablename__ = "users"
#     id = Column(Integer, primary_key=True)
#     created_at = Column(DateTime, default=datetime.utcnow)
#     updated = Column(DateTime, default=datetime.utcnow)
#     name = Column(String(255), nullable=False)
#     organization = Column(String(255), nullable=False)
#     email = Column(String(255), nullable=False, unique=True)
#     password = Column(Text, nullable=False)
#     phone_number = Column(String(20), nullable=False)
#     role = Column(Enum('admin', 'advocate', 'provider'), default='advocate')
#     # Services only apply to providers
#     services = relationship(
#         "Service", secondary="user_services", backref="providers")
#     reset_token = Column(String(255))
#     reset_created_at = Column(DateTime)
# 
#     def __init__(self, name, organization, email, password, phone_number,
#                  services, role):
#         self.name = name
#         self.organization = organization
#         self.email = email.lower()
#         self.set_password(password)
#         self.phone_number = phone_number
#         self.role = role
#         self.services = services
# 
#     def check_password(self, password):
#         """Check a user's password (includes salt)."""
#         return check_password_hash(self.password, password)
# 
#     def generate_reset_token(self):
#         self.reset_token = str(uuid.uuid4())
#         self.reset_created_at = datetime.utcnow()
# 
#     def get_alerts(self):
#         return Alert.query.filter(Alert.user == self) \
#             .order_by(desc(Alert.created_at)).all()
# 
#     def get_id(self):
#         """Get the User id in unicode or ascii."""
#         try:
#             return unicode(self.id)
#         except NameError:
#             return str(self.id)
# 
#     def set_password(self, password):
#         """Using pbkdf2:sha512, hash 'password'."""
#         self.password = generate_password_hash(
#             password=password,
#             method='pbkdf2:sha512',
#             salt_length=128
#         )
# 
#     @property
#     def is_admin(self):
#         return self.role == 'admin'
# 
#     @property
#     def is_advocate(self):
#         return self.role == 'advocate'
# 
#     @property
#     def is_provider(self):
#         return self.role == 'provider'
# 
#     @property
#     def is_authenticated(self):
#         """Authenticaition check."""
#         return True
# 
#     @property
#     def is_active(self):
#         """Active check."""
#         return True
# 
#     @property
#     def is_anonymous(self):
#         """Anonimity check."""
#         return False
# 
#     @classmethod
#     def get_users(cls):
#         return cls.query.order_by(desc(cls.created_at)).all()
# 
#     @classmethod
#     def providers_with_services(cls, services):
#         """Return a list of users in the passed in services."""
#         return cls.query \
#             .filter(cls.role == 'provider') \
#             .join(cls.services) \
#             .filter(Service.id.in_(services)) \
#             .distinct() \
#             .all()
# 
#     @classmethod
#     def get_by_email(cls, email):
#         """Return user based on email."""
#         return cls.query.filter(cls.email == email).first()
# 
#     def to_json(self):
#         return dict(
#             id=self.id,
#             name=self.name,
#             organization=self.organization,
#             email=self.email,
#             role=self.role,
#             phone_number=self.phone_number,
#             created_at=self.created_at,
#             services=[dict(name=s.name, id=s.id) for s in self.services]
#         )
# 
#     def __repr__(self):
#         """Return <User: %(email)."""
#         return '<User %s>' % (self.email)


# Path: _15thnight/migrations/versions/7ee0d022e8d6_user_name_organization.py
from alembic import op
from _15thnight.models import User
import sqlalchemy as sa

Revises: 3e7e65c44fc2
Create Date: 2016-08-22 22:27:02.630331

"""

# revision identifiers, used by Alembic.
revision = '7ee0d022e8d6'
down_revision = '3e7e65c44fc2'
branch_labels = None
depends_on = None



def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        'users', sa.Column('name', sa.String(length=255), nullable=True))
    op.add_column(
        'users',
        sa.Column('organization', sa.String(length=255), nullable=True))
    op.execute(
        User.__table__.update()
        .where(User.__table__.c.name == None)
        .values(**{'name': ''})
    )
    op.alter_column(
        'users', 'name', existing_type=sa.String(length=255),
        nullable=False)
    op.execute(
        User.__table__.update()

        .where(User.__table__.c.organization == None)