[web2py] Overlapping periods
Hi, I try to make a function that puts periods of time into a database. The function should only add periods that doesn't overlap any of the already registered ones. But the function does not work, and I can't see what's wrong. Any idea? Thanks in advance for help! code from the controller file: def addingperiod(): if request.args and request.args[0]: item_id=request.args[0] prerecords = db().select(db.period.ALL, orderby=db.period.begindate) if not session.timedelta2==timedelta(days=0): for prerecord in prerecords: if prerecord.theauth==auth.user_id: accepted1="true" accepted2="true" if previous.enddate: if item_id.vars.begindate < previousenddate: accepted1="false" if item_id.vars.enddate < prerecord.begindate: accepted2="false" if item_id.vars.enddate < prerecord.enddate: accepted2="false" previousenddate = prerecord.enddate if accepted1=="true": if accepted2=="true": period.append(item_id) redirect(URL(c=request.controller,f='goal'))
Re: [web2py] Overlapping periods
On Fri, 13 Apr 2012 11:38:25 -0700 (PDT), sababa.sab...@gmail.com said: > But the function does not work, and I can't see what's wrong. Any idea? You don't make it very easy for others to help. "The function does not work" - which function doesn't work, and in what way does it not work? What input, what output, and how does the output differ from what is expected? Also, your code has no indentation, so it's hard to read. Help us help you. -- "You can have everything in life you want if you help enough other people get what they want" - Zig Ziglar. Who did you help today?
Re: [web2py] Overlapping periods
On Fri, 13 Apr 2012 11:38:25 -0700 (PDT), sababa.sab...@gmail.com said: > The > function should only add periods that doesn't overlap any of the > already registered ones. Let me try to help (I owe this group a lot of help). I've ignored your code, and I don't know what schema you're using, but here's the pseudo and untested code: def should_this_period_be_inserted(start,end): # Check that all periods that started before this one # also finished before this one started q = ((db.period.begindatestart)) if db(query).count(): return False # Check that no periods started while this one was in progress q = ((db.period.begindate>=start)&(db.period.begindate<=end)) if db(query).count(): return False # If we got this far, the period didn't overlap return True Have I understood what you're trying to do? -- "You can have everything in life you want if you help enough other people get what they want" - Zig Ziglar. Who did you help today?
Re: [web2py] Overlapping periods
On Sat, 14 Apr 2012 09:44:28 +0100, k...@midnighthax.com said: > q = ((db.period.begindatestart)) > if db(query).count(): Argh. Typos. 'q' and 'query' should refer to the same thing, so change the second line to 'if db(q).count():' (that change needs to take place twice). -- "You can have everything in life you want if you help enough other people get what they want" - Zig Ziglar. Who did you help today?
Re: [web2py] Overlapping periods
Thanks for the replies. Pbreit, you pointed the real issue -- what should I call the arguments (addperiodform.vars.id), that are passed from the goal function to the addingperiod function? from the controller file: def goal(): ...some code... addperiodform = SQLFORM(db.period, fields=['begindate','enddate', 'planned'], labels={'begindate':'First day','enddate':'Last day','planned':'Goal'},submit_button='Submit',formstyle='table2cols') if addperiodform.accepts(request.post_vars, session): redirect(URL('default', 'addingperiod', args=addperiodform.vars.id)) ...some code... def addingperiod(): if request.args and request.args[0]: item_id=request.args[0] prerecords = db().select(db.period.ALL, orderby=db.period.begindate) for prerecord in prerecords: if prerecord.theauth==auth.user_id: accepted1="true" accepted2="true" ...some if-statements... if accepted1=="true": if accepted2=="true": db.period.insert(item_id) #this line creates an error redirect(URL(f='goal')) On Saturday, April 14, 2012 10:44:28 AM UTC+2, backseat wrote: > > On Fri, 13 Apr 2012 11:38:25 -0700 (PDT), sababa.sab...@gmail.com said: > > > The > > function should only add periods that doesn't overlap any of the > > already registered ones. > > Let me try to help (I owe this group a lot of help). I've ignored your > code, and I don't know what schema you're using, but here's the pseudo > and untested code: > > def should_this_period_be_inserted(start,end): > # Check that all periods that started before this one > # also finished before this one started > q = ((db.period.begindatestart)) > if db(query).count(): > return False > # Check that no periods started while this one was in progress > q = ((db.period.begindate>=start)&(db.period.begindate<=end)) > if db(query).count(): > return False > # If we got this far, the period didn't overlap > return True > > Have I understood what you're trying to do? > -- > "You can have everything in life you want if you help enough other people > get what they want" - Zig Ziglar. > > Who did you help today? > >