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


Ignore:
Timestamp:
04/22/06 10:53:48 (13 years ago)
Author:
tlesher@…
Comment:

Added recipe for using models outside apps

Legend:

Unmodified
Added
Removed
Modified
  • UsingModelsOutsideTurboGears

    v1 v1  
     1Sometimes you want to use classes in your data model outside your TurboGears application.  
     2 
     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. 
     7 
     8If you just try to import your model.py, you may get a traceback message that ends with this error: 
     9 
     10{{{ 
     11KeyError: 'No database configuration found!' 
     12}}} 
     13 
     14The problem is that you haven't told SQLObject where to find your database; this information is in your application's configuration scripts. 
     15 
     16To use your database from outside the application, create a script that looks like this: 
     17 
     18{{{ 
     19#!python 
     20#!/usr/bin/env python2.4 
     21# If you're not running on a Unix-like platform, you can omit the first "#!" line. 
     22 
     23# Hook up SQLObject to your database configuration from either "dev.cfg" or "prod.cfg" 
     24 
     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 
     29turbogears.update_config("dev.cfg") 
     30 
     31# Now you can import your model.  
     32from myapp.model import * 
     33 
     34# And now you have full access to your model 
     35s = SomeClass.select() 
     36 
     37# If you make any changes, you need to manually commit them before exiting. 
     38turbogears.hub.commit() 
     39}}} 
     40 
     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: 
     42 
     43{{{ 
     44#!python 
     45#!/usr/bin/env python2.4 
     46 
     47# Script to periodically do expensive things. 
     48 
     49import turbogears 
     50turbogears.update_config("dev.cfg") 
     51from myapp.model import * 
     52 
     53def read_from_db(): 
     54    calculations = ScheduledCalculation.select(ScheduledCalculation.q.result==None) 
     55    return [(calc.id, calc.what_to_do) for calc in calculations] 
     56 
     57def write_to_db(results): 
     58    for calc_id, result in results: 
     59        calc = ScheduledCalculation.get(calc_id) 
     60        calc.result = result 
     61 
     62 
     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() 
     66 
     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)) 
     72 
     73# Now write them all to the database 
     74write_to_db(results) 
     75turbogears.hub.commit() 
     76 
     77}}}