def __init__(self):
        self.session = db_session()
        # set user and role
        if g.user is not None and not g.user.is_anonymous:
            self.user = self.session.query(models.User).filter_by(id=g.user.id).first()

            if self.user.current_user_role_id is None:
                role_data = (self.session.query(models.Role, models.UserRole)
                             .join(models.UserRole)
                             .filter(models.UserRole.user_id==self.user.id).first())

                if role_data is None:
                    self.user = None
                    self.role = None
                    return

                self.user.current_user_role_id =  role_data.UserRole.id
                self.session.add(self.user)
                self.session.commit()
                self.role = role_data.Role
            else:
                self.role = (self.session.query(models.Role)
                             .join(models.UserRole)
                             .filter(models.UserRole.id==self.user.current_user_role_id).first())

            g.role_id = self.role.id
            facility_res = (self.session.query(models.Facility, models.Role,
                                               models.Level)
                            .join(models.Role, models.UserRole,
                                  models.Level)
                            .filter(models.UserRole.user_id ==
                                    self.user.id).order_by(models.Facility.id,
                                                           models.Level.order).all())

            self.facilities = {}
            self.facility = None
            self.level_id = None
            for fac in facility_res:
                if fac.Facility.name not in self.facilities:
                    self.facilities[fac.Facility.name] = fac.Role.id
                if fac.Role.id == self.role.id:
                    self.facility = fac.Facility
                    self.level_id = fac.Role.level_id
                    g.root_org_id = fac.Facility.root_organization_id

            if 'routes' in session and session['routes']:
                self.routes = session['routes']
                self.set_routes()
            else:
                self.set_routes()
        else:
            self.user = None
            self.role = None
            self.routes = []
            self.level_id = None
            self.facility = None
def get_table(table, attr = 'name'):
    session = db_session()
    filters = [getattr(TableObject, attr) == table]
    row = session.query(TableObject).filter(*filters).first()

    try:
        if hasattr(row, 'admin_table') and row.admin_table == 1:
            module_model = import_module('iggybase.admin.models')
        else:
            module_model = import_module('iggybase.models')
        table_object = getattr(module_model, to_camel_case(table))

    except AttributeError:
        print('Abort' + table)
        logging.info('abort ' + table)
        abort(403)

    return table_object
 def __init__(self, active=1):
     self.active = active
     self.session = db_session()
    def get_session(self):
        session = db_session()

        return session