Reserve an Item between start_date and end_date

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Reserve an Item between start_date and end_date

Post by maboroshi »

I am trying to figure out how to reserve an item between a start date and an end date. So if the item is reserved on those days it can not be taken out for those days.

here is a function I found while using Google to loop through a start and end date

Code: Select all

import datetime

def daterange( start_date, end_date ):
    if start_date <= end_date:
        for n in range( ( end_date - start_date ).days + 1 ):
            yield start_date + datetime.timedelta( n )
    else:
        for n in range( ( start_date - end_date ).days + 1 ):
            yield start_date - datetime.timedelta( n )
and then here is my code

Code: Select all

@auth.requires(auth.has_membership('second_year') or auth.has_membership('admin'))
def second_year_checkout():
    check_id = request.args(0)
    check = db(db.product.id == check_id).select()
    project_date = db(db.checkout.id>0).select()
    for valid in check:
        db.checkout.product.default=valid.id
        form = SQLFORM(db.checkout)
        if form.process().accepted:
            for project in project_date:
                start = project.date_in
                end = project.date_out
                for date in daterange(start, end):
                    if date:                    
                        session.flash = 'form accepted'
                        return dict(redirect(URL('default', 'next')))
                    else:
                        return dict(redirect(URL('error','availability')))
        elif form.errors:
            response.flash = 'form has errors'
        else:
            response.flash = 'please fill the form' 
            return dict(form=form)
The logic is beyond me atm haha

so any ideas? *cheers

Mabo

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Reserve an Item between start_date and end_date

Post by ayu »

Made a quick test in the interpreter, try this

Code: Select all

cats@Enma:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> start = datetime.date(2012,10,4)
>>> end = datetime.date(2012,10,10)
>>> start <= datetime.date(2012,10,5) <= end
True
>>> start <= datetime.date(2012,10,11) <= end
False
>>>
"The best place to hide a tree, is in a forest"

User avatar
CommonStray
Forum Assassin
Forum Assassin
Posts: 1215
Joined: 20 Aug 2005, 16:00
18

Re: Reserve an Item between start_date and end_date

Post by CommonStray »

to prevent reservation:

determine if the currentTime is greater than or equal to the startTime && the currentTime is less than or equal to the endTime

if both conditions are met then your within range.

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: Reserve an Item between start_date and end_date

Post by maboroshi »

Hey yea cats gave me some code to work with :-)

Code: Select all

                start = project.date_in
                end = project.date_out
                user_start = request.vars.checkout_date_in
                user_end = request.vars.checkout_date_out   
                if start <= user_start <= end or start <= user_end <= end:   
                    return dict(redirect(URL('error','availability')))
                else:
                    session.flash = 'form accepted'
                    return dict(redirect(URL('default', 'next')))
this seems right

*cheers Cats and CommonStray

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: Reserve an Item between start_date and end_date

Post by maboroshi »

The final code was this if anyone is interested

Code: Select all

def check_database_entry(form):
    project_date = db(db.checkout.id>0).select()
    for project in project_date:
        start = project.date_in
        end = project.date_out
        user_start = datetime.datetime.strptime(form.vars.date_in, '%Y-%m-%d').date()
        user_end = datetime.datetime.strptime(form.vars.date_out, '%Y-%m-%d').date()  
        if start <= user_start <= end or start <= user_end <= end:   
            session.flash = 'Those dates have been reserved for that item, please try another'
            redirect(URL('error', 'availability'))   
and

Code: Select all

@auth.requires(auth.has_membership('second_year') or auth.has_membership('admin'))
def second_year_checkout():
    check_id = request.args(0)
    check = db(db.product.id == check_id).select()
    for valid in check:
        db.checkout.product.default=valid.id
        form = SQLFORM(db.checkout)           
        if form.process(onvalidation=check_database_entry).accepted:    
            session.flash = 'Time Reserved'
            redirect(URL('default', 'next'))              
        elif form.errors:
            response.flash = 'form has errors'
        else:
            response.flash = 'please fill the form' 
            return dict(form=form)

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Reserve an Item between start_date and end_date

Post by ayu »

awesome, gj! :D
"The best place to hide a tree, is in a forest"

Post Reply