From 00b93d818d49991727eb0d5b6b93bda6382806b8 Mon Sep 17 00:00:00 2001 From: Chintalagiri Shashank Date: Fri, 15 Jan 2016 10:30:25 +0530 Subject: [PATCH] Integrated user auth seems functional. Data migration done manually via csv export/import. --- ...4860_add_user_auth_models_to_primary_db.py | 65 +++++++++++++++++++ tendril/auth/db/controller.py | 9 +++ tendril/auth/db/model.py | 17 ++--- tendril/frontend/parts/forms.py | 2 +- tendril/frontend/users/controller.py | 29 --------- 5 files changed, 84 insertions(+), 38 deletions(-) create mode 100644 alembic/versions/3badcc784860_add_user_auth_models_to_primary_db.py delete mode 100644 tendril/frontend/users/controller.py diff --git a/alembic/versions/3badcc784860_add_user_auth_models_to_primary_db.py b/alembic/versions/3badcc784860_add_user_auth_models_to_primary_db.py new file mode 100644 index 0000000..5c535f6 --- /dev/null +++ b/alembic/versions/3badcc784860_add_user_auth_models_to_primary_db.py @@ -0,0 +1,65 @@ +"""Add user auth models to primary db + +Revision ID: 3badcc784860 +Revises: 4e622fb65953 +Create Date: 2016-01-15 10:09:31.464087 + +""" + +# revision identifiers, used by Alembic. +revision = '3badcc784860' +down_revision = '4e622fb65953' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.create_table('Role', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=50), nullable=True), + sa.Column('description', sa.String(length=255), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + op.create_table('User', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('email', sa.String(length=255), nullable=False), + sa.Column('confirmed_at', sa.DateTime(), nullable=True), + sa.Column('is_active', sa.Boolean(), server_default='0', nullable=False), + sa.Column('full_name', sa.String(length=50), server_default='', nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email') + ) + op.create_table('UserAuth', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=True), + sa.Column('username', sa.String(length=50), nullable=False), + sa.Column('password', sa.String(length=255), server_default='', nullable=False), + sa.Column('reset_password_token', sa.String(length=100), server_default='', nullable=False), + sa.Column('active', sa.Boolean(), server_default='0', nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['User.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('username') + ) + op.create_table('UserRoles', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=True), + sa.Column('role_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['role_id'], ['Role.id'], ondelete='CASCADE'), + sa.ForeignKeyConstraint(['user_id'], ['User.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id') + ) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_table('UserRoles') + op.drop_table('UserAuth') + op.drop_table('User') + op.drop_table('Role') + ### end Alembic commands ### diff --git a/tendril/auth/db/controller.py b/tendril/auth/db/controller.py index 318b08c..f8dc2bd 100644 --- a/tendril/auth/db/controller.py +++ b/tendril/auth/db/controller.py @@ -21,3 +21,12 @@ """ Docstring for controller """ + +from tendril.utils.db import with_db + +from .model import User + + +@with_db +def get_users_list(session=None): + return session.query(User.full_name).all() diff --git a/tendril/auth/db/model.py b/tendril/auth/db/model.py index 326e726..f0e3b8e 100644 --- a/tendril/auth/db/model.py +++ b/tendril/auth/db/model.py @@ -42,6 +42,14 @@ logger = log.get_logger(__name__, log.DEFAULT) +# Define the UserRoles association model +class UserRoles(DeclBase, BaseMixin): + user_id = Column(Integer(), + ForeignKey('User.id', ondelete='CASCADE')) + role_id = Column(Integer(), + ForeignKey('Role.id', ondelete='CASCADE')) + + # Define the User data model. Make sure to add the flask_user.UserMixin !! class User(DeclBase, BaseMixin, UserMixin): @@ -56,7 +64,7 @@ class User(DeclBase, BaseMixin, UserMixin): # Relationships user_auth = relationship('UserAuth', uselist=False) - roles = relationship('Role', secondary='UserRoles', + roles = relationship('Role', secondary=UserRoles.__table__, backref=backref('users', lazy='dynamic')) @@ -81,10 +89,3 @@ class Role(DeclBase, BaseMixin): name = Column(String(50), unique=True) description = Column(String(255)) - -# Define the UserRoles association model -class UserRoles(DeclBase, BaseMixin): - user_id = Column(Integer(), - ForeignKey('User.id', ondelete='CASCADE')) - role_id = Column(Integer(), - ForeignKey('Role.id', ondelete='CASCADE')) diff --git a/tendril/frontend/parts/forms.py b/tendril/frontend/parts/forms.py index fa83f68..72a51cf 100644 --- a/tendril/frontend/parts/forms.py +++ b/tendril/frontend/parts/forms.py @@ -29,7 +29,7 @@ from wtforms.validators import ValidationError from flask_user import current_user -from tendril.frontend.users.controller import get_users_list +from tendril.auth.db.controller import get_users_list def user_auth_check(form, field): diff --git a/tendril/frontend/users/controller.py b/tendril/frontend/users/controller.py deleted file mode 100644 index ef524ad..0000000 --- a/tendril/frontend/users/controller.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python -# encoding: utf-8 - -# Copyright (C) 2015 Chintalagiri Shashank -# -# This file is part of tendril. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -""" -Docstring for controller -""" - -from .models import User - - -def get_users_list(): - return User.query.all()