647c0ea1bce7

web: store data on the app object instead of globally
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 13 Jun 2010 10:31:53 -0400
parents 7551a383f66a
children 993a35243d1c
branches/tags (none)
files review/web_ui.py

Changes

--- a/review/web_ui.py	Sun Jun 13 04:43:25 2010 -0400
+++ b/review/web_ui.py	Sun Jun 13 10:31:53 2010 -0400
@@ -59,19 +59,16 @@
     'str': str,
 }
 
-datastore = None
-site_read_only = False
-
 def _render(template, **kwargs):
-    return render_template(template, read_only=site_read_only, utils=utils,
-        datastore=datastore, **kwargs)
+    return render_template(template, read_only=app.read_only, utils=utils,
+        datastore=app.datastore, **kwargs)
 
 
 @app.route('/')
 def index():
-    rev_max = datastore.target['tip'].rev()
+    rev_max = app.datastore.target['tip'].rev()
     rev_min = rev_max - LOG_PAGE_LEN if rev_max >= LOG_PAGE_LEN else 0
-    rcsets = [datastore[r] for r in xrange(rev_max, rev_min, -1)]
+    rcsets = [app.datastore[r] for r in xrange(rev_max, rev_min, -1)]
     return _render('index.html', title='', rcsets=rcsets)
 
 
@@ -85,7 +82,7 @@
         signoff = ''
 
     body = request.form.get('new-signoff-body', '')
-    rcset = datastore[revhash]
+    rcset = app.datastore[revhash]
     rcset.add_signoff(body, signoff, force=True)
 
     return redirect("/changeset/%s/" % revhash)
@@ -98,21 +95,21 @@
     body = request.form['new-comment-body']
     
     if body:
-        rcset = datastore[revhash]
+        rcset = app.datastore[revhash]
         rcset.add_comment(body, filename, lines)
     
     return redirect("/changeset/%s/" % revhash)
 
 @app.route('/changeset/<revhash>/', methods=['GET', 'POST'])
 def changeset(revhash):
-    if request.method == 'POST' and not site_read_only:
+    if request.method == 'POST' and not app.read_only:
         signoff = request.form.get('signoff', None)
         if signoff:
             return _handle_signoff(revhash)
         else:
             return _handle_comment(revhash)
     
-    rcset = datastore[revhash]
+    rcset = app.datastore[revhash]
     rev = rcset.target[revhash]
     
     cu_signoffs = rcset.signoffs_for_current_user()
@@ -126,16 +123,16 @@
 
 @app.route('/pull/', methods=['POST'])
 def pull():
-    if not site_read_only:
+    if not app.read_only:
         path = request.form['path']
-        commands.pull(datastore.repo.ui, datastore.repo, path, update=True)
+        commands.pull(app.datastore.repo.ui, app.datastore.repo, path, update=True)
     return redirect('/')
 
 @app.route('/push/', methods=['POST'])
 def push():
-    if not site_read_only:
+    if not app.read_only:
         path = request.form['path']
-        commands.push(datastore.repo.ui, datastore.repo, path)
+        commands.push(app.datastore.repo.ui, app.datastore.repo, path)
     return redirect('/')
 
 
@@ -145,11 +142,10 @@
         import webbrowser
         webbrowser.open('http://localhost:%d/' % port)
         
-    global datastore, site_read_only
-    datastore = api.ReviewDatastore(ui, repo)
-    site_read_only = read_only
+    app.datastore = api.ReviewDatastore(ui, repo)
+    app.read_only = read_only
+    app.debug = ui.debugflag
 
-    app.debug = ui.debugflag
     if app.debug:
         from flaskext.lesscss import lesscss
         lesscss(app)