-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
230 lines (153 loc) · 4.26 KB
/
start.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import sqlite3 as lite
import sys
import os
from flask import *
from contextlib import closing
app = Flask(__name__, template_folder='templates')
appname = 'ReportIt'
DATABASE = 'db/c.db'
######### SQLITE
def test_db():
con = None
try:
con = lite.connect(DATABASE)
cur = con.cursor()
cur.execute('SELECT SQLITE_VERSION()')
data = cur.fetchone()
print "SQLite version: %s" % data
except lite.Error, e:
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_to_database()
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def connect_db():
return lite.connect(app.config['DATABASE'])
def make_dicts(cursor, row):
return dict((cur.description[idx][0], value)
for idx, value in enumerate(row))
def query_db(query, args=(), one=False):
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
# for user in query_db('select * from users'):
# print user['username'], 'has the id', user['user_id']
########## FLASK RESPONDS TO PAGES
@app.route('/submit', methods = ['GET', 'POST'])
def submit():
print (request.method, request.form)
if request.method == 'POST':
email = request.form['email']
lat = request.form['lat']
lon = request.form['lon']
cat1 = request.form['cat1']
cat2 = request.form['cat2']
description = request.form['description']
# notify = request.form['notify']
notify = False
city = request.form['city']
country = request.form['country']
pic_url = request.form['pic_url']
# city = ""
# country = ""
# pic_url = ""
con = lite.connect(DATABASE)
with con:
cur = con.cursor()
cur.execute('''INSERT INTO complaints(lat,lon,email,cat1,cat2,description,notify,city,country,pic_url)
VALUES(?,?,?,?,?,?,?,?,?,?)''', (lat,lon,email,cat1,cat2,description,notify,city,country,pic_url))
return render_template('form_action.html',
mail = email,
lat = lat,
lon = lon,
cat1 = cat1,
cat2 = cat2,
description = description
)
else:
lat = request.args.get('lat', False)
lon = request.args.get('lon', False)
if lat and lon:
return render_template('form_submit.html',
lat = lat,
lon = lon,
auto = True)
else:
return render_template('form_submit.html',
auto = False)
@app.route('/submit/<lat>/<lon>')
def submit_redirect(lat, lon):
return render_template('form_submit.html',
lat = lat,
lon = lon)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/map')
def map():
return render_template('map.html')
@app.route('/complaint')
def complaint():
return render_template('complaint_.html')
@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')
@app.route('/working')
def working():
return render_template('js-play.html')
@app.route('/test')
def index():
return render_template('map.html')
@app.route('/country/<country>')
def print_country_region(country):
country = country
con = lite.connect(DATABASE)
with con:
cur = con.cursor()
cur.execute("SELECT * FROM complaints WHERE country = :country OR city = :country AND lat NOT IN ('1.0', 1.0)",
{"country": country})
con.commit()
rows = cur.fetchall()
return render_template('country.html',
rows=rows,
country=country
)
# return render_template('hello.html', name = country)
@app.route('/city/<city>')
def print_city(city):
return render_template('js-play.html',
city=city
)
@app.route('/p/<int:post_id>')
def show_post(post_id):
####
# Post Page
####
return 'Post %d' % post_id
# with app.test_request_context():
# print url_for('home')
# print url_for('login', next = '/')
# print url_for('print_country_region', country = 'Jordan')
# url_for('static', filename='style.css')
# this has to be in folder static
if __name__ == '__main__':
app.debug = True
# app.logger.warning('A warning occurred (%d apples)', 42)
app.run(host='0.0.0.0')