Skip to content

Commit 76b6263

Browse files
添加CLINC, HOSPITAL, FACTORY, DONATION API. (wuhan2020#14)
* add donation, factory, clinic. * fix conflict * add yaml for hospital. * add auth token wapper for http basic auth. Co-authored-by: LiuChangFreeman <[email protected]>
1 parent 990b67a commit 76b6263

File tree

3 files changed

+201
-74
lines changed

3 files changed

+201
-74
lines changed

const.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
"""
3+
CSV HEADERS
4+
"""
5+
DONATION_HEADERS = [
6+
'donate_sorce',
7+
'donate_way',
8+
'donate_link',
9+
'donate_contact',
10+
'donate_status',
11+
]
12+
FACTORY_HEADERS = [
13+
'factory_name',
14+
'factory_prov',
15+
'factory_addr',
16+
'factory_prod_type',
17+
'factory_capacity',
18+
'factory_contact',
19+
]
20+
CLINIC_HEADERS = [
21+
'clinic_unit',
22+
'clinic_contact',
23+
'clinic_note',
24+
]
25+
HOTEL_HEADERS = [
26+
'hotel_name',
27+
'hotel_loc',
28+
'hotel_addr',
29+
'hotel_supplier',
30+
'hotel_concat'
31+
]
32+
LOGISTICS_HEADERS = [
33+
'logistics_name',
34+
'logistics_area',
35+
'logistics_power',
36+
'logistics_link',
37+
'logistics_contact'
38+
]
39+
NEWS_HEADERS = [
40+
'news_title',
41+
'news_summary',
42+
'news_time',
43+
'news_link',
44+
]

tools.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import functools
2+
import logging
3+
from flask import make_response
4+
5+
logger = logging.getLogger(__file__)
6+
7+
8+
def auth_token_wapper(token):
9+
def _handler(func):
10+
@functools.wraps(func)
11+
def wrapper(request, *args, **kwargs):
12+
http_token = request.META.get('HTTP_TOKEN')
13+
logger.info(http_token)
14+
if not http_token:
15+
return make_response('403: HTTP_TOKEN is invalid')
16+
else:
17+
if token != http_token:
18+
return make_response('403: HTTP_TOKEN is invalid')
19+
return func(request, *args, **kwargs)
20+
return wrapper
21+
return _handler

utils.py

Lines changed: 136 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import datetime
66
import platform
77
import csv
8-
import os
8+
import os
9+
import traceback
10+
import yaml
11+
12+
from const import *
913

1014
data = Blueprint('register', __name__)
1115
if platform.system()=="Linux":
@@ -18,88 +22,146 @@
1822

1923
if not os.path.exists(path_home):
2024
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+
2171

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")
2572

2673

2774
@data.route('/hotel_list')
2875
def hotel_list():
76+
resp = {
77+
'success': False,
78+
'data': [],
79+
'msg': '',
80+
}
2981
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
4585
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+
}
5496
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
70100
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)
76103

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+
}
79161
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
100165
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

Comments
 (0)