[web2py] Re: Why doesn't validate_and_insert() return on error?

2013-09-10 Thread Christopher Morlier
Hi Anthony,

I discovered the problem with the 200 code.  It was a basically a cut and 
paste error.  The code I was using had a try-except block wrapping the body 
of the POST handler method, which looks like:
try:
   ...
except Exception as e:
   return str(e)

I removed the exception and things behave as expected.

Sorry for the wasted bandwidth.

Thanks for the help,
Chris


On Monday, September 9, 2013 4:12:13 PM UTC-5, Anthony wrote:
>
> On Monday, September 9, 2013 4:40:43 PM UTC-4, Christopher Morlier wrote:
>
>> Okay, that is what I what I was anticipating when I asked, "Do the 
>> validators not check required fields?"
>>
>> The behavior is still a little surprising to me, since 
>> validate_and_insert() is part of the DAL, and it has an explicit method of 
>> reporting errors.
>>
>
> The "validators" are the things you put in the "requires" attribute of a 
> field, typically for use with forms. The .validate_and_insert() method was 
> added to enable manual (i.e., non-form) inserts that still run the 
> validators. The "required" attribute is not a validator in this sense -- it 
> is enforced directly by the DAL on any insert. The intention of 
> .validate_and_insert() is to behave as form validation would -- that is why 
> it doesn't do anything special with the "required" attribute. The 
> "required" attribute is still processed as usual, but by the .insert() 
> method that gets called by .validate_and_insert(). If your goal is to use 
> validators to require values, simply use the IS_NOT_EMPTY() validator.
>  
>
>>  It is also surprising to me that an error returns a 200 code on an 
>> error, but maybe that is just a problem for my use case.
>>
>
> That does seem odd -- can you show the full code and how you are making 
> the call?
>
> Anthony
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Why doesn't validate_and_insert() return on error?

2013-09-09 Thread Christopher Morlier
Okay, that is what I what I was anticipating when I asked, "Do the 
validators not check required fields?"

The behavior is still a little surprising to me, since 
validate_and_insert() is part of the DAL, and it has an explicit method of 
reporting errors.  It is also surprising to me that an error returns a 200 
code on an error, but maybe that is just a problem for my use case.

Wrapping the call with a try block and handling the exceptions seems to be 
working for me.

Thanks for the clarification!

On Monday, September 9, 2013 3:19:17 PM UTC-5, Anthony wrote:
>
> From the book:
>
> Notice that requires=... is enforced at the level of forms, required=True is 
> enforced at the level of the DAL (insert), while notnull,unique and 
> ondelete are enforced at the level of the database. While they sometimes 
> may seem redundant, it is important to maintain the distinction when 
> programming with the DAL.
>
> If you specified required=True, that is not handled by form validation or 
> .validate_and_insert(). Instead, that is enforced by the DAL during an 
> .insert(), and failure simply results in an exception being thrown, not a 
> validation error being returned.
>
> Anthony
>
> On Monday, September 9, 2013 12:07:46 PM UTC-4, Christopher Morlier wrote:
>>
>> At the moment, I am not explicitly providing any validators; so I expect 
>> only the default validators for my field types.
>>
>> Actually, the test where this arises is when I leave out a required 
>> field, and I get the error "Table: missing required field: time_stamp".  Do 
>> the validators not check required fields?
>>
>>
>> On Monday, September 9, 2013 10:28:56 AM UTC-5, Niphlod wrote:
>>>
>>> how do you specify the validators for your required fields ?
>>>
>>> On Monday, September 9, 2013 5:09:04 AM UTC+2, Christopher Morlier wrote:
>>>>
>>>> Hello,
>>>>
>>>> I am implementing a RESTful API and have the following code in my POST 
>>>> handler:
>>>>
>>>> ret = db.recording.validate_and_insert(**fixed_fields)
>>>> if ret.errors:
>>>> raise HTTP(400, 'Validation failed: ' + str(ret.errors))
>>>>
>>>> url = URL('api', args=('recording',ret.id))
>>>> response.headers['Location'] = url
>>>> response.status = 201
>>>> return dict(link=A('Recording ', ret.id, _href=url))
>>>>
>>>> On success, things work as expected.  However, if I omit a required 
>>>> field, none of my code after validate_and_insert() is called.  The client 
>>>> receives a HTTP 200 response as if all is well, albeit with an appropriate 
>>>> text description of the error.
>>>>
>>>> This is not how validate_and_insert() is described in chapter 6 of the 
>>>> book.  Is there a reason it is different?
>>>>
>>>> As a work around I added a try-except block around the 
>>>>  validate_and_insert() call, expecting an HTTP exception, but discovered 
>>>> it 
>>>> is actually a RuntimeError exception.
>>>> Is this the recommended work around or is there a better solution?
>>>>
>>>> I browsed through the code, but it isn't clear to me where the 
>>>> exception is thrown.  However, I believe I noticed two typos in the 
>>>> "validate_and_insert, validate_and_update" section:"ret.error" should be 
>>>> "ret.errors" and "res.updated" should be "ret.updated".
>>>>
>>>> BTW, I am running 2.5.1-stable+timestamp.2013.06.06.15.39.19 on 
>>>> Apache/2.2.16 (Debian)
>>>>
>>>> Thanks for your help,
>>>> Chris
>>>>
>>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Why doesn't validate_and_insert() return on error?

2013-09-09 Thread Christopher Morlier
Basically it is this:

db.define_table('recording',
Field('time_stamp','bigint',required=
True,
  comment='Recording creation time in milliseconds.'),
Field('system_id', 'reference system',  required=
True,
  comment='Reference to recording system.'),
Field('video', 'upload',required=
True,
  uploadfolder=os.path.join(request.folder,'upload/videos'),
  uploadseparate=True, length=256,
  comment='Filename for the uploaded file.'),
Field('description',   'string',   length=256,
  comment='Optional description for recordings.'),
format=format_recording
)


On Monday, September 9, 2013 1:52:43 PM UTC-5, Niphlod wrote:
>
> ok, let me rephrase: what is your model ?
>
> On Monday, September 9, 2013 6:07:46 PM UTC+2, Christopher Morlier wrote:
>>
>> At the moment, I am not explicitly providing any validators; so I expect 
>> only the default validators for my field types.
>>
>> Actually, the test where this arises is when I leave out a required 
>> field, and I get the error "Table: missing required field: time_stamp".  Do 
>> the validators not check required fields?
>>
>>
>> On Monday, September 9, 2013 10:28:56 AM UTC-5, Niphlod wrote:
>>>
>>> how do you specify the validators for your required fields ?
>>>
>>> On Monday, September 9, 2013 5:09:04 AM UTC+2, Christopher Morlier wrote:
>>>>
>>>> Hello,
>>>>
>>>> I am implementing a RESTful API and have the following code in my POST 
>>>> handler:
>>>>
>>>> ret = db.recording.validate_and_insert(**fixed_fields)
>>>> if ret.errors:
>>>> raise HTTP(400, 'Validation failed: ' + str(ret.errors))
>>>>
>>>> url = URL('api', args=('recording',ret.id))
>>>> response.headers['Location'] = url
>>>> response.status = 201
>>>> return dict(link=A('Recording ', ret.id, _href=url))
>>>>
>>>> On success, things work as expected.  However, if I omit a required 
>>>> field, none of my code after validate_and_insert() is called.  The client 
>>>> receives a HTTP 200 response as if all is well, albeit with an appropriate 
>>>> text description of the error.
>>>>
>>>> This is not how validate_and_insert() is described in chapter 6 of the 
>>>> book.  Is there a reason it is different?
>>>>
>>>> As a work around I added a try-except block around the 
>>>>  validate_and_insert() call, expecting an HTTP exception, but discovered 
>>>> it 
>>>> is actually a RuntimeError exception.
>>>> Is this the recommended work around or is there a better solution?
>>>>
>>>> I browsed through the code, but it isn't clear to me where the 
>>>> exception is thrown.  However, I believe I noticed two typos in the 
>>>> "validate_and_insert, validate_and_update" section:"ret.error" should be 
>>>> "ret.errors" and "res.updated" should be "ret.updated".
>>>>
>>>> BTW, I am running 2.5.1-stable+timestamp.2013.06.06.15.39.19 on 
>>>> Apache/2.2.16 (Debian)
>>>>
>>>> Thanks for your help,
>>>> Chris
>>>>
>>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Why doesn't validate_and_insert() return on error?

2013-09-09 Thread Christopher Morlier
At the moment, I am not explicitly providing any validators; so I expect 
only the default validators for my field types.

Actually, the test where this arises is when I leave out a required field, 
and I get the error "Table: missing required field: time_stamp".  Do the 
validators not check required fields?


On Monday, September 9, 2013 10:28:56 AM UTC-5, Niphlod wrote:
>
> how do you specify the validators for your required fields ?
>
> On Monday, September 9, 2013 5:09:04 AM UTC+2, Christopher Morlier wrote:
>>
>> Hello,
>>
>> I am implementing a RESTful API and have the following code in my POST 
>> handler:
>>
>> ret = db.recording.validate_and_insert(**fixed_fields)
>> if ret.errors:
>> raise HTTP(400, 'Validation failed: ' + str(ret.errors))
>>
>> url = URL('api', args=('recording',ret.id))
>> response.headers['Location'] = url
>> response.status = 201
>> return dict(link=A('Recording ', ret.id, _href=url))
>>
>> On success, things work as expected.  However, if I omit a required 
>> field, none of my code after validate_and_insert() is called.  The client 
>> receives a HTTP 200 response as if all is well, albeit with an appropriate 
>> text description of the error.
>>
>> This is not how validate_and_insert() is described in chapter 6 of the 
>> book.  Is there a reason it is different?
>>
>> As a work around I added a try-except block around the 
>>  validate_and_insert() call, expecting an HTTP exception, but discovered it 
>> is actually a RuntimeError exception.
>> Is this the recommended work around or is there a better solution?
>>
>> I browsed through the code, but it isn't clear to me where the exception 
>> is thrown.  However, I believe I noticed two typos in the 
>> "validate_and_insert, validate_and_update" section:"ret.error" should be 
>> "ret.errors" and "res.updated" should be "ret.updated".
>>
>> BTW, I am running 2.5.1-stable+timestamp.2013.06.06.15.39.19 on 
>> Apache/2.2.16 (Debian)
>>
>> Thanks for your help,
>> Chris
>>
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Why doesn't validate_and_insert() return on error?

2013-09-09 Thread Christopher Morlier
Hello,

I am implementing a RESTful API and have the following code in my POST 
handler:

ret = db.recording.validate_and_insert(**fixed_fields)
if ret.errors:
raise HTTP(400, 'Validation failed: ' + str(ret.errors))

url = URL('api', args=('recording',ret.id))
response.headers['Location'] = url
response.status = 201
return dict(link=A('Recording ', ret.id, _href=url))

On success, things work as expected.  However, if I omit a required field, 
none of my code after validate_and_insert() is called.  The client receives 
a HTTP 200 response as if all is well, albeit with an appropriate text 
description of the error.

This is not how validate_and_insert() is described in chapter 6 of the 
book.  Is there a reason it is different?

As a work around I added a try-except block around the 
 validate_and_insert() call, expecting an HTTP exception, but discovered it 
is actually a RuntimeError exception.
Is this the recommended work around or is there a better solution?

I browsed through the code, but it isn't clear to me where the exception is 
thrown.  However, I believe I noticed two typos in the 
"validate_and_insert, validate_and_update" section:"ret.error" should be 
"ret.errors" and "res.updated" should be "ret.updated".

BTW, I am running 2.5.1-stable+timestamp.2013.06.06.15.39.19 on 
Apache/2.2.16 (Debian)

Thanks for your help,
Chris

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.