/
venuerunner.py
152 lines (129 loc) · 4.35 KB
/
venuerunner.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import sqlite3
from flask import Flask, g, render_template, request, redirect, url_for
from werkzeug.datastructures import MultiDict
from contextlib import closing
#VenueRunner classes
import VRForms
import VRDB
# Config - TODO use separate file!
DEBUG = True # Remove in production code
DATABASE = 'venue.db'
PASSWORD = 'moo'
SECRET_KEY = 'SEKERETKEY'
SCHEMA_FILE = 'schema.sql'
app = Flask(__name__)
app.config.from_object(__name__)
#set up the application
def init_db():
with closing(connect_db()) as db:
with app.open_resource(app.config['SCHEMA_FILE']) as f:
db.cursor().executescript(f.read())
db.commit()
def connect_db():
db = sqlite3.connect(app.config['DATABASE'])
#force database to use foreign keys
#db.execute('PRAGMA foreign_keys = ON')
return db
# Assume we always need db connection.
@app.before_request
def before_request():
g.db = connect_db()
@app.after_request
def after_request(response):
g.db.close()
return response
@app.route('/favicon.ico')
def favicon():
return app.send_static_file('favicon.ico')
@app.route('/')
def menu():
return render_template('menu.html')
#TODO write save functionality
@app.route('/customer/<int:customer_id>', methods=['GET', 'POST'])
def customer(customer_id):
customer = VRDB.customer(customer_id)
form = VRForms.addcustomer(MultiDict(customer))
return render_template('customer.html', form=form)
@app.route('/list_customers')
def list_customers():
customers = VRDB.customers()
return render_template('list_customers.html', customers=customers)
#posts form and redirects to the new customer. Really should let the app
# decide what is best to do rather than redirect!
@app.route('/add_customer', methods=['GET', 'POST'])
def add_customer():
form = VRForms.addcustomer()
if form.validate_on_submit():
lead = request.form.get('lead', False) == 'y'
follow = request.form.get('follow', False) == 'y'
customer_id = VRDB.customer_add(first_name=request.form['first_name'],
last_name=request.form['last_name'],
email=request.form['email'],
lead=lead,
follow=follow,
signup_date=request.form['signup_date'],
note=request.form['note'])
return redirect(url_for('customer', customer_id=customer_id))
return render_template('add_customer.html', form=form)
#REST thingy to add customers, not curretly used
@app.route('/customer/add', methods=['POST'])
def post_add_customer():
form = VRForms.addcustomer()
customer_id = -1;
errors = None;
if form.validate_on_submit():
lead = request.form.get('lead', False) == 'y'
follow = request.form.get('follow', False) == 'y'
customer_id = customer_add(first_name=request.form['first_name'],
last_name=request.form['last_name'],
email=request.form['email'],
lead=lead,
follow=follow,
signup_date=request.form['signup_date'],
note=request.form['note'])
return jsonify(errors=errors, customer_id =customer_id)
#TODO write save functionality
@app.route('/event/<int:event_id>')
def event(event_id):
event = VRDB.event(event_id)
form = VRForms.addevent(MultiDict(event))
customers = VRDB.customers()
attendence = VRDB.event_attendence(event_id)
return render_template('event.html', form=form, customers=customers)
@app.route('/list_events')
def list_events():
events = VRDB.events()
return render_template('list_events.html', events=events)
@app.route('/add_event', methods=['GET', 'POST'])
def add_event():
form = VRForms.addevent()
if form.validate_on_submit():
event_id = VRDB.event_add(name=request.form['name'], date=request.form['date'],
description=request.form['description'])
app.logger.debug('moo') # XXX
return redirect(url_for('event', event_id=event_id))
return render_template('add_event.html', form=form)
@app.route('/event/attendence/record', methods=['POST'])
def record_attendence():
#validate
#record
# structure
# list
# customer id
# price
# event
return jsonify(success=true)
@app.route('/event/attendence/record', methods=['POST'])
def delete_attendence():
return
# dead code, but maybe use in the future
def smart_truncate(content, length=100, suffix='...'):
if len(content) <= length:
return content
else:
return content[:length].rsplit(' ', 1)[0]+suffix
def htmlify(content):
content = content.strip()
return content
if __name__ == '__main__':
app.run()