On Friday, November 25, 2016 at 5:49:08 AM UTC-8, dirman wrote:
>
>
> I have this newspaper subscription table;
>
> db.define_table('business_and_financial_times',
>     Field('subscription', 'boolean', default=False),
>     Field('creation_date', 'date', default=now),
>     
> Field('subscription_option',requires=IS_IN_SET((['none','monthly','yearly']))),
>     Field('days','integer',requires=IS_IN_SET(([0, 30, 365])), default=0),
>     Field('subscription_date', 'date', default=now),
>     
> Field('payment_option',requires=IS_IN_SET((['...','...','...','....'])),default='None'),
>     Field('payment_token'),
>     Field('payment_process_time','time'),
>     Field('subscriber', db.auth_user, default=auth.user_id))
>
> i want to count down the days after a user subscribes to a newspaper and 
> update the days left depending on 
> the subscription option.
>
> Field('days','integer',requires=IS_IN_SET(([0, 30, 365])), default=0),
>
> I need help
>
> thanks
>
>
Something along the lines of

import datetime 


row = db(business_and_financial_times.id == someid).select()
timesincesub = request.now - row.subscription_date
if timesincesub.days > row.days:
    row.update_record(days = 0)
else:
    if row.subsciption_option == "monthly":
       row.update_record(days = 30 - timesincesub.days)
    if row.subsciption_option == "yearly"
       row.update_record(days = 365 - timesincesub.days)


but it might be better to have an "expires" field with the date the 
subscription ends, and make days a computed field.

Then when you process a yearly subscription,

db.business_and_financial_times.insert([stuf], expires = request.now + 
timedelta(days=365), subsription_date = request.now, [stuff])



Hmmm, either way requires handling leap years and different length months, 
but that's not a logic change.

/dps


-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to