Re: [web2py] Init Script in Web2Py

209 views
Skip to first unread message

Richard Vézina

unread,
Aug 10, 2012, 5:57:45 PM8/10/12
to web...@googlegroups.com
if db(db.state.id>0).count() == 0:
    db.state.truncate()
    db.city.truncate()
    db.zipcode.truncate()
    db.areacode.truncate()
    db.state.insert(name='Texas')
    db.state.insert(name='Illinois')
    db.state.insert(name='California')

    db.city.insert(name='Austin',state=1)
    db.city.insert(name='Dallas',state=1)
    db.city.insert(name='Chicago',state=2)
    db.city.insert(name='Aurora',state=2)
    db.city.insert(name='Los Angeles',state=3)
    db.city.insert(name='San Diego',state=3)

    db.zipcode.insert(value='78704',city=1)
    db.zipcode.insert(value='78745',city=1)
    db.zipcode.insert(value='75001',city=2)
    db.zipcode.insert(value='75038',city=2)
    db.zipcode.insert(value='60606',city=3)
    db.zipcode.insert(value='60607',city=3)
    db.zipcode.insert(value='60504',city=4)
    db.zipcode.insert(value='60505',city=4)
    db.zipcode.insert(value='90005',city=5)
    db.zipcode.insert(value='90006',city=5)
    db.zipcode.insert(value='92101',city=6)
    db.zipcode.insert(value='92102',city=6)

    db.areacode.insert(value='512',zipcode=1)
    db.areacode.insert(value='511',zipcode=1)
    db.areacode.insert(value='345',zipcode=2)
    db.areacode.insert(value='456',zipcode=2)
    db.areacode.insert(value='567',zipcode=3)
    db.areacode.insert(value='678',zipcode=3)
    db.areacode.insert(value='789',zipcode=4)
    db.areacode.insert(value='890',zipcode=4)
    db.areacode.insert(value='901',zipcode=5)
    db.areacode.insert(value='321',zipcode=5)
    db.areacode.insert(value='432',zipcode=6)
    db.areacode.insert(value='534',zipcode=6)
    db.areacode.insert(value='645',zipcode=7)
    db.areacode.insert(value='765',zipcode=7)
    db.areacode.insert(value='876',zipcode=8)
    db.areacode.insert(value='987',zipcode=8)
    db.areacode.insert(value='141',zipcode=9)
    db.areacode.insert(value='252',zipcode=9)
    db.areacode.insert(value='363',zipcode=10)
    db.areacode.insert(value='474',zipcode=10)
    db.areacode.insert(value='585',zipcode=11)
    db.areacode.insert(value='686',zipcode=11)
    db.areacode.insert(value='797',zipcode=12)
    db.areacode.insert(value='898',zipcode=12)

You can put something like that in models somewhere.

You just need to adapt for you particular table and the records you want to create and verify the presence of.

Just wrote the proper db request with the proper where attribute :

Let's say you have create user1 in auth_user so you can write this :

if db(db.auth_user.first_name = 'user1').count() != 1:
    db.auth_user.insert(first_name='user1')

Hope it what you were searching for.

Richard

On Fri, Aug 10, 2012 at 5:56 AM, tommasot <tomm...@gmail.com> wrote:
I have a newbie question.

I need to create some record in db on application startup,like for example default user and other things..



Where is the right script/place to implement it



--




Massimo Di Pierro

unread,
Aug 10, 2012, 9:34:52 PM8/10/12
to web...@googlegroups.com
Yes but replace

if db(db.state.id>0).count() == 0:

with

if cache.ram('init',lambda:db(db.state.id>0).count(),None) == 0:

So that is executed once and then cached forever.

Anthony

unread,
Aug 10, 2012, 10:42:09 PM8/10/12
to web...@googlegroups.com
On Friday, August 10, 2012 5:57:45 PM UTC-4, Richard wrote:
if db(db.state.id>0).count() == 0:

FYI, now you can do:

if db(db.mytable).isempty():

Note, db(db.mytable) is equivalent to db(db.mytable.id > 0).

Anthony 

Massimo Di Pierro

unread,
Aug 10, 2012, 11:23:56 PM8/10/12
to web...@googlegroups.com
you stil want to cache that

if cache.ram('init',lambda:db(db.mytable).isempty(),None):

Mandar Vaze

unread,
Aug 13, 2012, 3:04:58 AM8/13/12
to web...@googlegroups.com
You can put something like that in models somewhere.

Putting this in models directory will make it execute every single time.
I suggest you put it in a separate scripts folder (which is what I have done) and execute it when you need it using :

python web2py.py -S <appname> -M -R applications/<appname>/scripts/<scriptname>.py

-Mandar

tommasot

unread,
Sep 5, 2012, 11:04:42 AM9/5/12
to web...@googlegroups.com
Thank you so much

Jay Martin

unread,
Mar 31, 2014, 3:57:00 PM3/31/14
to web...@googlegroups.com
I'm confused as to why this scenario needs caching.

Isn't it true that for an empty user table:

if cache.ram('init',lambda:db(db.auth_user).isempty(),None):
   arbitrary_function
()

will only cache the boolean value True and keep that value (True) in cache forever (until the app is restarted), associated with the given key 'init'?

Therefore, only this initial state is remembered. All future executions of this code will only ever return this initial value (True), therefore the subsequent arbitrary function will continue to be executed every cycle of execution.

Am I missing something? Many thanks!

A36_Marty

unread,
May 4, 2015, 9:56:12 PM5/4/15
to web...@googlegroups.com
Likewise, I too am confused by Massimo's suggestion of using:

if cache.ram('init',lambda:db(db.auth_user).isempty(),None):
   arbitrary_function
()


The True value indicating auth_user is empty is stored in the cache.ram 'init' value, then never updated.   In my test code, 'arbitary_function' gets executed every time a page is loaded (since True for .isempty() is cached), even after records are added to auth_user....

It's only after I stop web2py, restart (and the new value is stored in cache.ram 'init' does the function stop being executed.

How can setting a cache.ram value then setting the expiration of the value to None / never expire every work?

Massimiliano

unread,
May 5, 2015, 4:09:51 AM5/5/15
to web...@googlegroups.com
Maybe you can use:

cache.ram.clear('init')

after executing arbitrary_function()

--
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+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Massimiliano

A36_Marty

unread,
May 12, 2015, 6:52:54 AM5/12/15
to web...@googlegroups.com
I am trying to follow the advice by Massimo and others on this thread but am missing something.

Specifically, the follow code is intended to check if a user group exists, and if not, create it.  Unfortunately it executes every time until the web2py server is restarted and the value is cached.

What do I need to change check/update the cache value correctly to see if a user group exists?

My current code:
### Make sure the general user group exist, if not, create them.
if cache.ram('init_generalusers_group', lambda:db(db.auth_group.role == 'general_users').count() == 0, None):
    print "inside general users init fx"
    db.auth_group.insert(role = 'general_users',
                         description = "A user group for all general users.")
    cache.ram.clear('init_generalusers_group')
    
GENUSER_GROUP_ID = cache.ram("init_generalusers_group", lambda:db(db.auth_group.role == 'general_users').select().first().id, None)


Thanks in advance.
Reply all
Reply to author
Forward
0 new messages