Yes, it works.

Every controller has a trash function because every index table has a
trash link.  Similar to the way grid has a delete button on its index
tables.

It is like this:

def index():
  query = db.sometable.is_active==True
  if such_and_such:
    query = query & db.sometable.somefield== foo
...

So I don't want to copy/paste this generic function in every
controller.

I guess I could do a redirect and then redirect back, but I need to
understand modules better so that's the approach I tried.

On Mar 8, 1:08 pm, Richard Vézina <[email protected]> wrote:
> If you bring your module trashit function into controller does it work?
>
> Make what you try to do (I mean define a "page" controller function into
> module) and what the utility to put the trash function into module? Modules
> are useful for function you want to use every where (any controller files)
> without having to copy/paste them. But I don't see the utility for page
> controller function... Why not just make a controller file for you trash
> function and redirect from one an other controller when you need this
> function???
>
> Richard
>
>
>
>
>
>
>
> On Thu, Mar 8, 2012 at 12:52 PM, Cliff <[email protected]> wrote:
> > Here is the controller
>
> > ### Controller:
> > def trash():
> >    import trash_it
> >    return trash_it.trashit(request, session,response, db)
>
> > On Mar 8, 12:06 pm, Richard Vézina <[email protected]>
> > wrote:
> > > I didn't see rapidly other problem in the code...
>
> > > The only thing I could suspect is if you use directly (I mean call the
> > > module trashit function from the URL) trashit...
>
> > > Since it is a parameted function if won't be allowed to execute it from
> > the
> > > URL.
>
> > > We don't see your controller...
>
> > > Also, maybe try without sub-func  "crumbs.do_crumbs(session, request,
> > db)"
> > > first and then add it...
>
> > > Richard
>
> > > On Thu, Mar 8, 2012 at 11:45 AM, Cliff <[email protected]> wrote:
> > > > Richard,
>
> > > > Thanks for the hints.
>
> > > > You can pass the response object to a module directly, without using
> > > > current.  I have other modules that work this way; the only problem is
> > > > the long parms list.
>
> > > > I know the html imports are working correctly because the form
> > > > renders.
> > > > I suspect the http imports are working because no exception gets
> > > > raised and the the traceback points to gluon/http.py.  So it must be
> > > > imported.
>
> > > > On Mar 8, 11:21 am, Richard Vézina <[email protected]>
> > > > wrote:
> > > > > You may check if those import are working properly :
>
> > > > > from html import FORM, H2, P, BR, INPUT, URL
> > > > > from http import HTTP, redirect
>
> > > > > Could it be possible that it should be import like this :
>
> > > > > from gluon.html import FORM??
>
> > > > > Richard
>
> > > > > On Thu, Mar 8, 2012 at 11:09 AM, Richard Vézina <
> > > > [email protected]
>
> > > > > > wrote:
> > > > > > I think you will need to read a bit about current,
> > current.response,
> > > > > > current.request, etc.
>
> > > > > > Richard
>
> > > > > > On Thu, Mar 8, 2012 at 11:04 AM, Cliff <[email protected]> wrote:
>
> > > > > >> What am I doing wrong?
>
> > > > > >> This code works fine in a controller.  When moved to a module it
> > fails
> > > > > >> with a 303 error.
> > > > > >> Here is the code:
>
> > > > > >> ### Controller:
> > > > > >> def trash():
> > > > > >>    import trash_it
> > > > > >>    return trash_it.trashit(request, session,response, db)
>
> > > > > >> ## module trash_it.py
> > > > > >> from html import FORM, H2, P, BR, INPUT, URL
> > > > > >> from http import HTTP, redirect
>
> > > > > >> def trashit(request, session, response, db):
> > > > > >>    import crumbs
> > > > > >>    crumb_trail = crumbs.do_crumbs(session, request, db)
> > > > > >>    form = FORM(
> > > > > >>        H2('Are you sure?'),
> > > > > >>        P('Do you really want to move the item to the trash?'),
> > > > > >>        BR(),
> > > > > >>        INPUT(_type='submit', _name='yes', _value='Yes, trash it',
> > > > > >>              _style='margin-right:1em'
> > > > > >>             ),
> > > > > >>        INPUT(_type='submit', _name='no', _value='No'),
> > > > > >>    )
> > > > > >>    response.view = 'are_you_sure.html'
>
> > > > > >>    if form.accepts(request, session):
> > > > > >>        if 'yes' in request.post_vars:
> > > > > >>            db[request.controller][request.args(0)] =
> > > > > >> dict(is_active=False)
> > > > > >>            session.flash = 'Item moved to trash.'
> > > > > >>            redirect(URL(request.application, request.controller,
> > > > > >> 'index'))
> > > > > >>        elif 'no' in request.post_vars:
> > > > > >>            session.flash = 'Trash cancelled.'
> > > > > >>            redirect(URL(request.application, request.controller,
> > > > > >> 'index'))
> > > > > >>    else:
> > > > > >>        response.flash = 'Just checking.'
>
> > > > > >>    return dict( form=form, crumb_trail=crumb_trail)
>
> > > > > >> # The traceback
>
> > > > > >> Traceback (most recent call last):
> > > > > >>  File "/media/Myfiles/3-o/gluon/main.py", line 493, in wsgibase
> > > > > >>    serve_controller(request, response, session)
> > > > > >>  File "/media/Myfiles/3-o/gluon/main.py", line 202, in
> > > > > >> serve_controller
> > > > > >>    page = run_controller_in(request.controller, request.function,
> > > > > >> environment)
> > > > > >>  File "/media/Myfiles/3-o/gluon/compileapp.py", line 537, in
> > > > > >> run_controller_in
> > > > > >>    restricted(code, environment, filename)
> > > > > >>  File "/media/Myfiles/3-o/gluon/restricted.py", line 204, in
> > > > > >> restricted
> > > > > >>    exec ccode in environment
> > > > > >>  File "/media/Myfiles/3-o/applications/operations/controllers/
> > > > > >> buckslips.py", line 344, in <module>
> > > > > >>  File "/media/Myfiles/3-o/gluon/globals.py", line 172, in <lambda>
> > > > > >>    self._caller = lambda f: f()
> > > > > >>  File "/media/Myfiles/3-o/applications/operations/controllers/
> > > > > >> buckslips.py", line 341, in trash
> > > > > >>    return trash_it.trashit(request, session,response, db)
> > > > > >>  File "applications/operations/modules/trash_it.py", line 22, in
> > > > > >> trashit
> > > > > >>    redirect(URL(request.application, request.controller, 'index'),
> > > > > >>  File "/media/Myfiles/3-o/gluon/http.py", line 128, in redirect
> > > > > >>    Location=location)
> > > > > >> HTTP: 303 SEE OTHER

Reply via email to