On Friday, August 24, 2018 at 9:27:24 AM UTC-7, lbjc...@gmail.com wrote:
>
> We are a team of newbies in web2py trying out something new. If we insert 
> into a database an item, and let modules access the new values it does not 
> work properly as expected.
> The code below:
> code = ''
>     
> form = FORM(INPUT(_name='message'), INPUT(_type='submit'))
>     if request.vars:
>         r = [request.vars.message]
>         db.post.insert(replies=r[0])# successful insert
>     row = db(db.post.author== auth.user.id).select(db.post.id, db.post.
> replies, orderby=~db.post.id, limitby=(0,1)).first()#should get the last 
> row
>     code  = row.replies if row else None
>     return dict(value=code)
>
> This code in the modules folder is part of other functions to access the 
> latest row value for processing:
> def Search_reply():
>   db = current.db
>   auth = Auth(db, hmac_key=Auth.get_or_create_key())
>   auth.define_tables()
>   name  = [r.replies.translate(None, '~!@#$%^&*()_+}{|":?><>-=][\';/.,') 
> for r in db(db.post.author == auth.user.id).select(db.post.ALL)][-1]
>   return name
>   ##name will be used by other functions and a reply returned to view 
>
> Problem
>
>    1. Search_reply does not always gets me the last inserted item.
>    2. I have to refresh the page for the function Search_reply to 
>    retrieve the latest item
>
> Tried options
>
>    1. We tried using a different controller that had Search_replies and 
>    called the function in view, also tried using the function from models 
> with 
>    the same result.
>    2. We tried using db.commit() after db.post.insert() but with the same 
>    results. One thing of note is that after the insert to db.post from 
> web2py, 
>    if running a python function that accesses the database on the python idle 
>    interpreter, the new inserted message from db.post.replies is easily read 
>    without having refreshed the web2py view page. Again, '
>    code.replies if row else None
>    gets the last inserted item BEFORE page refresh.
>
> Is there a way to have modules access the new inserted message within 
> web2py without page refresh?
> Is there a way of dal refresh (not page refresh) after inserting a new 
> item?
> why doesn't db.commit(), on controller work to interrupt the workflow, 
> have a message inserted then read the latest item?
> We are thinking of something like this in the same workflow:
> ..workflow starts with user input
> ..controller functions insert the input to db
> ..controller imported functions read new inserted message for processing
> ..modules process message
> ..module through controller returns reply to view
> ..workflow ends
>
> cc
>
> Dream team
>


I've mentioned Chapter 7 before,
especially 
<URL:http://web2py.com/books/default/chapter/29/07/forms-and-validators#The-process-and-validate-methods>

I would make your controller look like:
 
def my_controller():
    form = FORM(INPUT(_name='message'), INPUT(_type='submit'))
    if form.process().accepted:
      newrow = form.vars.id
      response.flash = "input accepted in row %d" % (newrow)
    elif form.errors:
      response.flash = "input has errors"
    else:
      response.flash = "please fill out the form"
    return dict(form=form, rowid = newrow)

def quicklastpost():
    if request.vars.rowid:
      row = db.post(request.vars.rowid)
    return row



In your view (my_controller.html), add a LOAD() helper.
(see chapter 12)

{{extend 'layout.html'}}

{{form = form}}
{{if rowid in globals():}}
{{loadmsg = 'loading ...."}}
{{=LOAD(c='default', f='quicklastpost.load', target='lastpost', 
content=loadmsg, ajax=True)}}
{{pass}



and views/default/quicklastpost.load:

{{=row}





In your module, rather than recreating the auth tables just to figure out 
the user, why not use current to pass in the user?

def Search_reply():
  db = current.db
  user = current.user_id
  name  = [r.replies.translate(None, '~!@#$%^&*()_+}{|":?><>-=][\';/.,') \
      for r in db(db.post.author == user).select(db.post.ALL)][-1]
  return name
  ##name will be used by other functions and a reply returned to view 


-- 
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