automated application initialization... Automating application setup tasks is something we should probably do more of in keeping with the Web2py "zero configuration" spirit. Seeing your discussions here inspired me in my quest towards an eventual config and/or application setup logging system of sorts.
This is a simple way of tracking completed initialization tasks, like setting up new groups and users. It currently uses Joe's code and does not hit the database (although it does incorporate global variables). It might be interesting to create install logging as well. Please review for sanity and improvements. Eventually we might want to look into using something like this: http://www.voidspace.org.uk/python/configobj.html one of the config parsers mentioned in this document http://wiki.python.org/moin/ConfigParserShootout but in the mean time the following code seems to get the job done with causing too many issue. it goes in the models directory, I called mine initialization.py. It creates and appends to 000_uc_initialization.py In this example I have appended a couple of global variables. comments? # coding: utf8 # initialization.py import os initialization_file_path = os.path.join(request.folder,'models','000_uc_initialization.py') # create an initialization file if we do not have one if not 'initialization_file_created' in globals(): if not os.path.exists(initialization_file_path): ''' if keyfile is found, we create an empty one. ''' our_file = open(initialization_file_path,"w") our_text = 'initialization_file_created = True' our_file.writelines(our_text) our_file.close() else: print('if a tree falls in the forest does anybody hear it?') if not 'my_group_created' in globals(): if not db(db.auth_group).count(): db.auth_group.insert(role='root',description='Exactly what you think it does') db.auth_group.insert(role='super',description='Supervisor access for team manager') db.auth_group.insert(role='user',description='Normal user') our_file = open(initialization_file_path,"a") our_text = 'my_group_created = True' our_file.write(our_text) our_file.close() else: print('if a tree falls in the forest does anybody hear it?') On Apr 1, 10:26 pm, Anthony <abasta...@gmail.com> wrote: > On Friday, April 1, 2011 2:22:55 PM UTC-4, Mike wrote: > > > Nevermind. I thought there was a builtin web2py way for this but I > > just did this instead (after sifting more through the groups): > > > def groupadd(check_group): > > if not db(db.auth_group.role==check_group).count(): > > db.auth_group.insert(role=check_group) > > > groupadd('Admins') > > groupadd('Maintenance Users') > > groupadd('Download Users') > > If that's in a model file, it seems like you're doing an unnecessary > database hit on every single request. If you just need to create those three > groups one time, why not just do so outside the application code? Or maybe > create an action in a controller and just call it once.