-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 53 -- Separate DB-initated object field setting from user intiated
- Loading branch information
Jeff Jenkins
committed
Apr 7, 2011
1 parent
53cf3b7
commit b23f69b
Showing
4 changed files
with
61 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,40 @@ | ||
''' | ||
This page is going to go through some more advanced modeling techniques | ||
using forward and self-references | ||
''' | ||
|
||
|
||
from mongoalchemy.document import Document, DocumentField | ||
from mongoalchemy.fields import * | ||
from datetime import datetime | ||
|
||
from pprint import pprint | ||
class Event(Document): | ||
name = StringField() | ||
children = ListField(DocumentField('Event'), default=[]) | ||
start = DateTimeField() | ||
children = ListField(DocumentField('Event')) | ||
begin = DateTimeField() | ||
end = DateTimeField() | ||
|
||
def __init__(self, name, parent=None): | ||
Document.__init__(self, name=name) | ||
self.children = [] | ||
if parent != None: | ||
parent.children.append(self) | ||
def __enter__(self): | ||
self.start = datetime.utcnow() | ||
self.begin = datetime.utcnow() | ||
return self | ||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.end = datetime.utcnow() | ||
|
||
root = Event('request') | ||
with Event('main_func', root) as br: | ||
with Event('setup', br): | ||
pass | ||
with Event('handle', br): | ||
pass | ||
with Event('teardown', br): | ||
with Event('request') as root: | ||
with Event('main_func', root) as br: | ||
with Event('setup', br): | ||
pass | ||
with Event('handle', br): | ||
pass | ||
with Event('teardown', br): | ||
pass | ||
with Event('cleanup', root): | ||
pass | ||
with Event('cleanup', root): | ||
pass | ||
|
||
print root.wrap() | ||
pprint(root.wrap()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from mongoalchemy.document import Document, DocumentField | ||
from mongoalchemy.fields import * | ||
from datetime import datetime | ||
from pprint import pprint | ||
|
||
class HashField(StringField): | ||
def set_value(self, instance, value, from_db=False): | ||
if from_db: | ||
super(HashField, self).set_value(instance, value) | ||
else: | ||
super(HashField, self).set_value(instance, str(hash(value))) | ||
|
||
class User(Document): | ||
password = HashField() | ||
|
||
from mongoalchemy.session import Session | ||
session = Session.connect('mongoalchemy-tutorial') | ||
session.clear_collection(User) | ||
|
||
user = User(password='pw') | ||
print user.password | ||
user.password = 'newpw' | ||
print user.password | ||
|
||
session.insert(user) | ||
|
||
loaded_user = session.query(User).one() | ||
|
||
print loaded_user.password | ||
print loaded_user.password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters