[Grok-dev] LoginDemo and PlainLoginDemo

Sebastian Ware sebastian at urbantalk.se
Sun Jan 13 12:31:02 EST 2008


I might be making a fool of myself, and I haven't tested this code,  
but I would have thought that by adding the following to your  
PlainLoginDemo, you would have easily extendable user objects that  
would support automatic form generation etc. (I guess you could  
compact it further by removing the interfaces. Then you would go from  
49 LOC down to 32 LOC)

"""
In order to access the user objects you write:

userhandler = IUserDatabase(grok.getSite())
user = userhandler.getUser(username)
user.email = 'iam at email.com'
...or whatever you wish to do...
"""

from zope.interface import Interface
from zope import schema
from BTrees.OOBTree import OOTreeSet
from logindemo.app import LoginDemo

class IUser(Interface):
     """
     This is the edit history annotation.
     """
	title = schema.TextLine(title=u"email", required=False)
     title = schema.Text(title=u"description", required=False)

class IUserDatabase(Interface):
     """
     This is the user annotation mechanism.
     """
     def addUser(username, email, description):
         """ Add a user object to the user database. """
     def getUser(username):
         """ Get a user object. """
	def delUser(username):
	        """ Remove a user object. """

class User(grok.Model):
	grok.implements(IUser)
	
	def __init__(self, email=None, description=None):
		self.email = email
		self.description = description

class UserDatabase(grok.Annotation):
     grok.implements(IUserDatabase)
     grok.context(LoginDemo)

     def __init__(self):
         self._users = OOTreeSet() # I should probably use some other  
container...

     def addUser(self, username, email, description):
		if self._users.has_key(username):
			# Raise some kind of error?
		else:
			self._users[username] = User(email, description)
		return self.getUser(username)			

     def getUser(self, username):
		return self._users[username]
		
	def delUser(self, username):
		del self._users[username]
			
Mvh Sebastian

13 jan 2008 kl. 16.06 skrev Luciano Ramalho:

> Today I removed everything related to annotations from the LoginDemo
> and created PlainLoginDemo:
>
> http://svn.zope.org/grokapps/PlainLoginDemo/
>
> Removing the email field resulted in a reduction of more than 100
> lines of code, because then I did not need to define an interface but
> used the predefined IInternalPrincipal interface; email validation and
> the UserData adapter also became unnecessary.
>
> I also added docstrings and comments.
>
> I will continue to work on both LoginDemo and PlainLogin demo over the
> next few days.
>
> Regards,
>
> Luciano
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev



More information about the Grok-dev mailing list