-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
98 lines (86 loc) · 3.26 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import logging
from pyramid.config import Configurator
from pyramid.events import NewRequest
from pyramid.events import subscriber
from pyramid.events import ApplicationCreated
from pyramid.httpexceptions import HTTPFound
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.view import view_config
from wsgiref.simple_server import make_server
import psycopg2
import psycopg2.extras
logging.basicConfig()
log = logging.getLogger(__file__)
here = os.path.dirname(os.path.abspath(__file__))
# views
@view_config(route_name='list', renderer='list.mako')
def list_view(request):
print request.cursor
rs = request.cursor.execute("select id, name from tasks where closed = false")
print rs;
tasks = [dict(id=row[0], name=row[1]) for row in rs.fetchall()]
return {'tasks': tasks}
@view_config(route_name='new', renderer='new.mako')
def new_view(request):
if request.method == 'POST':
if request.POST.get('name'):
request.db.execute('insert into tasks (name, closed) values (?, ?)',
[request.POST['name'], 0])
request.db.commit()
request.session.flash('New task was successfully added!')
return HTTPFound(location=request.route_url('list'))
else:
request.session.flash('Please enter a name for the task!')
return {}
@view_config(route_name='close')
def close_view(request):
task_id = int(request.matchdict['id'])
request.cursor("update tasks set closed = ? where id = ?", (1, task_id))
request.db.commit()
request.session.flash('Task was successfully closed!')
return HTTPFound(location=request.route_url('list'))
@view_config(context='pyramid.exceptions.NotFound', renderer='notfound.mako')
def notfound_view(self):
return {}
# subscribers
@subscriber(NewRequest)
def new_request_subscriber(event):
request = event.request
settings = request.registry.settings
request.db = psycopg2.connect(settings['db'])
request.cursor = request.db.cursor()
@subscriber(ApplicationCreated)
def application_created_subscriber(event):
log.warn('Initializing database...')
f = open(os.path.join(here, 'schema.sql'), 'r')
stmt = f.read()
settings = event.app.registry.settings
db = psycopg2.connect(settings['db'])
cursor = db.cursor()
cursor.execute(stmt)
db.commit()
f.close()
if __name__ == '__main__':
# configuration settings
settings = {}
settings['reload_all'] = True
settings['debug_all'] = True
settings['mako.directories'] = os.path.join(here, 'templates')
settings['db'] = ('host=localhost dbname=tasks user=postgres password= port=5432')
# session factory
session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet')
# configuration setup
config = Configurator(settings=settings, session_factory=session_factory)
# routes setup
config.add_route('list', '/')
config.add_route('new', '/new')
config.add_route('close', '/close/{id}')
# static view setup
config.add_static_view('static', os.path.join(here, 'static'))
# scan for @view_config and @subscriber decorators
config.scan()
# serve app
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()