Hello,

I would like to be able to abandon a request however deeply nested within a function and return a json object.

pylons.controllers.util.abort is the equivalent of what I would like, but it only returns HTML, even if the request was json.

This is what I'd like the code to look like:

class MyController(BaseController):

    @expose('json')
    def get_one(self, name):
         person = self._get_person(name)

         # do something else

         return dict(person=person)

    @expose('json')
    def delete(self, name):
         person = self._get_person(name)
         DBsession.delete(person)

    def _get_person(self, name):
          try:
               p = DBSession.query(Person).filter_by(name=name).one()
          except NoResultFound:
               abort_json(code=77, message="This person does not exist.")
          return p


Without the abort_json call, I would have to duplicate that code in all my methods, because I can't return a response object from within a nested function. Unless I'm missing something...

I looked at the error/document method, but I can't get it to return json when the request comes from a mymethod.json, unless I delete the normal @expose('copr.templates.error'). I'd also like the response object to have a 200 HTTP status code.

I'm using TurboGears2.0

Thanks.


--
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en.

Reply via email to