|
5 | 5 | import datetime
|
6 | 6 | import platform
|
7 | 7 | import csv
|
8 |
| -import os |
| 8 | +import os |
| 9 | +import traceback |
| 10 | +import yaml |
| 11 | + |
| 12 | +from const import * |
9 | 13 |
|
10 | 14 | data = Blueprint('register', __name__)
|
11 | 15 | if platform.system()=="Linux":
|
|
18 | 22 |
|
19 | 23 | if not os.path.exists(path_home):
|
20 | 24 | os.mkdir(path_home)
|
| 25 | + |
| 26 | +""" |
| 27 | +CACHE PATH |
| 28 | +""" |
| 29 | +HOSPITAL_PATH = os.path.join(path_home, "HOSPITAL.csv") |
| 30 | +HOTEL_PATH = os.path.join(path_home, "HOTEL.csv") |
| 31 | +LOGISITICAL_PATH = os.path.join(path_home, "LOGISITICAL.csv") |
| 32 | +NEWS_PATH = os.path.join(path_home, "NEWS.csv") |
| 33 | +DONATION_PATH = os.path.join(path_home, "DONATION.csv") |
| 34 | +FACTORY_PATH = os.path.join(path_home, "FACTORY.csv") |
| 35 | +CLINIC_PATH = os.path.join(path_home, "CLINIC.csv") |
| 36 | + |
| 37 | + |
| 38 | +""" |
| 39 | +Tools |
| 40 | +""" |
| 41 | +def csv_helper(fpath, headers): |
| 42 | + result = [] |
| 43 | + with open(fpath) as f: |
| 44 | + for line in f.readlines(): |
| 45 | + csv_data = line.strip().split(",") |
| 46 | + result.append(dict(zip(headers,csv_data))) |
| 47 | + return result |
| 48 | + |
| 49 | +def yaml_helper(fpath): |
| 50 | + result = [] |
| 51 | + with open(fpath, 'r') as f: |
| 52 | + result = yaml.load(f) |
| 53 | + return result |
| 54 | + |
| 55 | + |
| 56 | +@data.route('/hospital_list') |
| 57 | +def hospital_list(): |
| 58 | + resp = { |
| 59 | + 'success': False, |
| 60 | + 'data': [], |
| 61 | + 'msg': '', |
| 62 | + } |
| 63 | + try: |
| 64 | + resp_data = yaml_helper(HOSPITAL_PATH) |
| 65 | + resp['success'] = True |
| 66 | + resp['data'] = resp_data |
| 67 | + except Exception as e: |
| 68 | + resp['msg'] = str(e) |
| 69 | + return json.dumps(resp, ensure_ascii=False) |
| 70 | + |
21 | 71 |
|
22 |
| -PATH_HOSPITAL = os.path.join(path_home, "HOSPITAL.csv") |
23 |
| -PATH_LOGISTICAL = os.path.join(path_home, "LOGISTICAL.csv") |
24 |
| -PATH_HOTEL = os.path.join(path_home, "HOTEL.csv") |
25 | 72 |
|
26 | 73 |
|
27 | 74 | @data.route('/hotel_list')
|
28 | 75 | def hotel_list():
|
| 76 | + resp = { |
| 77 | + 'success': False, |
| 78 | + 'data': [], |
| 79 | + 'msg': '', |
| 80 | + } |
29 | 81 | try:
|
30 |
| - hotels = [] |
31 |
| - with open(PATH_HOTEL) as f: |
32 |
| - for line in f.readlines(): |
33 |
| - hotel = line.strip().split(",") |
34 |
| - item = {} |
35 |
| - item["name"] = hotel[0] |
36 |
| - item["area"] = hotel[1] |
37 |
| - item["address"] = hotel[2] |
38 |
| - item["bed_nums"] = hotel[3] |
39 |
| - item["phone"] = hotel[4] |
40 |
| - hotels.append(item) |
41 |
| - response = { |
42 |
| - "success" : True, |
43 |
| - "data" : hotels, |
44 |
| - } |
| 82 | + resp_data = csv_helper(HOTEL_PATH, HOTEL_HEADERS) |
| 83 | + resp['success'] = True |
| 84 | + resp['data'] = resp_data |
45 | 85 | except Exception as e:
|
46 |
| - response = { |
47 |
| - "success" : False, |
48 |
| - "message" : e.message, |
49 |
| - } |
50 |
| - return json.dumps(response,ensure_ascii=False) |
51 |
| - |
52 |
| -@data.route('/logistical_list') |
53 |
| -def logistical_list(): |
| 86 | + resp['msg'] = str(e) |
| 87 | + return json.dumps(resp, ensure_ascii=False) |
| 88 | + |
| 89 | +@data.route('/logstics_list') |
| 90 | +def logstics_list(): |
| 91 | + resp = { |
| 92 | + 'success': False, |
| 93 | + 'data': [], |
| 94 | + 'msg': '', |
| 95 | + } |
54 | 96 | try:
|
55 |
| - logisticals = [] |
56 |
| - with open(PATH_LOGISTICAL) as f: |
57 |
| - for line in f.readlines(): |
58 |
| - logistical = line.strip().split(",") |
59 |
| - item = {} |
60 |
| - item["name"] = logistical[0] |
61 |
| - item["area"] = logistical[1] |
62 |
| - item["ability"] = logistical[2] |
63 |
| - item["url"] = logistical[3] |
64 |
| - item["phone"] = logistical[4] |
65 |
| - logisticals.append(item) |
66 |
| - response = { |
67 |
| - "success" : True, |
68 |
| - "data" : logisticals, |
69 |
| - } |
| 97 | + resp_data = csv_helper(LOGISITICAL_PATH, LOGISITICAL_HEADERS) |
| 98 | + resp['success'] = True |
| 99 | + resp['data'] = resp_data |
70 | 100 | except Exception as e:
|
71 |
| - response = { |
72 |
| - "success" : False, |
73 |
| - "message" : e.message, |
74 |
| - } |
75 |
| - return json.dumps(response,ensure_ascii=False) |
| 101 | + resp['msg'] = str(e) |
| 102 | + return json.dumps(resp, ensure_ascii=False) |
76 | 103 |
|
77 |
| -@data.route('/hospital_list') |
78 |
| -def hospital_list(): |
| 104 | + |
| 105 | + |
| 106 | +@data.route('/news_list') |
| 107 | +def news_list(): |
| 108 | + resp = { |
| 109 | + 'success': False, |
| 110 | + 'data': [], |
| 111 | + 'msg': '', |
| 112 | + } |
| 113 | + try: |
| 114 | + resp_data = csv_helper(NEWS_PATH, NEWS_HEADERS) |
| 115 | + resp['success'] = True |
| 116 | + resp['data'] = resp_data |
| 117 | + except Exception as e: |
| 118 | + resp['msg'] = str(e) |
| 119 | + return json.dumps(resp, ensure_ascii=False) |
| 120 | + |
| 121 | + |
| 122 | +@data.route('/donation_list') |
| 123 | +def donation_list(): |
| 124 | + resp = { |
| 125 | + 'success': False, |
| 126 | + 'data': [], |
| 127 | + 'msg': '', |
| 128 | + } |
| 129 | + try: |
| 130 | + resp_data = csv_helper(DONATION_PATH, DONATION_HEADERS) |
| 131 | + resp['success'] = True |
| 132 | + resp['data'] = resp_data |
| 133 | + except Exception as e: |
| 134 | + resp['msg'] = str(e) |
| 135 | + return json.dumps(resp, ensure_ascii=False) |
| 136 | + |
| 137 | + |
| 138 | +@data.route('/factory_list') |
| 139 | +def factory_list(): |
| 140 | + resp = { |
| 141 | + 'success': False, |
| 142 | + 'data': [], |
| 143 | + 'msg': '', |
| 144 | + } |
| 145 | + try: |
| 146 | + resp_data = csv_helper(FACTORY_PATH, FACTORY_HEADERS) |
| 147 | + resp['success'] = True |
| 148 | + resp['data'] = resp_data |
| 149 | + except Exception as e: |
| 150 | + resp['msg'] = str(e) |
| 151 | + return json.dumps(resp, ensure_ascii=False) |
| 152 | + |
| 153 | + |
| 154 | +@data.route('/clinic_list') |
| 155 | +def clinic_list(): |
| 156 | + resp = { |
| 157 | + 'success': False, |
| 158 | + 'data': [], |
| 159 | + 'msg': '', |
| 160 | + } |
79 | 161 | try:
|
80 |
| - data = csv.reader(open(PATH_HOSPITAL, 'r')) |
81 |
| - next(data) |
82 |
| - hospitals = [] |
83 |
| - for hospital in data: |
84 |
| - item = {} |
85 |
| - if len(hospital)!=8: |
86 |
| - continue |
87 |
| - item["province"]=hospital[0] |
88 |
| - item["name"] = hospital[1] |
89 |
| - item["address"] = hospital[2] |
90 |
| - item["people"] = hospital[3] |
91 |
| - item["need"] = hospital[4] |
92 |
| - item["link"] = hospital[5] |
93 |
| - item["phone"] = hospital[6] |
94 |
| - item["extra"] = hospital[7] |
95 |
| - hospitals.append(item) |
96 |
| - response = { |
97 |
| - "success" : True, |
98 |
| - "data": hospitals, |
99 |
| - } |
| 162 | + resp_data = csv_helper(CLINIC_PATH, CLINC_HEADERS) |
| 163 | + resp['success'] = True |
| 164 | + resp['data'] = resp_data |
100 | 165 | except Exception as e:
|
101 |
| - response = { |
102 |
| - "success":False, |
103 |
| - "msg":e.message, |
104 |
| - } |
105 |
| - return json.dumps(response,ensure_ascii=False) |
| 166 | + resp['msg'] = str(e) |
| 167 | + return json.dumps(resp, ensure_ascii=False) |
0 commit comments