The first sample
from enamel import authentication, deployment, pages, servers, storage, deferreds import enamel class notIndex(pages.Standard): def body(self): return "This is a child page of the index" class Index(pages.Standard): arbitraryArguments = True childPages = {'notindex': notIndex} def body(self): return str(self.arguments) class Storage(storage.Vanilla): @deferreds.deferredGenerator # Storage functions should always be deferred. def authenticateUser(self, username, password): """ Usualy authentication logic is more complex than this...""" if username=="test" and password=="test": yield True else: yield False class Test(enamel.Enamel): indexPage = Index loginPage = pages.Login storage = Storage() anonymousAccess = True authenticator = authentication.DatabaseAuthenticator server = servers.TwistedWeb port = 8080 deployment.run('testApp', [ Test() ])
Example of using a real database
Using a real database (in this case SQLite) with anonymous access.
from enamel import authentication, deployment, pages from enamel import servers, storage, deferreds, tags from enamel import sql, form, url import enamel import sha class CreateUser(pages.Standard): def form_addUser(self, data): addUser = form.Form() addUser.addField('name', form.String(), label = "Username") addUser.addField('password', form.String(), form.CheckedPassword, label = "Password") addUser.addAction(self.addUser) return addUser def addUser(self, context, f, data): # data returned from a form is in Unicode. self.enamel.storage.addUser( data['name'].encode(), sha.sha(data['password'].encode()).hexdigest() ) return url.root def body(self): return tags.directive('form addUser') class Index(pages.Standard): arbitraryArguments = True # Enable REST style arguments to the page childPages = { 'login': pages.Login, 'addUser': CreateUser } def body(self): if self.avatarId: user = self.avatarId.username else: user = "Anonymous" return tags.div[ tags.a(href="/addUser")["Add User"], tags.br, tags.a(href="/login")["Login"], tags.br, "You are logged in as: ", user ] class SQLStorage(storage.SQL): tables = { 'users':[ sql.Column('uid', sql.Integer, primary_key = True), sql.Column('username', sql.String(255)), sql.Column('password', sql.String(255)) ] } @sql.transact def authenticateUser(self, username, password): return self.users.select( sql.and_( self.users.c.username==username, self.users.c.password==sha.sha(password).hexdigest() ) ).execute().fetchone() @sql.transact def addUser(self, username, password): return self.users.insert().execute( username = username, password = password ) class Test(enamel.Enamel): indexPage = Index loginPage = pages.Login storage = SQLStorage('sqlite:///test.db') anonymousAccess = True authenticator = authentication.DatabaseAuthenticator server = servers.TwistedWeb port = 8080 deployment.run('testApp', [ Test() ])
Using LDAP Authentication
class Test(enamel.Enamel): indexPage = Index loginPage = pages.Login anonymousAccess = False authenticator = authentication.LDAPAuthenticator ldapAuthUrl = "ldap://localhost:389/dc=test,dc=co,dc=za,o=ORG?uid?sub?" ldapAuthDn = "cn=Manager, o=ORG" ldapBindPassword = "password" server = servers.TwistedWeb port = 8080
