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 7 and Version 8 of ExtendingVisitFramework


Ignore:
Timestamp:
06/24/07 18:35:03 (12 years ago)
Author:
Chris Arndt
Comment:

migration notice

Legend:

Unmodified
Added
Removed
Modified
  • ExtendingVisitFramework

    v7 v8  
    1 = Extending the Visit Framework (Turbogears v0.9prerelease) = 
    2  
    3 A thread on the Turbogears group mentioned adding the ability to track IP addresses to the visit framework.  This feature is pretty easy to implement as a plugin to the visit framework as it exists right now.  Steps to doing so are as follows (assuming you already have visit enabled): 
    4  
    5 == Step One - Create your model == 
    6  
    7 I added the following in my main model.py: 
    8  
    91{{{ 
    10 class VisitIP(SQLObject): 
    11      
    12     class sqlmeta: 
    13         table = 'visit_ip' # SQL object default naming is visitor_i_p, not pretty 
    14      
    15     visit = ForeignKey('TG_Visit') 
    16     ip_address = StringCol(length=20) 
     2#!rst 
     3.. note:: This page has been migrated to http://docs.turbogears.org/1.0/ExtendingVisitFramework. 
    174}}} 
    18  
    19 Create the table in your database with 'tg-admin sql create --class=VisitIP' 
    20  
    21 == Step Two - Add Plugin Logic == 
    22  
    23 I made a new module in my project named 'visit_plugin.py'.  Its contents are as follows: 
    24  
    25 {{{ 
    26 import cherrypy 
    27 from turbogears import visit 
    28 from model import VisitIP 
    29  
    30 def ip_tracking_is_on(): 
    31     "Returns True if ip tracking is properly enabled, False otherwise." 
    32     return cherrypy.config.get('visit.on', False) and cherrypy.config.get('visit.ip_tracking.on', False) 
    33  
    34 #Interface for the TurboGears extension 
    35 def start_extension(): 
    36     if not ip_tracking_is_on(): 
    37         return 
    38     cherrypy.log( "Visit ip tracker starting" ) 
    39  
    40     #Register the plugin for the Visit Tracking framework 
    41     visit.enable_visit_plugin( IPVisitPlugin() ) 
    42      
    43 def shutdown_extension(): 
    44     if not ip_tracking_is_on(): 
    45         return 
    46     cherrypy.log( "Visit ip tracker shutting down" ) 
    47      
    48 class IPVisitPlugin(object): 
    49      
    50     def __init__(self): 
    51         cherrypy.log("IPVisitPlugin extension starting") 
    52      
    53     def record_request(self, visit_id): 
    54         # This method gets called on every single visit, so if you  
    55         # want to record something every time they make a request, this 
    56         # is the place to do it. 
    57         pass 
    58          
    59     def new_visit(self, visit_id): 
    60         # This method gets called the first time the visit is started. 
    61         # I think IP tracking makes sense in here. 
    62         v = visit.TG_Visit.get(visit_id) 
    63          
    64         # add a new visit ip object to the database 
    65         VisitIP(visit=v, ip_address=cherrypy.request.remoteAddr) 
    66          
    67 }}} 
    68  
    69 The start_extension and shutdown_extension functions are called by turbogears when starting up and shutting down (imagine that).  The key is this process is the visit.enable_visit_plugin call, which registers your plugin with the visit framework. 
    70  
    71  
    72 == Step Three: Tie in the extension in setup.py == 
    73  
    74 In your project's setup.py, add an 'entry_points' parameter to the setup() function:  
    75 {{{ 
    76 setup( 
    77     # Lots of stuff snipped 
    78     test_suite = 'nose.collector' 
    79     # begin new 
    80     , entry_points=""" 
    81     [turbogears.extensions] 
    82     my_visit_extension = ip_plugin.visit_plugin 
    83     """ 
    84     # end new 
    85     ) 
    86 }}} 
    87  
    88 My project name is ip_plugin, so you will need to edit this a bit make this point to the right module in your project.  (Aside: I'm not sure that [turbogears.extensions] is the right place to put this entry point.  If there is a better area to place this in, please edit as appropriate.  But, the above does work). 
    89  
    90 == Step Four: Update Config File == 
    91  
    92 Add a configuration variable so that the tracking can be turned on and off.  Somewhere in config.py, add the line 
    93  
    94 {{{ 
    95 visit.ip_tracking.on=True 
    96 }}} 
    97  
    98 == Step Five: Update egg info == 
    99  
    100 This step just re-generates the egg information for your project so that the extension actually gets called at runtime.  From the command line, at the root level of your project run 'python setup.py egg_info'. 
    101  
    102 == Conclusion == 
    103  
    104 That's it.  Fire up your project and you should be tracking ip activity just like the NSA.   
    105  
    106 It may be possible to register the plugin (via visit.enable_visit_plugin()) some other way (perhaps in setup.py?).  If you believe there is a more canonical way that things like this should be done, please go ahead and make the edit.