Skip to content

Commit

Permalink
added single file version of todo app just for comparison purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
cguardia committed Mar 30, 2011
1 parent 1156fb1 commit 4ab9a58
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
84 changes: 84 additions & 0 deletions code/transaction/todo_single_file/pickledm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import os
import pickle
import transaction

class PickleDataManager(object):

transaction_manager = transaction.manager

def __init__(self, pickle_path='Data.pkl'):
self.pickle_path = pickle_path
try:
data_file = open(self.pickle_path, 'rb')
except IOError:
data_file = None
uncommitted = {}
if data_file is not None:
try:
uncommitted = pickle.load(data_file)
except EOFError:
pass
self.uncommitted = uncommitted
self.committed = uncommitted.copy()

def __getitem__(self, name):
return self.uncommitted[name]

def __setitem__(self, name, value):
self.uncommitted[name] = value

def __delitem__(self, name):
del self.uncommitted[name]

def keys(self):
return self.uncommitted.keys()

def values(self):
return self.uncommitted.values()

def items(self):
return self.uncommitted.items()

def __repr__(self):
return self.uncommitted.__repr__()

def abort(self, transaction):
self.uncommitted = self.committed.copy()

def tpc_begin(self, transaction):
pass

def commit(self, transaction):
pass

def tpc_vote(self, transaction):
devnull = open(os.devnull, 'wb')
try:
pickle.dump(self.uncommitted, devnull)
except (TypeError, pickle.PicklingError):
raise ValueError("Unpickleable value cannot be saved")

def tpc_finish(self, transaction):
data_file = open(self.pickle_path, 'wb')
pickle.dump(self.uncommitted, data_file)
self.committed = self.uncommitted.copy()

def tpc_abort(self, transaction):
self.uncommitted = self.committed.copy()

def sortKey(self):
return 'pickledm' + str(id(self))

def savepoint(self):
return PickleSavepoint(self)


class PickleSavepoint(object):

def __init__(self, dm):
self.dm = dm
self.saved_committed = self.dm.uncommitted.copy()

def rollback(self):
self.dm.uncommitted = self.saved_committed.copy()

36 changes: 36 additions & 0 deletions code/transaction/todo_single_file/todo.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal">
<head>
<title>Todo list demo for repoze.tm2</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<meta name="keywords" content="python web application" />
<meta name="description" content="pyramid web application" />
</head>
<body>
<h1>Todo List</h1>
<p tal:condition="status"><b>$status</b></p>
<form method="POST">
<table tal:condition="tasks">
<tr tal:repeat="task tasks">
<td>
<input type="checkbox" name="tasks"
tal:attributes="value task[0]" />
</td>
<td tal:condition="not task[1]['task_completed']"
tal:content="task[1]['task_description']"></td>
<td tal:condition="task[1]['task_completed']">
<s tal:content="task[1]['task_description']"></s>
</td>
</tr>
</table>
<p tal:condition="tasks">
<input type="submit" name="done" value="done" />
<input type="submit" name="not done" value="not done" />
<input type="submit" name="delete" value="delete" />
</p>
<h3>New task</h3>
<textarea name="text" rows="5" cols="80"></textarea><br/>
<input type="submit" name="add" value="add" />
</form>
</body>
</html>
79 changes: 79 additions & 0 deletions code/transaction/todo_single_file/todo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os
import time
import transaction

from paste.httpserver import serve

from pyramid.view import view_config
from pyramid.config import Configurator

from pickledm import PickleDataManager

here = os.path.dirname(os.path.abspath(__file__))
template = os.path.join(here, 'todo.pt')

class Root(object):
def __init__(self, request):
self.request = request

class TodoView(object):

def __init__(self, request):
self.request = request
self.dm = PickleDataManager()
t = transaction.get()
t.join(self.dm)

@view_config(context=Root, request_method='GET', renderer=template)
def todo_view(self):
tasks = self.dm.items()
tasks.sort()
return { 'tasks': tasks, 'status': None }

@view_config(context=Root, request_param='add', renderer=template)
def add_view(self):
text = self.request.params.get('text')
key = str(time.time())
self.dm[key] = {'task_description': text, 'task_completed': False}
transaction.commit()
tasks = self.dm.items()
tasks.sort()
return { 'tasks': tasks, 'status': 'New task inserted.' }

@view_config(context=Root, request_param='done', renderer=template)
def done_view(self):
tasks = self.request.params.getall('tasks')
for task in tasks:
self.dm[task]['task_completed'] = True
transaction.commit()
tasks = self.dm.items()
tasks.sort()
return { 'tasks': tasks, 'status': 'Marked tasks as done.' }

@view_config(context=Root, request_param='not done', renderer=template)
def not_done_view(self):
tasks = self.request.params.getall('tasks')
for task in tasks:
self.dm[task]['task_completed'] = False
transaction.commit()
tasks = self.dm.items()
tasks.sort()
return { 'tasks': tasks, 'status': 'Marked tasks as not done.' }

@view_config(context=Root, request_param='delete', renderer=template)
def delete_view(self):
tasks = self.request.params.getall('tasks')
for task in tasks:
del(self.dm[task])
transaction.commit()
tasks = self.dm.items()
tasks.sort()
return { 'tasks': tasks, 'status': 'Deleted tasks.' }

if __name__ == '__main__':
settings = {}
config = Configurator(root_factory=Root, settings=settings)
config.scan()
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')

0 comments on commit 4ab9a58

Please sign in to comment.