Validating documents?

27 views
Skip to first unread message

Voltron

unread,
Feb 25, 2012, 12:24:29 PM2/25/12
to Humongolus
Hi Christopher! Just as I was implementing my first Humongolus clases
I noticed it could be much "cleaner" if one could validate documents
before saving as with forms:

class Car(orm.Document):
_db = "test"
_collection = "cars"
owner = field.DynamicDocument()
make = field.Char()
model = field.Char()
year = field.Date()
silly_date = field.TimeStamp()

I could just call instead of placing it in a try except conditional
and wait for validation tracebacks:

car.validate() # True or False

What do you think?

Christopher Coté

unread,
Feb 25, 2012, 1:02:26 PM2/25/12
to humon...@googlegroups.com
A validate method on the Document class as well, that just returns True or False, hmmm.

I did have the save method doing that originally, but I wanted to keep it consistent through-out, so I went with the more pythonic implementation of raising an Exception.

I'll think about it.

If that's how you prefer to have it act,for now you could extend the Document class, and just create a validate method for your use.

Look at how .save() calls ._errors() then throws an exception, you could do the same and just return true or false.

Voltron

unread,
Feb 26, 2012, 6:30:16 PM2/26/12
to Humongolus
It would be nice. As it is, I noticed that one cannot wrap the calls
in a try/except clause:

try:
id = user.save()
except Exception, detail:
page_content = str(detail)
else:
page_content = id

The tracebacks are only emitted by the logger,

Christopher S Coté

unread,
Feb 26, 2012, 7:47:16 PM2/26/12
to humon...@googlegroups.com
you can definitely wrap them.

Check out the test cases, that's how they all pass.

try:
id = obj.save()
page_content = id
except DocumentException as e:
print e

On 02/26/2012 05:30 PM, Voltron wrote:
> It would be nice. As it is, I noticed that one cannot wrap the calls
> in a try/except clause:
>
> try:
> id = user.save()
> except Exception, detail:
> page_content = str(detail)
> else:
> page_content = id
>
> The tracebacks are only emitted by the logger,
>

Voltron

unread,
Feb 27, 2012, 12:40:23 PM2/27/12
to Humongolus
I am not exactly sure what is going on, but I am even having problems
with the DocumentException class

Traceback (most recent call last):
File "/applications/test/env/lib/python2.6/site-packages/tornado/
web.py", line 988, in _execute
getattr(self, self.request.method.lower())(*args, **kwargs)
File "/applications/test/handlers/accounts.py", line 71, in get
except DocumentException as e:
NameError: global name 'DocumentException' is not defined

Is the casse different for a document and not a from?

Thanks

Christopher S Coté

unread,
Feb 27, 2012, 12:48:26 PM2/27/12
to humon...@googlegroups.com
are you explicitly importing the DocumentException?

looks like your not, try orm.DocumentException

or humongolus.DocumentException

depending on your imports

try:
_id = obj.save()
print _id
except orm.DocumentException as e:
print e.errors


On 02/27/2012 11:40 AM, Voltron wrote:
> I am not exactly sure what is going on, but I am even having problems
> with the DocumentException class
>
> Traceback (most recent call last):
> File "/applications/test/env/lib/python2.6/site-packages/tornado/
> web.py", line 988, in _execute
> getattr(self, self.request.method.lower())(*args, **kwargs)
> File "/applications/test/handlers/accounts.py", line 71, in get
> except DocumentException as e:
> NameError: global name 'DocumentException' is not defined
>
> Is the casse different for a document and not a from?
>
> Thanks
>

Voltron

unread,
Feb 27, 2012, 12:55:39 PM2/27/12
to Humongolus
I noticed that a few seconds after posting :) but I still cannot wrap
the save() though:

#
------------------------------------------------------------------------------
import uuid
import tornado.web
import datetime
from models.users import User

from humongolus import DocumentException

Christopher S Coté

unread,
Feb 27, 2012, 12:58:27 PM2/27/12
to humon...@googlegroups.com
If you can post your models and code, I can give you a hand.

Also, are you getting any errors? Post those as well.


On 02/27/2012 11:55 AM, Voltron wrote:
> I noticed that a few seconds after posting :) but I still cannot wrap
> the save() though:
>
> #
> ------------------------------------------------------------------------------
> import uuid
> import tornado.web
> import datetime
> from models.users import User
>
> from humongolus import DocumentException
>
>

Reply all
Reply to author
Forward
0 new messages