Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NewFeature]列医院信息+按关键字搜索医院 #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
'factory_capacity',
'factory_contact',
]
HOSPICAL_HEADERS = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: HOSPITAL_HEADERS

'hospital_name',
'hospital_addr',
'hospital_headcount',
'hospital_requirement',
'hospital_link',
'hospital_contact',
'hospital_comment'
]
CLINIC_HEADERS = [
'clinic_unit',
'clinic_contact',
Expand Down
49 changes: 46 additions & 3 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
from flask import Blueprint
from flask import request
import codecs
import os
import json
import datetime
Expand Down Expand Up @@ -40,12 +42,35 @@
"""
def csv_helper(fpath, headers):
result = []
with open(fpath) as f:
with codecs.open(fpath, encoding='utf-8') as f:
for line in f.readlines()[1:]:
csv_data = line.strip().split(",")
result.append(dict(zip(headers,csv_data)))
return result

def filter_entity(entity_list, search_fields, keywords):
recall_set = []
for entity in entity_list:
if is_valid_entity(entity, search_fields, keywords):
recall_set.append(entity)
return recall_set

def is_valid_entity(entity, search_fields, keywords):
field_values = []
for search_field in search_fields:
if search_field in entity:
field_values.append(entity[search_field])
field_val_concat = ' '.join(field_values).lower()
is_valid = True
for keyword in keywords.split(' '):
try:
if keyword.lower() not in field_val_concat:
is_valid = False
break
except Exception as ex:
Copy link
Contributor

@rexwangcc rexwangcc Jan 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be great to not catch all exceptions, but only a handful of expected errors.

is_valid = False
return is_valid

def yaml_helper(fpath):
result = []
with open(fpath, 'r') as f:
Expand All @@ -61,15 +86,33 @@ def hospital_list():
'msg': '',
}
try:
resp_data = csv_helper(HOSPITAL_PATH,HOTEL_HEADERS)
resp_data = csv_helper(HOSPITAL_PATH,HOSPICAL_HEADERS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: HOSPITAL_HEADERS

resp['success'] = True
resp['data'] = resp_data
except Exception as e:
resp['msg'] = str(e)
return json.dumps(resp, ensure_ascii=False)



@data.route('/hospital_search')
def hospital_search():
keyword = request.args.get('keyword')
print('keyword: %s' % keyword)
resp = {
'success': False,
'data': [],
'msg': '',
}
try:
hospital_all = csv_helper(HOSPITAL_PATH,HOSPICAL_HEADERS)
search_fields = ['hospital_name', 'hospital_addr', 'hospital_requirement']
print('search_fields created: %s' % search_fields)
search_result = filter_entity(hospital_all, search_fields, keyword)
resp['success'] = True
resp['data'] = search_result
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to https://github.com/wuhan2020/api-server/pull/38/files#r372081013, this is trying to catch any exceptions, it also swallows the status codes.

resp['msg'] = str(e)
return json.dumps(resp, ensure_ascii=False)

@data.route('/hotel_list')
def hotel_list():
Expand Down