예제 #1
0
    async def _full_guild_add(session, guild):
        # add guild to database
        try:
            new_guild = models.Guild(id=guild.id, name=guild.name)
            session.add(new_guild)

            # we need to add all channels
            for channel in guild.channels:
                # add channel to database
                new_channel = models.Channel(id=channel.id,
                                             guild_id=guild.id,
                                             name=channel.name,
                                             channeltype=channel.type.name)
                session.add(new_channel)

            # we need to add all roles
            for role in guild.roles:
                # add role to database
                new_role = models.Role(
                    id=role.id,
                    guild_id=guild.id,
                    name=role.name,
                )
                session.add(new_role)

            session.commit()

        except Exception as e:
            session.rollback()
            logging.error(e)
        finally:
            session.close()
예제 #2
0
def create_role(db: Session, role: role_schema.RoleCreate):
    check_role_inputs(role)
    db_role = models.Role()
    [setattr(db_role, i[0], i[1]) for i in role]
    db.add(db_role)
    db.commit()
    db.refresh(db_role)
    return db_role
예제 #3
0
def create_role(db: Session, role: schemas.CreateRole):
    """
    创建模型--》创建 schemas 架构--》crud 构建逻辑
    """
    db_role = models.Role(
        name=role.name,
        rolecategory_id=role.rolecategory_id
    )
    db.add(db_role)
    db.commit()
    db.refresh(db_role)
    return db_role
예제 #4
0
    async def _full_guild_sync(session, guild):
        # sync guild to database
        try:
            if session.query(
                    exists().where(models.Guild.id == guild.id)).scalar():
                session.query(models.Guild) \
                       .filter(models.Guild.id == guild.id,
                               models.Guild.name != guild.name) \
                       .update({models.Guild.name: guild.name})
            else:
                new_guild = models.Guild(id=guild.id, name=guild.name)
                session.add(new_guild)

            # we need to sync all channels
            for channel in guild.channels:
                # sync channel to database
                if session.query(exists().where(
                        models.Channel.id == channel.id)).scalar():
                    session.query(models.Channel).filter(
                        models.Channel.id == channel.id,
                        models.Channel.name != channel.name).update(
                            {models.Channel.name: channel.name})
                else:
                    new_channel = models.Channel(id=channel.id,
                                                 guild_id=guild.id,
                                                 name=channel.name,
                                                 channeltype=channel.type.name)
                    session.add(new_channel)

            # we need to sync all roles
            for role in guild.roles:
                # sync role to database
                if session.query(
                        exists().where(models.Role.id == role.id)).scalar():
                    session.query(models.Role) \
                        .filter(models.Role.id == role.id,
                                models.Role.name != role.name) \
                        .update({models.Role.name: role.name})
                else:
                    new_role = models.Role(id=role.id,
                                           guild_id=guild.id,
                                           name=role.name)
                    session.add(new_role)

            session.commit()

        except Exception as e:
            session.rollback()
            logging.error(e)
        finally:
            session.close()
예제 #5
0
    async def on_guild_role_create(self, role):
        try:
            # init new session
            session = self.db.Session()

            new_role = models.Role(id=role.id,
                                   guild_id=role.guild.id,
                                   name=role.name)
            session.add(new_role)

        except Exception as e:
            logging.error(e)
        finally:
            session.close()
예제 #6
0
import models
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--action",
                    help="action to execute",
                    required=True,
                    choices=['create-database'])
args = parser.parse_known_args()[0]

if args.action == 'create-database':
    models.db.create_tables(models.DemoModel.__subclasses__())
    models.Role(name='User', code='user').save()
    models.Role(name='User Manager', code='user-manager').save()
    models.Role(name='Admin', code='admin').save()
예제 #7
0
from app import db
import models

db.create_all()

r = {'role_id': 1, 'role': 'admin'}
db.session.add(models.Role(**r))
r = {'role_id': 2, 'role': 'end_user'}
db.session.add(models.Role(**r))
db.session.commit()

t = {'user_name': 'Lekshmi', 'password': '******', 'role_id': 1}
db.session.add(models.User(**t))
t = {'user_name': 'Niveditha', 'password': '******', 'role_id': 1}
db.session.add(models.User(**t))
db.session.commit()
예제 #8
0
def seed():
    """Seed the database with sample data."""
    import models
    # the rooms

    room_features = ['Projector', 'TV', 'Webcam', 'Phone Line']

    rooms = {
        '1560': ['Projector'],
        '1561': ['TV', 'Webcam'],
        '1562': ['Projector'],
        '1563': ['TV'],
        '1564': ['Projector'],
        '1565': ['TV', 'Webcam'],
        '1665': ['TV', 'Webcam'],
        '1663': ['TV'],
        '1662': ['Projector'],
        '1661': ['Projector'],
        '1660': ['Projector']
    }

    feature_dict = {}
    for feature in room_features:
        f = models.RoomFeature(name=feature)
        get_db().add(f)
        feature_dict[feature] = f

    rooms_dict = {}
    for room_number in rooms:
        r = models.Room(number=room_number)
        for feature in rooms[room_number]:
            f = feature_dict[feature]
            r.features.append(f)
        rooms_dict[room_number] = r
        get_db().add(r)

    # the types of team
    teamtype_dict = {}
    teamtype_dict['single'] = models.TeamType(
        name='single',
        priority=4,
        advance_time=7 * 2  # 2 weeks
    )
    get_db().add(teamtype_dict['single'])
    get_db().add(
        models.TeamType(
            name='other_team',
            priority=4,
            advance_time=7 * 2  # 2 weeks
        ))
    get_db().add(
        models.TeamType(
            name='class',
            priority=3,
            advance_time=7 * 2  # 2 weeks
        ))
    get_db().add(
        models.TeamType(
            name='colab_class',
            priority=2,
            advance_time=7 * 2  # 2 weeks
        ))
    get_db().add(
        models.TeamType(
            name='senior_project',
            priority=1,
            advance_time=7 * 2  # 2 weeks
        ))

    # the permissions

    perm_names = [
        'team.create', 'team.create.elevated', 'team.delete',
        'team.delete.elevated', 'team.read', 'team.read.elevated',
        'team.update', 'team.update.elevated', 'reservation.create',
        'reservation.delete', 'reservation.delete.elevated',
        'reservation.read', 'reservation.update',
        'reservation.update.elevated', 'room.update.elevated',
        'room.create.elevated', 'room.read', 'room.delete.elevated',
        'feature.create', 'feature.delete', 'feature.update', 'feature.read',
        'role.create', 'role.delete', 'role.update'
    ]
    perm_dict = {}
    for perm in perm_names:
        p = models.Permission(name=perm)
        get_db().add(p)
        perm_dict[perm] = p

    roles = {
        'student': [
            'team.create', 'team.delete', 'team.read', 'team.update',
            'reservation.create', 'reservation.delete', 'reservation.read',
            'reservation.update', 'room.read', 'feature.read'
        ],
        'labbie': [
            'team.create', 'team.delete', 'team.read', 'team.update',
            'reservation.create', 'reservation.delete', 'reservation.read',
            'reservation.update', 'room.read', 'feature.read',
            'team.read.elevated'
        ],
        'professor': [
            'team.create', 'team.delete', 'team.read', 'team.update',
            'reservation.create', 'reservation.delete', 'reservation.read',
            'reservation.update', 'room.read', 'feature.read',
            'team.create.elevated', 'team.read.elevated'
        ],
        'admin': [
            'team.create', 'team.create.elevated', 'team.delete',
            'team.delete.elevated', 'team.read', 'team.read.elevated',
            'team.update', 'team.update.elevated', 'reservation.create',
            'reservation.delete', 'reservation.delete.elevated',
            'reservation.read', 'reservation.update',
            'reservation.update.elevated', 'room.update.elevated',
            'room.create.elevated', 'room.read', 'room.delete.elevated',
            'feature.create', 'feature.delete', 'feature.update',
            'feature.read', 'role.create', 'role.delete', 'role.update'
        ]
    }
    users_dict = {}
    for role in roles:
        r = models.Role(name=role)
        for permission in roles[role]:
            p = perm_dict[permission]
            r.permissions.append(p)
        get_db().add(r)
        # seed a user TODO don't do this in production?
        u = models.User(name=role, email=role + "@example.com")
        u.roles.append(r)
        team = models.Team(u.name)
        team_type = teamtype_dict['single']
        team.team_type = team_type
        team.members.append(u)
        u.teams.append(team)
        users_dict[role] = u
        get_db().add(team)
        get_db().add(u)

    reservation = models.Reservation(
        start=datetime.datetime.now() + datetime.timedelta(days=7),
        end=datetime.datetime.now() + datetime.timedelta(days=7, hours=1),
        team=users_dict['admin'].teams[0],
        room=rooms_dict['1660'],
        created_by=users_dict['admin'])
    get_db().add(reservation)

    get_db().commit()
예제 #9
0
import initial_data as data
import models

users_collection = []
for user in data.users:
    users_collection.append(
        models.User(user['id'], user['name'], user['email'],
                    user['created_at']))

roles_collection = []
for role in data.roles:
    roles_collection.append(models.Role(role['id'], role['name']))

action_types_collection = []
for action_type in data.action_types:
    action_types_collection.append(
        models.ActionType(action_type['id'], action_type['name']))

user_role_relation_collection = []
for user_role_relation in data.user_role_relations:
    user_role_relation_collection.append(
        models.UserRoleRelation(user_role_relation['user_id'],
                                user_role_relation['role_id']))

action_type_role_relation_collection = []
for action_type_role_relation in data.action_type_role_relations:
    action_type_role_relation_collection.append(
        models.ActionTypeRoleRelation(
            action_type_role_relation['action_type_id'],
            action_type_role_relation['role_id']))