Changeset 4550
- Timestamp:
- 04/30/08 12:30:38 (3 months ago)
- Files:
-
- projects/tg.devtools/trunk/devtools/commands/quickstart.py (modified) (3 diffs)
- projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/config/middleware.py_tmpl (modified) (2 diffs)
- projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/controllers/root.py_tmpl (modified) (2 diffs)
- projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/model/__init__.py_tmpl (modified) (2 diffs)
- projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/templates/login.html (added)
- projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/websetup.py_tmpl (modified) (1 diff)
- projects/tg.devtools/trunk/setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/tg.devtools/trunk/devtools/commands/quickstart.py
r4479 r4550 99 99 help="use SQLAlchemy Elixir instead of SQLObject", 100 100 action="store_true", dest="elixir", default = False) 101 #parser.add_option("-i", "--identity",102 #help="provide Identity support",103 #action="store_true", dest="identity", default = False)101 parser.add_option("-i", "--identity", 102 help="provide Identity support", 103 action="store_true", dest="identity", default = False) 104 104 parser.add_option("-p", "--package", 105 105 help="package name for the code", … … 140 140 141 141 doidentity = self.identity 142 """while not doidentity:142 while not doidentity: 143 143 doidentity = raw_input("Do you need Identity " 144 144 "(usernames/passwords) in this project? [no] ") 145 145 doidentity = doidentity.lower() 146 146 if not doidentity or doidentity.startswith('n'): 147 self.identity ="none"147 self.identity = False 148 148 break 149 149 if doidentity.startswith("y"): … … 158 158 else: 159 159 self.identity = "sqlobject" 160 """ 160 161 161 self.name = pkg_resources.safe_name(self.name) 162 162 projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/config/middleware.py_tmpl
r4488 r4550 14 14 15 15 from {{package}}.config.environment import load_environment 16 {{if identity == "sqlalchemy"}} 17 from tgrepozewho.middleware import make_who_middleware 18 from {{package}}.model import User, Group, Permission, DBSession 19 20 # the criterion (ie instance of column) against which to mach 21 # the user name coming from a tentative login form 22 user_criterion = User.user_name 23 # the name of the column which is used as a unique identifier 24 user_id_col = 'user_id' 25 {{endif}} 16 26 17 27 def make_app(global_conf, full_stack=True, **app_conf): … … 50 60 app = TGWidgetsMiddleware(app, host_framework) 51 61 62 {{if identity == "sqlalchemy"}} 63 # Identity Middleware 64 app = make_who_middleware(app, config, User, user_criterion, user_id_col, 65 DBSession) 66 {{endif}} 67 52 68 if asbool(full_stack): 53 69 # Handle Python exceptions projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/controllers/root.py_tmpl
r4488 r4550 4 4 from pylons.i18n import ugettext as _ 5 5 #from tg import redirect, validate 6 #from {{package}}.model import DBSession 6 #from {{package}}.model import DBSession, metadata 7 8 {{if identity == "sqlalchemy"}} 9 from tgrepozewho import authorize 10 #from dbsprockets.dbmechanic.frameworks.tg2 import DBMechanic 11 #from dbsprockets.saprovider import SAProvider 12 {{endif}} 7 13 8 14 class RootController(BaseController): 9 15 {{if identity == "sqlalchemy"}} 16 #dbmechanic = DBMechanic(SAProvider(metadata), '/dbmechanic') 17 {{endif}} 10 18 @expose('{{package}}.templates.index') 11 19 def index(self): … … 16 24 def about(self): 17 25 return dict(page='about') 26 27 {{if identity == "sqlalchemy"}} 28 @expose('{{package}}.templates.about') 29 @authorize.require(authorize.has_permission('manage')) 30 def manage_permission_only(self, **kw): 31 return dict(page='about') 32 33 @expose('{{package}}.templates.about') 34 @authorize.require(authorize.is_user('editor')) 35 def editor_user_only(self, **kw): 36 return dict(page='about') 37 38 @expose('{{package}}.templates.login') 39 def login(self, **kw): 40 came_from = kw.get('came_from', '/') 41 return dict(page='login', header=lambda *arg: None, 42 footer=lambda *arg: None, came_from=came_from) 43 {{endif}} projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/model/__init__.py_tmpl
r4488 r4550 5 5 from pylons import config 6 6 7 from sqlalchemy import Column, MetaData, Table, types7 from sqlalchemy import Column, MetaData, Table, Integer, Unicode, DateTime, types 8 8 from sqlalchemy.orm import mapper, relation 9 10 # Global session manager. Session() returns the session object 9 {{if identity == "sqlalchemy"}} 10 import datetime 11 from sqlalchemy import ForeignKey 12 import md5 13 import sha 14 {{endif}} 15 16 # Global session manager. DBSession() returns the session object 11 17 # appropriate for the current web request. 12 18 DBSession = scoped_session(sessionmaker(autoflush=True, transactional=True)) … … 33 39 #mapper(Reflected, t_reflected) 34 40 41 {{if identity == "sqlalchemy"}} 42 groups_table = Table('tg_group', metadata, 43 Column('group_id', Integer, primary_key=True), 44 Column('group_name', Unicode(16), unique=True), 45 Column('display_name', Unicode(255)), 46 Column('created', DateTime, default=datetime.datetime.now) 47 ) 48 49 users_table = Table('tg_user', metadata, 50 Column('user_id', Integer, primary_key=True), 51 Column('user_name', Unicode(16), unique=True), 52 Column('email_address', Unicode(255), unique=True), 53 Column('display_name', Unicode(255)), 54 Column('password', Unicode(40)), 55 Column('created', DateTime, default=datetime.datetime.now) 56 ) 57 58 permissions_table = Table('tg_permission', metadata, 59 Column('permission_id', Integer, primary_key=True), 60 Column('permission_name', Unicode(16), unique=True), 61 Column('description', Unicode(255)) 62 ) 63 64 user_group_table = Table('tg_user_group', metadata, 65 Column('user_id', Integer, ForeignKey('tg_user.user_id', 66 onupdate="CASCADE", ondelete="CASCADE")), 67 Column('group_id', Integer, ForeignKey('tg_group.group_id', 68 onupdate="CASCADE", ondelete="CASCADE")) 69 ) 70 71 group_permission_table = Table('tg_group_permission', metadata, 72 Column('group_id', Integer, ForeignKey('tg_group.group_id', 73 onupdate="CASCADE", ondelete="CASCADE")), 74 Column('permission_id', Integer, ForeignKey('tg_permission.permission_id', 75 onupdate="CASCADE", ondelete="CASCADE")) 76 ) 77 # identity model 78 class Group(object): 79 """An ultra-simple group definition. 80 """ 81 def __repr__(self): 82 return '<Group: name=%s>' % self.group_name 83 84 class User(object): 85 """Reasonably basic User definition. Probably would want additional 86 attributes. 87 """ 88 def __repr__(self): 89 return '<User: email="%s", display name="%s">' % ( 90 self.email_address, self.display_name) 91 92 def permissions(self): 93 perms = set() 94 for g in self.groups: 95 perms = perms | set(g.permissions) 96 return perms 97 permissions = property(permissions) 98 99 def by_email_address(klass, email): 100 """A class method that can be used to search users 101 based on their email addresses since it is unique. 102 """ 103 session = DBSession() 104 return session.query(klass).filter(klass.email_address==email).first() 105 106 by_email_address = classmethod(by_email_address) 107 108 def by_user_name(klass, username): 109 """A class method that permits to search users 110 based on their user_name attribute. 111 """ 112 session = DBSession() 113 return session.query(klass).filter(klass.user_name==username).first() 114 115 by_user_name = classmethod(by_user_name) 116 117 def _set_password(self, password): 118 """encrypts password on the fly using the encryption 119 algo defined in the configuration 120 """ 121 algorithm = config.get('authorize.hashmethod', None) 122 self._password = self.__encrypt_password(algorithm, password) 123 124 def _get_password(self): 125 """returns password 126 """ 127 return self._password 128 129 password = property(_get_password, _set_password) 130 131 def __encrypt_password(self, algorithm, password): 132 """Hash the given password with the specified algorithm. Valid values 133 for algorithm are 'md5' and 'sha1'. All other algorithm values will 134 be essentially a no-op.""" 135 hashed_password = password 136 137 if isinstance(password, unicode): 138 password_8bit = password.encode('UTF-8') 139 140 else: 141 password_8bit = password 142 143 if "md5" == algorithm: 144 hashed_password = md5.new(password_8bit).hexdigest() 145 146 elif "sha1" == algorithm: 147 hashed_password = sha.new(password_8bit).hexdigest() 148 149 # TODO: re-add the possibility to provide own hasing algo 150 # here... just get the real config... 151 152 #elif "custom" == algorithm: 153 # custom_encryption_path = turbogears.config.get( 154 # "identity.custom_encryption", None ) 155 # 156 # if custom_encryption_path: 157 # custom_encryption = turbogears.util.load_class( 158 # custom_encryption_path) 159 160 # if custom_encryption: 161 # hashed_password = custom_encryption(password_8bit) 162 163 # make sure the hased password is an UTF-8 object at the end of the 164 # process because SQLAlchemy _wants_ a unicode object for Unicode columns 165 if not isinstance(hashed_password, unicode): 166 hashed_password = hashed_password.decode('UTF-8') 167 168 return hashed_password 169 170 def validate_password(self, password): 171 """Check the password against existing credentials. 172 """ 173 algorithm = config.get('authorize.hashmethod', None) 174 return self.password == self.__encrypt_password(algorithm, password) 175 176 class Permission(object): 177 """A relationship that determines what each Group can do 178 """ 179 pass 180 181 mapper(User, users_table, 182 properties=dict(_password=users_table.c.password)) 183 184 mapper(Group, groups_table, 185 properties=dict(users=relation(User, 186 secondary=user_group_table, backref='groups'))) 187 188 mapper(Permission, permissions_table, 189 properties=dict(groups=relation(Group, 190 secondary=group_permission_table, backref='permissions'))) 191 {{endif}} 35 192 # Normal tables may be defined and mapped at module level. 36 193 projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/websetup.py_tmpl
r4488 r4550 18 18 print "Creating tables" 19 19 model.metadata.create_all(bind=config['pylons.app_globals'].sa_engine) 20 21 {{if identity == "sqlalchemy"}} 22 u = model.User() 23 u.user_name = u'manager' 24 u.display_name = u'Exemple manager' 25 u.email_address = u'manager@somedomain.com' 26 u.password = u'managepass' 27 28 model.DBSession.save(u) 29 30 g = model.Group() 31 g.group_name = u'managers' 32 g.display_name = u'Managers Group' 33 34 g.users.append(u) 35 36 model.DBSession.save(g) 37 38 p = model.Permission() 39 p.permission_name = u'manage' 40 p.description = u'This permission give an administrative right to the bearer' 41 p.groups.append(g) 42 43 model.DBSession.save(p) 44 model.DBSession.flush() 45 46 u1 = model.User() 47 u1.user_name = u'editor' 48 u1.display_name = u'Exemple editor' 49 u1.email_address = u'editor@somedomain.com' 50 u1.password = u'editpass' 51 52 model.DBSession.save(u1) 53 model.DBSession.flush() 54 {{endif}} 55 20 56 model.DBSession.commit() 21 57 print "Successfully setup" projects/tg.devtools/trunk/setup.py
r4486 r4550 21 21 'TurboGears2', 'sqlalchemy-migrate >= 0.4.4', 22 22 'tw.forms', 'DBSprockets', 23 'tgrepozewho' 23 24 ], 24 25 entry_points='''