* basic SQLAlchemy model
* menu handling code * SQLAlchemy setup in environent.py, base.py and websetup.py
This commit is contained in:
parent
292c102db8
commit
096df406b8
9 changed files with 125 additions and 5 deletions
|
@ -0,0 +1,15 @@
|
|||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
|
||||
from gnuviechadminweb.model import meta
|
||||
|
||||
def init_model(engine):
|
||||
"""Call me before using any of the tables or classes in the model."""
|
||||
|
||||
sm = orm.sessionmaker(autoflush=True, transactional=True, bind=engine)
|
||||
|
||||
meta.engine = engine
|
||||
meta.Session = orm.scoped_session(sm)
|
||||
|
||||
from gnuviechadminweb.model import menu
|
||||
from gnuviechadminweb.model.menu import Menu, User, Role
|
64
gnuviechadminweb/model/menu.py
Normal file
64
gnuviechadminweb/model/menu.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
# -*- python -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
|
||||
from gnuviechadminweb.model import meta
|
||||
|
||||
t_menu = \
|
||||
sa.Table("menu", meta.metadata,
|
||||
sa.Column("id", sa.types.Integer, primary_key=True),
|
||||
sa.Column("title", sa.types.String(40), nullable=False),
|
||||
sa.Column("controller", sa.types.String(40), nullable=False),
|
||||
sa.Column("action", sa.types.String(40), nullable=False)
|
||||
)
|
||||
|
||||
t_user = \
|
||||
sa.Table("user", meta.metadata,
|
||||
sa.Column("id", sa.types.Integer, primary_key=True),
|
||||
sa.Column("name", sa.types.String(40), nullable=False),
|
||||
sa.Column("password", sa.types.String(128), nullable=False)
|
||||
)
|
||||
|
||||
t_role = \
|
||||
sa.Table("role", meta.metadata,
|
||||
sa.Column("id", sa.types.Integer, primary_key=True),
|
||||
sa.Column("name", sa.types.String(40), nullable=False)
|
||||
)
|
||||
|
||||
t_menu_role = \
|
||||
sa.Table("menu_role", meta.metadata,
|
||||
sa.Column("id", sa.types.Integer, primary_key=True),
|
||||
sa.Column("menu_id", sa.types.Integer, sa.ForeignKey(t_menu.c.id)),
|
||||
sa.Column("role_id", sa.types.Integer, sa.ForeignKey(t_role.c.id))
|
||||
)
|
||||
|
||||
t_user_role = \
|
||||
sa.Table("user_role", meta.metadata,
|
||||
sa.Column("id", sa.types.Integer, primary_key=True),
|
||||
sa.Column("user_id", sa.types.Integer, sa.ForeignKey(t_user.c.id)),
|
||||
sa.Column("role_id", sa.types.Integer, sa.ForeignKey(t_role.c.id))
|
||||
)
|
||||
|
||||
class Menu(object):
|
||||
@classmethod
|
||||
def allowed(cls, user=None):
|
||||
menu_q = meta.Session.query(cls)
|
||||
return menu_q.all()
|
||||
|
||||
class User(object):
|
||||
pass
|
||||
|
||||
class Role(object):
|
||||
pass
|
||||
|
||||
orm.mapper(Menu, t_menu, {
|
||||
'roles' : orm.relation(Role, secondary = t_menu_role),
|
||||
})
|
||||
orm.mapper(Role, t_role, properties = {
|
||||
'users' : orm.relation(User, secondary = t_user_role),
|
||||
'menus' : orm.relation(Menu, secondary = t_menu_role),
|
||||
})
|
||||
orm.mapper(User, t_role, properties = {
|
||||
'roles' : orm.relation(User, secondary = t_user_role)
|
||||
})
|
14
gnuviechadminweb/model/meta.py
Normal file
14
gnuviechadminweb/model/meta.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
"""SQLAlchemy Metadata and Session object"""
|
||||
from sqlalchemy import MetaData
|
||||
|
||||
__all__ = ['engine', 'metadata', 'Session']
|
||||
|
||||
# SQLAlchemy database engine. Updated by model.init_model().
|
||||
engine = None
|
||||
|
||||
# SQLAlchemy session manager. Updated by model.init_model().
|
||||
Session = None
|
||||
|
||||
# Global metadata. If you have multiple databases with overlapping table
|
||||
# names, you'll need a metadata for each database.
|
||||
metadata = MetaData()
|
Loading…
Add table
Add a link
Reference in a new issue