-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcity.py
More file actions
24 lines (20 loc) · 937 Bytes
/
city.py
File metadata and controls
24 lines (20 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
import json
from .base import BaseHandler
from db.city import insert_many_into_city, select_from_city, select_all_from_city
class CityHandler(BaseHandler):
def get(self, *args, **kwargs):
name = self.get_argument('name', None)
if name:
data = select_from_city(city=name)
else:
data = select_all_from_city()
self.write({'status': 'success', 'count': len(data), 'data': data})
def post(self, *args, **kwargs):
with open(os.path.join(os.path.dirname(__file__), '../data/querys.json')) as fh:
cities = json.loads(fh.read())['cities']
message = insert_many_into_city(params=list(map(lambda x: (x, ), cities)))
if not message:
self.write({'status': 'success', 'message': '插入{}条城市数据'.format(len(cities))})
else:
self.write({'status': 'error', 'message': message})