-
Notifications
You must be signed in to change notification settings - Fork 3
/
jzs_mobile.py
269 lines (205 loc) · 6.41 KB
/
jzs_mobile.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# coding: utf-8
import re
from pymongo import Connection, ASCENDING, DESCENDING
from pymongo.objectid import ObjectId
from gevent.wsgi import WSGIServer
from flask import Flask, redirect, url_for, render_template, jsonify, \
request, flash, abort, session
from tornado import httpclient
from helper import get_city, get_city_by_ip
# config db
db = Connection('localhost', 27017).jzsou
# client
http_client = httpclient.HTTPClient()
cellphone = re.compile(r'^(1(([35]\d)|(47)|[8][0126789]))\d{8}$')
# config app
app = Flask(__name__)
app.config.from_pyfile('config.cfg')
@app.errorhandler(404)
def page_not_found(error):
return render_template("errors/404.html", error=error)
@app.errorhandler(500)
def server_error(error):
return render_template("errors/500.html", error=error)
# request handlers
@app.route('/')
@app.route('/<city>/')
def home_list(city=None):
if not city:
city_dict = session.get('curcity', None)
else:
city_dict = get_city(city)
session['curcity'] = city_dict
cates = db.Cate.find({}).sort('no', ASCENDING)
return render_template('home_list.html',
cates=cates,
city=city_dict)
@app.route('/setcity/<latlon>')
@app.route('/setcity/')
def setcity(latlon=None):
if latlon:
try:
city_label = http_client.fetch('http://l.n2u.in/city/%s' % latlon)
except httpclient.HTTPError, e:
city_label = 'hangzhou'
else:
city_label = get_city_by_ip()
city_dict = get_city(city_label)
session['curcity'] = city_dict
return jsonify(city_dict)
@app.route('/getcity/<latlon>')
@app.route('/getcity/')
def getcity(latlon=None):
if latlon:
try:
city_label = http_client.fetch('http://l.n2u.in/city/%s' % latlon)
except httpclient.HTTPError, e:
city_label = 'hangzhou'
else:
city_label = get_city_by_ip()
city_dict = get_city(city_label)
return jsonify(city_dict)
@app.route('/entry/<cate>/')
@app.route('/entry/<q>/s')
def entry_list(cate=None, q=None):
city = session.get('curcity', get_city('hangzhou'))
query_dict = {
'city_label': city['label'],
'status': 'show',
}
args = {
'_id': 1,
'title': 1,
'address': 1,
}
pos = request.args.get('pos', None)
if pos:
lat, lon = pos.split(',')
query_dict['_location'] = {
'$maxDistance': 0.091,
'$near': [float(lon), float(lat)]
}
st = int(request.args.get('st', 1))
# process functions
if cate:
query_dict['tags'] = cate
if q:
rqs = [e.lower() for e in re.split('\s+', q) if e]
regex = re.compile(r'%s' % '|'.join(rqs), re.IGNORECASE)
query_dict['$or'] = [{'title': regex}, {'brief': regex},
{'desc': regex}, {'tags': {'$in': rqs}}]
cur_entry = db.Entry.find(query_dict, args)
num = cur_entry.count()
entries = list(cur_entry.skip(st).limit(20))
for e in entries:
e['pk'] = str(e['_id'])
del e['_id']
# what's next
url = None
if st + 20 < num:
if q:
condition = 'key:%s' % q
else:
condition = 'tag:%s' % cate
if pos:
url = url_for('search', st=st+20, q=condition, pos=pos)
else:
url = url_for('search', st=st+20, q=condition)
return render_template('entry_list.html',
valid_city=db.City.find_one({'label': city['label'], 'block': False}),
entries=entries,
city=city,
q=q,
cate=db.Cate.find_one({'label': cate}),
data_url=url)
@app.route('/s/')
def search():
city = session.get('curcity', get_city('hangzhou'))
query_dict = {
'city_label': city['label'],
'status': 'show',
}
args = {
'_id': 1,
'title': 1,
'address': 1,
}
pos = request.args.get('pos', None)
if pos:
lat, lon = pos.split(',')
query_dict['_location'] = {
'$maxDistance': 0.091,
'$near': [float(lon), float(lat)]
}
condition = request.args.get('q')
if ':' in condition:
field, value = condition.split(':')
else:
abort(400)
st = int(request.args.get('st', 1))
# process functions
def do_tag(tag):
query_dict['tags'] = tag
return db.Entry.find(query_dict, args)
def do_key(data):
rqs = [e.lower() for e in re.split('\s+', data) if e]
regex = re.compile(r'%s' % '|'.join(rqs), re.IGNORECASE)
query_dict['$or'] = [{'title': regex}, {'brief': regex},
{'desc': regex}, {'tags': {'$in': rqs}}]
return db.Entry.find(query_dict, args)
handle_q = {
'tag': do_tag,
'key': do_key,
}
if field in handle_q:
cur_entry = handle_q[field](value)
num = cur_entry.count()
entries = list(cur_entry.skip(st).limit(20))
for e in entries:
e['pk'] = str(e['_id'])
del e['_id']
# what's next
if st + 20 < num:
if pos:
next = url_for('search', q=condition, st=st+20, pos=pos)
else:
next = url_for('search', q=condition, st=st+20)
else:
next = None
return render_template('macros/_listcell.html',
entries=entries,
next=next)
else:
abort(400)
@app.route('/city/')
def change_city():
cities = db.City.find({'block': False}).sort('no', ASCENDING)
return render_template('city.html',
cities=cities,
)
@app.route('/entry/<eid>/detail')
def detail(eid):
back_url = request.args.get('back')
entry = db.Entry.find_one({'_id': ObjectId(eid)})
phones = entry['contracts']
# get phone
tel = None
for e in phones:
if not cellphone.match(e):
tel = e
break
if not tel:
if len(phones) >= 3:
tel = phones[1]
else:
tel = phones[0]
if not entry: abort(404)
return render_template('detail.html',
back=back_url,
tel=tel,
is_cell=bool(cellphone.match(tel)),
e=entry)
# helpers
if __name__ == "__main__":
http_server = WSGIServer(('127.0.0.1', 8300), app)
http_server.serve_forever()