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 Version 5 and Version 6 of UsingModelsOutsideTurboGears


Ignore:
Timestamp:
06/29/07 11:25:18 (12 years ago)
Author:
Chris Arndt
Comment:

migration notice

Legend:

Unmodified
Added
Removed
Modified
  • UsingModelsOutsideTurboGears

    v5 v6  
    1 Sometimes you want to use classes in your data model outside your TurboGears application.  
    2  
    3 For 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  
    8 If you just try to import your model.py, you may get a traceback message that ends with this error: 
    9  
    101{{{ 
    11 KeyError: 'No database configuration found!' 
     2#!rst 
     3.. note:: This page has been migrated to http://docs.turbogears.org/1.0/ModelOutsideTG. 
    124}}} 
    13  
    14 The problem is that you haven't told SQLObject where to find your database; this information is in your application's configuration scripts. 
    15  
    16 To 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. 
    28 # The value for modulename can be pulled right from start-project_name.py  
    29 # script in the top-level application directory. 
    30 import turbogears 
    31 turbogears.update_config(configfile="dev.cfg", modulename="project_name.config") 
    32  
    33 # Note: turbogears 0.8.9 doesn't have .update_config() 
    34  
    35 # Now you can import your model.  
    36 from myapp.model import * 
    37  
    38 # And now you have full access to your model 
    39 s = SomeClass.select() 
    40  
    41 # If you make any changes, you need to manually commit them before exiting. 
    42 turbogears.hub.commit() 
    43 }}} 
    44  
    45 Note 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: 
    46  
    47 {{{ 
    48 #!python 
    49 #!/usr/bin/env python2.4 
    50  
    51 # Script to periodically do expensive things. 
    52  
    53 import turbogears 
    54 turbogears.update_config(configfile="dev.cfg", modulename="project_name.config") 
    55 from myapp.model import * 
    56  
    57 def read_from_db(): 
    58     calculations = ScheduledCalculation.select(ScheduledCalculation.q.result==None) 
    59     return [(calc.id, calc.what_to_do) for calc in calculations] 
    60  
    61 def write_to_db(results): 
    62     for calc_id, result in results: 
    63         calc = ScheduledCalculation.get(calc_id) 
    64         calc.result = result 
    65  
    66  
    67 # Get the list of what to do from the database.  The result list we get should 
    68 # not include any SQLObject-derived objects. 
    69 calculations = read_from_db() 
    70  
    71 # Now we can do the expensive calculations, since there won't be any  
    72 # live objects locking the database. 
    73 results = [] 
    74 for calc_id, what_to_do in calculations: 
    75   results.append((calc_id, do_expensive_calc(what_to_do)) 
    76  
    77 # Now write them all to the database 
    78 write_to_db(results) 
    79 turbogears.hub.commit() 
    80  
    81 }}}