Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implemented new database model.

  • Loading branch information...
commit 0dcf8e77ba74a72a4f0325c515de4897db1627e3 1 parent 0d6d73b
@williamstein authored
View
1  index.yaml
@@ -0,0 +1 @@
+
View
126 simplesage.py
@@ -10,25 +10,9 @@
import json
import urllib2
-##############################
-# database
-##############################
-
-class WorkRequest(db.Model):
- id = db.IntegerProperty()
- input = db.StringProperty(multiline=True)
- output = db.StringProperty(multiline=True)
- date = db.DateTimeProperty(auto_now_add=True)
-
-key = db.Key.from_path('work', 'request')
-
-def next_id():
- for a in db.GqlQuery("SELECT * from WorkRequest ORDER by id DESC"):
- return a.id + 1
- return 0
##############################
-# handling URL's
+# decorators
##############################
# makes it so "g.user" is defined and not None
@@ -44,47 +28,103 @@ def wrapper(*args, **kwds):
return f(*args, **kwds)
return wrapper
-@app.route("/")
-def main_page():
- return render_template('main.html')
+##############################
+# database
+##############################
-@app.route('/input', methods=['POST']) # so g.user is defined in function
-@login_required
-def input_page():
- id = next_id()
- input = cgi.escape(request.form['input'])
+class Cells(db.Model):
+ user_id = db.StringProperty()
+ cell_id = db.IntegerProperty()
+ input = db.StringProperty(multiline=True)
+ output = db.StringProperty(multiline=True)
+ status = db.StringProperty() # 'run', 'done'
- all_work = get_all_work()
- wr = WorkRequest(parent=key, id=id, input=input)
- wr.put()
+class Workers(db.Model):
+ worker_id = db.StringProperty()
- return render_template('input.html', **locals())
+class Sessions(db.Model):
+ worker_id = db.StringProperty()
+ user_id = db.StringProperty()
+ status = db.StringProperty()
-def get_all_work():
- return db.GqlQuery("SELECT * FROM WorkRequest ORDER BY date DESC")
+#TODO: what's this key -- makes no sense to me?
+key = db.Key.from_path('work', 'request')
-@app.route("/database")
-def database():
- return render_template('database.html', all_work = get_all_work())
-@app.route("/submit_work")
-def submit_work():
- return render_template('submit_work.html')
+@login_required
+def next_cell_id():
+ q = Cells.all()
+ q.filter("user_id =", g.user.user_id())
+ q.order("-cell_id")
+ for a in q:
+ return a.cell_id + 1
+ return 0
+
+##############################
+# handling URL's
+##############################
+
+@app.route("/")
+def main_page():
+ return render_template('main.html')
+
+@app.route('/input', methods=['POST'])
+@login_required
+def input_page():
+ cell = Cells(user_id = g.user.user_id(),
+ cell_id = next_cell_id(),
+ input = cgi.escape(request.form['input']))
+ cell.put()
+ return redirect('/db/cells')
+
+@login_required
+def get_all_cells():
+ q = Cells.all()
+ q.filter("user_id =", g.user.user_id())
+ q.order('-cell_id')
+ return q
+
+@app.route('/db/cells')
+def db_cells():
+ all_cells = get_all_cells()
+ return render_template('db_cells.html', **locals())
+
+@app.route('/db/workers')
+def db_workers():
+ all_workers = Workers.all()
+ return render_template('db_workers.html', **locals())
+
+@app.route('/db/sessions')
+def db_sessions():
+ all_sessions = Sessions.all()
+ return render_template('db_sessions.html', **locals())
@app.route("/work")
def work():
- all_work = db.GqlQuery("SELECT * FROM WorkRequest")
- # TODO: should only query for things with output none!
- return json.dumps([{'id':a.id, 'input':a.input} for a in all_work if a.output is None])
+ q = Cells.all()
+ q.filter('status !=', 'done')
+ w = [{'cell_id':a.cell_id, 'user_id':a.user_id, 'input':a.input} for a in q]
+ return json.dumps(w)
+@app.route("/submit_work")
+def submit_work():
+ user = users.get_current_user()
+ if user is not None:
+ user_id = user.user_id()
+ return render_template('submit_work.html', **locals())
@app.route('/receive_work', methods=['POST'])
def receive_work():
output = cgi.escape(request.form['output'])
- id = int(cgi.escape(request.form['id']))
+ cell_id = int(cgi.escape(request.form['cell_id']))
+ user_id = cgi.escape(request.form['user_id'])
- for a in db.GqlQuery("SELECT * FROM WorkRequest WHERE id=%s"%id):
+ q = Cells.all()
+ q.filter('cell_id =', cell_id)
+ q.filter('user_id =', user_id)
+ for a in q:
a.output = output
+ a.status = 'done'
a.put()
- return render_template('receive_work.html', **locals())
+ return 'success'
View
14 templates/base.html
@@ -2,10 +2,16 @@
<body>
<br>
{% block navbar %}
-Flask Simple Sage Notebook Demo<br><br>
-<a href="/">submit</a>&nbsp;&nbsp;&nbsp;
-<a href="/database">database</a>&nbsp;&nbsp;&nbsp;
-<a href="/submit_work">submit work</a>
+Flask Simple Sage Notebook Demo
+<hr>
+<a href="/">submit input</a>&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="/submit_work">submit output</a>&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="/db/cells">cells</a>&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="/db/workers">workers</a>&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="/db/sessions">sessions</a>&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="/work">work</a>&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="/submit_work">submit_work</a>
+
<br>
<hr>
{% endblock %}
View
12 templates/database_table.html
@@ -1,10 +1,12 @@
<table border=1>
-<tr><th>Date</th><th width=100>id</th><th width=150>input</th><th>output</th></tr>
-{% for a in all_work %}
+<tr><th>user_id</th><th>cell_id</th><th>status</th><th>input</th><th>output</th></tr>
+{% for a in all_cells %}
<tr bgcolor="{% if a.output is defined %}yellow{% else %}#eee{% endif %}">
- <td>{{ a.date.ctime() }} </td>
- <td> {{ a.id }} </td>
+ <td>{{ a.user_id }} </td>
+ <td> {{ a.cell_id }} </td>
+ <td>{{ a.status }}</td>
<td><pre>{{ a.input }}</pre></td>
- <td><pre> {{ a.output }} </pre></td></tr>
+ <td><pre> {{ a.output }} </pre></td>
+</tr>
{% endfor %}
</table>
View
18 templates/db_cells.html
@@ -0,0 +1,18 @@
+{% extends 'base.html' %}
+
+{% block content %}
+
+<table border=1>
+<tr><th>user_id</th><th>cell_id</th><th>status</th><th>input</th><th>output</th></tr>
+{% for a in all_cells %}
+<tr bgcolor="{% if a.output is defined %}#eee{% else %}#bbb{% endif %}">
+ <td>{{ a.user_id }} </td>
+ <td> {{ a.cell_id }} </td>
+ <td>{{ a.status }}</td>
+ <td><pre>{{ a.input }}</pre></td>
+ <td><pre> {{ a.output }} </pre></td>
+</tr>
+{% endfor %}
+</table>
+
+{% endblock %}
View
16 templates/db_sessions.html
@@ -0,0 +1,16 @@
+{% extends 'base.html' %}
+
+{% block content %}
+
+<table border=1>
+<tr><th>worker_id</th><th>user_id</th><th>status</th></tr>
+{% for a in all_sessions %}
+<tr >
+ <td>{{ a.worker_id }} </td>
+ <td> {{ a.user_id }} </td>
+ <td>{{ a.status }}</td>
+</tr>
+{% endfor %}
+</table>
+
+{% endblock %}
View
14 templates/db_workers.html
@@ -0,0 +1,14 @@
+{% extends 'base.html' %}
+
+{% block content %}
+
+<table border=1>
+<tr><th>worker_id</th></tr>
+{% for a in all_workers %}
+<tr>
+ <td>{{ a.worker_id }} </td>
+</tr>
+{% endfor %}
+</table>
+
+{% endblock %}
View
3  templates/submit_work.html
@@ -1,7 +1,8 @@
{% extends 'base.html' %}
{% block content %}
<form action="/receive_work" method="post">
- <div>id:<br><textarea name="id" rows="1" cols="10"></textarea></div>
+ <div>user_id:<br><textarea name="user_id" rows="1" cols="10">{{ user_id }}</textarea></div>
+ <div>cell_id:<br><textarea name="cell_id" rows="1" cols="10"></textarea></div>
<div>output:<br><textarea name="output" rows="6" cols="70"></textarea></div>
<div><input type="submit" value="Submit Work"></div>
</form>
View
21 todo.txt
@@ -0,0 +1,21 @@
+[x] users (openid)
+
+[ ] change database schema
+
+ cells:
+ - user_id -- string
+ - cell_id -- integer
+ - input -- text
+ - output -- text
+ - status -- 'done' or 'running'
+
+ workers:
+ - worker_id -- string
+
+ sessions:
+ - worker_id -- string
+ - user_id -- string (which user is being served by this session)
+ - status -- string (latest status of this session)
+
+
+
View
11 worker.py
@@ -4,8 +4,8 @@ def get_work(url):
u = urllib2.urlopen('%s/work'%url)
return json.loads(u.read())
-def submit_work(url, id, output):
- data = urllib.urlencode({'id':id, 'output':output})
+def submit_work(url, cell_id, user_id, output):
+ data = urllib.urlencode({'cell_id':cell_id, 'user_id':user_id, 'output':output})
urllib2.urlopen('%s/receive_work'%url, data=data)
def do_work(url='http://localhost:9000'):
@@ -19,9 +19,10 @@ def do_work(url='http://localhost:9000'):
output = sage_eval(w['input'])
except Exception, msg:
output = "Error: '%s'"%msg
- id = w['id']
- print id, output
- submit_work(url, id, output)
+ user_id = w['user_id']
+ cell_id = w['cell_id']
+ print user_id, cell_id, output
+ submit_work(url, cell_id, user_id, output)
def go(url='http://localhost:9000', delay=0.5):
while True:
Please sign in to comment.
Something went wrong with that request. Please try again.