Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Initial Version and Version 1 of UsingModelsOutsideTurboGears

04/22/06 10:53:48 (13 years ago)

Added recipe for using models outside apps


  • UsingModelsOutsideTurboGears

    v1 v1  
     1Sometimes you want to use classes in your data model outside your TurboGears application.  
     3For example, you might want to write a script to: 
     4   * Initialize your database 
     5   * Perform scheduled maintenance tasks 
     6   * Insert or update data based on external events, like an email responder. 
     8If you just try to import your model.py, you may get a traceback message that ends with this error: 
     11KeyError: 'No database configuration found!' 
     14The problem is that you haven't told SQLObject where to find your database; this information is in your application's configuration scripts. 
     16To use your database from outside the application, create a script that looks like this: 
     20#!/usr/bin/env python2.4 
     21# If you're not running on a Unix-like platform, you can omit the first "#!" line. 
     23# Hook up SQLObject to your database configuration from either "dev.cfg" or "prod.cfg" 
     25# This assumes that the script lives in your top-level application directory; if 
     26# that's not the case, you will have to provide a full path to your configuration  
     27# and set your PYTHONPATH so that the model import succeeds. 
     28import turbogears 
     31# Now you can import your model.  
     32from myapp.model import * 
     34# And now you have full access to your model 
     35s = SomeClass.select() 
     37# If you make any changes, you need to manually commit them before exiting. 
     41Note that if you're using SQLite for your database, a long-running script outside your application can lock the database for long periods of time. To avoid this, isolate your database access from your expensive operations: 
     45#!/usr/bin/env python2.4 
     47# Script to periodically do expensive things. 
     49import turbogears 
     51from myapp.model import * 
     53def read_from_db(): 
     54    calculations = ScheduledCalculation.select(ScheduledCalculation.q.result==None) 
     55    return [(calc.id, calc.what_to_do) for calc in calculations] 
     57def write_to_db(results): 
     58    for calc_id, result in results: 
     59        calc = ScheduledCalculation.get(calc_id) 
     60        calc.result = result 
     63# Get the list of what to do from the database.  The result list we get should 
     64# not include any SQLObject-derived objects. 
     65calculations = read_from_db() 
     67# Now we can do the expensive calculations, since there won't be any  
     68# live objects locking the database. 
     69results = [] 
     70for calc_id, what_to_do in calculations: 
     71  results.append((calc_id, do_expensive_calc(what_to_do)) 
     73# Now write them all to the database