I believe i have found a proper way to display errors on the same page that 
causes them without having to modify web2py core files.

[image: 2019-01-20_152028.png]


This is useful on the development stage since we would see the problem 
directly without having to navigate to the admin area or switch between 
browser tabs.

We will need the python module mechanize that you can install by typing 
this on a command shell:
pip install mechanize

So we will need to set routes.py (see url rewrite 
<http://web2py.com/books/default/chapter/29/04/the-core?search=routes.py#URL-rewrite>
)
1.- Copy *routes.patterns.example.py* from the examples directory to you 
web2py root folder and rename it as "routes.py"
2.- Edit routes.py
Make sure admin is not reroted in routes_in and routes_out so leave the 
example lines uncommented
routes_in = (
    # do not reroute admin unless you want to disable it
    (BASE + '/admin', '/admin/default/index'),
    (BASE + '/admin/$anything', '/admin/$anything'),
    ...

routes_out = (
    # do not reroute admin unless you want to disable it
    ('/admin/$anything', BASE + '/admin/$anything'),
    ...

3.- Enable routes on error:
on routes.py add this line
routes_onerror = [(r"*/*", r'/admin/errors/index')]
Any error from any app would be processed by admin/errors/index

4.- On the admin app create a controller called errors.py and add the index 
function as:
def index():
    import mechanize
    
    code = request.vars.code
    print "Error code:", code
    ticket = request.vars.ticket
    url = "http://localhost/admin/default/ticket/"; + ticket

    br = mechanize.Browser()
    br.set_handle_robots(False)
    br.open(url)
    br.select_form(nr=0)
    br['password'] = "1234"
    res = br.submit()
    return res.read()

Have in mind you have to set the admin password, to do so:
run web2py.py located in the root folder
set a password on the server window when it pops up.
use this password on the code above for the admin/errors/index function 
br['password'] = "<whatever you typed on the screen above>"

Thats it enjoy!.

Disclaimer: Have in mind this technique is only suitable for development, 
not production.

If you know any way to read a web2py's applications response (processing 
the necessary forms) and use it on another random controller, we wouldnt 
need mechanize, please let me know how, that would be even cleaner.
The problem is we have to bypass the admin login form.

something like:

on myapp1/index controller:
def index():
  res = read_response("myapp2/anycontroller", args=(,), kwargs={"password": 
"1234")
  return res

Thanks!

-- 
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/d/optout.

Reply via email to