To explain my problem I will be referring to a basic user class.
The urls would look like:
localhost/user
localhost/user/sam
First point of misunderstanding:
Do I set up a container object with a name like UserContainer
which has the __getitem__ method?
The problem I see with this is that it breaks the default traversal
mechanism whereby the
resource is called by the path segment. By this I mean, a class named
UserContainer is not
called by a path segment like localhost/usercontainer
but rather I would have to use an if clause in the parent's
__getitem__ method that calls it if the
key = 'user'. Should I use an interface for both the container and
leaf? Should I not have a container at all?
Second point of misunderstanding:
I would like to keep my leaf class representative of the underlying
database schema.
Meaning I do not want the instantiation of it to depend on whether or
not it has already existed.
I realize this case would not happen with zobd because it saves the
instance object directly in the db.
But with mongo, all I have saved in the db are the attributes.
So I have to create a new instance of the class and then hydrate the
attributes.
A problem occurs if I have an attribute with specific method to set
it, because
if I am just resetting it, them I do not want to call this mehod.
However avoiding
it means I would have to have a conditional clause in the __init__
that checks to see if
the obect has already existed or create a new class which inherits
this lead class and overides __init__.
Thank you for any guidance you may bring.
The code which explains this scenario follows:
class Root(dict):
__name__ = None
__parent__ = None
def __init__(self, request):
self.request = request
def __getitem__(self, key):
if key == 'user':
return UserContainer('user', self)
doc = self.request.db.company.find_one({'__name__':key})
etc...
def get_root(request):
root = Root(request):
return root
class UserContainer(object):
__name__ = 'user'
def __init__(self, parent):
self.__parent__ = parent
self.request = parent.request
def __getitem__(self, key):
doc = self.request.db.user.find_one({'username':key})
if doc is None:
raise KeyError
user = UserHydratable()
for k, v in doc.items():
setattr(user, k, v)
user.__parent__ = self
user.__name__ = key
return user
class User(object):
def __init__(self, data):
self.username= data['username']
self.groups = ['editors']
self.created = datetime.datetime.now
self.password = self.setPassword(data['password'])
def setPassword(self, unencoded):
bcrypt = cryptacular.bcrypt.BCRYPTPasswordManager()
return bcrypt.encode(unencoded)
class UserHydratable(User):
def __init__(self):
pass
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.