Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.

Commit 71cf293

Browse files
authored
Merge pull request #10 from leafcoder/develop
Develop
2 parents e7242c2 + 898c672 commit 71cf293

File tree

7 files changed

+172
-31
lines changed

7 files changed

+172
-31
lines changed

demo/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
77
<title>新冠肺炎(2019-nCoV)疫情大屏</title>
88
<link rel="stylesheet" type="text/css" href="css/app.css" />
9+
<script>
10+
var _hmt = _hmt || [];
11+
(function() {
12+
var hm = document.createElement("script");
13+
hm.src = "https://hm.baidu.com/hm.js?f3fbccf0789476b80fab0c58359399a5";
14+
var s = document.getElementsByTagName("script")[0];
15+
s.parentNode.insertBefore(hm, s);
16+
})();
17+
</script>
18+
<script data-ad-client="ca-pub-9406265482044737" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
919
</head>
1020

1121
<body>

demo_proj/ncov/settings.py

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
https://docs.djangoproject.com/en/2.2/ref/settings/
1111
"""
1212

13+
# insert project path here
14+
import sys; sys.path.insert(0, '..')
15+
1316
import os
1417

1518
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)

django_covid19/serializers.py

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ class Meta:
167167
exclude = ('id', 'dailyData')
168168

169169

170+
class StateDailyListSerializer(serializers.ModelSerializer):
171+
172+
class Meta:
173+
model = models.State
174+
fields = ['stateName', 'dailyData']
175+
176+
170177
class StateDailySerializer(serializers.Serializer):
171178

172179
state = serializers.CharField()

django_covid19/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
path('countries/<str:countryName>/daily/', views.CountryDailyListView.as_view(), name='country-daily-list'),
2828

2929
url(r'states/(?:(?P<raw>raw)/)?(?P<countryShortCode>[^/]+)/$', views.StateListView.as_view(), name='state-list'),
30+
url(r'states/(?:(?P<raw>raw)/)?(?P<countryShortCode>[^/]+)/daily/$', views.StateListDailyListView.as_view(), name='state-list-daily-list'),
3031
url(r'states/(?:(?P<raw>raw)/)?(?P<countryShortCode>[^/]+)/(?P<state>[A-Z]+)/$', views.StateRetrieveView.as_view(), name='state-detail'),
3132
url(r'states/(?:(?P<raw>raw)/)?(?P<countryShortCode>[^/]+)/(?P<state>[A-Z]+)/daily/$', views.StateDailyListView.as_view(), name='state-daily-list'),
3233
url(r'states/(?:(?P<raw>raw)/)?(?P<countryShortCode>[^/]+)/(?P<stateName>[^/]+)/$', views.StateRetrieveByNameView.as_view(), name='state-detail-by-name'),

django_covid19/views.py

+80-30
Original file line numberDiff line numberDiff line change
@@ -320,37 +320,14 @@ def get(self, request, countryShortCode, state, raw=None):
320320
return Response(serializer.data)
321321

322322

323-
class StateDailyListView(APIView):
323+
class BaseDailyView(object):
324324

325-
"""州按天返回列表"""
326-
327-
def get_object(self, countryShortCode, state):
328-
state = models.State.objects.filter(
329-
countryShortCode=countryShortCode, state=state).first()
330-
if state is None:
331-
raise Http404
332-
return state
333-
334-
@method_decorator(cache_page(
335-
CACHE_PAGE_TIMEOUT, key_prefix='state-daily-list'))
336-
def get(self, request, countryShortCode, state, raw=None):
337-
inst = self.get_object(countryShortCode, state)
338-
result = inst.dailyData
339-
result = json.loads(result)
340-
if raw == 'raw':
341-
return Response(result)
342-
data = []
343-
for r in result:
344-
data.append(self.format(inst, r))
345-
serializer = serializers.StateDailySerializer(data, many=True)
346-
return Response(serializer.data)
347-
348-
def format(self, inst, data):
325+
def format(self, countryShortCode, stateName, data):
349326
item = {}
350327
item['date'] = data['date']
351328
item['state'] = data['state']
352-
item['stateName'] = inst.stateName
353-
item['countryShortCode'] = inst.countryShortCode
329+
item['stateName'] = stateName
330+
item['countryShortCode'] = countryShortCode
354331

355332
item['confirmedCount'] = data.get('positive')
356333
item['currentConfirmedCount'] = self.get_current_confirmed(data)
@@ -376,7 +353,34 @@ def get_current_confirmed_incr(self, data):
376353
death = data['deathIncrease'] if data.get('deathIncrease') else 0
377354
return positive - death
378355

379-
class StateDailyListByNameView(StateDailyListView):
356+
357+
class StateDailyListView(APIView, BaseDailyView):
358+
359+
"""州按天返回列表"""
360+
361+
def get_object(self, countryShortCode, state):
362+
state = models.State.objects.filter(
363+
countryShortCode=countryShortCode, state=state).first()
364+
if state is None:
365+
raise Http404
366+
return state
367+
368+
@method_decorator(cache_page(
369+
CACHE_PAGE_TIMEOUT, key_prefix='state-daily-list'))
370+
def get(self, request, countryShortCode, state, raw=None):
371+
inst = self.get_object(countryShortCode, state)
372+
result = inst.dailyData
373+
result = json.loads(result)
374+
if raw == 'raw':
375+
return Response(result)
376+
stateName = inst.stateName
377+
data = []
378+
for r in result:
379+
data.append(self.format(countryShortCode, stateName, r))
380+
serializer = serializers.StateDailySerializer(data, many=True)
381+
return Response(serializer.data)
382+
383+
class StateDailyListByNameView(APIView, BaseDailyView):
380384

381385
def get_object(self, countryShortCode, stateName):
382386
state = models.State.objects.filter(
@@ -393,8 +397,54 @@ def get(self, request, countryShortCode, stateName, raw=None):
393397
result = json.loads(result)
394398
if raw == 'raw':
395399
return Response(result)
400+
stateName = inst.stateName
396401
data = []
397402
for r in result:
398-
data.append(self.format(inst, r))
403+
data.append(self.format(countryShortCode, stateName, r))
399404
serializer = serializers.StateDailySerializer(data, many=True)
400-
return Response(serializer.data)
405+
return Response(serializer.data)
406+
407+
408+
class StateListDailyListView(ListAPIView, BaseDailyView):
409+
410+
serializer_class = serializers.StateDailyListSerializer
411+
filter_class = filters.StateFilter
412+
413+
def get_queryset(self):
414+
countryShortCode = self.kwargs['countryShortCode']
415+
return models.State.objects.filter(
416+
countryShortCode=countryShortCode).order_by('state')
417+
418+
def list(self, request, *args, **kwargs):
419+
countryShortCode = kwargs['countryShortCode']
420+
queryset = self.filter_queryset(self.get_queryset())
421+
422+
if kwargs.get('raw') == 'raw':
423+
self.serializer_class = serializers.StateRawSerializer
424+
425+
result = []
426+
page = self.paginate_queryset(queryset)
427+
if page is not None:
428+
serializer = self.get_serializer(page, many=True)
429+
for item in serializer.data:
430+
stateName = item['stateName']
431+
dailyData = json.loads(item['dailyData'])
432+
for daily in dailyData:
433+
result.append(
434+
self.format(countryShortCode, stateName, daily))
435+
return self.get_paginated_response(result)
436+
437+
serializer = self.get_serializer(queryset, many=True)
438+
for item in serializer.data:
439+
stateName = item['stateName']
440+
dailyData = json.loads(item['dailyData'])
441+
for daily in dailyData:
442+
result.append(
443+
self.format(countryShortCode, stateName, daily))
444+
return Response(result)
445+
446+
@method_decorator(cache_page(
447+
CACHE_PAGE_TIMEOUT, key_prefix='state-list-daily-list'))
448+
def dispatch(self, *args, **kwargs):
449+
return super(StateListDailyListView, self).dispatch(*args, **kwargs)
450+

docs/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,76 @@ http://111.231.75.86:8000/api/states/USA/?stateNames=Alaska,Alabama
783783
]
784784
```
785785

786+
787+
#### 多个州的日统计 :id=state-USA-list-daily
788+
789+
可获取全部州或某几个州的日统计数据列表;
790+
791+
接口地址:/api/states/USA/\<STATE\>/daily/
792+
793+
原始数据:/api/states/raw/USA/\<STATE\>/daily/
794+
795+
请求方法:GET
796+
797+
请求参数:
798+
799+
参数 | 描述
800+
------------------- | -------
801+
stateNames | 州名,如:Alaska,Alabama;以逗号分割多个值;大小写敏感;
802+
states | 州缩写,如:AK(Alaska),AL(Alabama);大小写敏感;
803+
804+
示例链接:
805+
806+
http://111.231.75.86:8000/api/states/USA/daily/
807+
808+
http://111.231.75.86:8000/api/states/USA/daily/?states=AK,AL
809+
810+
http://111.231.75.86:8000/api/states/USA/daily/?stateNames=Alaska,Alabama
811+
812+
返回结果:
813+
814+
```
815+
[
816+
{
817+
"date": 20200306,
818+
"state": "AK",
819+
"stateName": "Alaska",
820+
"countryShortCode": "USA",
821+
"confirmedCount": 0,
822+
"currentConfirmedCount": 0,
823+
"suspectedCount": 1,
824+
"curedCount": null,
825+
"deadCount": 0,
826+
"currentConfirmedIncr": 0,
827+
"confirmedIncr": null,
828+
"suspectedIncr": null,
829+
"curedIncr": null,
830+
"deadIncr": null
831+
},
832+
// (20200307 - 现在)AK 日统计
833+
...
834+
{
835+
"date": 20200307,
836+
"state": "AL",
837+
"stateName": "Alabama",
838+
"countryShortCode": "USA",
839+
"confirmedCount": 0,
840+
"currentConfirmedCount": 0,
841+
"suspectedCount": null,
842+
"curedCount": null,
843+
"deadCount": null,
844+
"currentConfirmedIncr": 0,
845+
"confirmedIncr": null,
846+
"suspectedIncr": null,
847+
"curedIncr": null,
848+
"deadIncr": null
849+
},
850+
// (20200307 - 现在)AL 日统计
851+
...
852+
// 其他州的日统计
853+
```
854+
855+
786856
#### 某州最新疫情 :id=state-USA-detail
787857

788858

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = django_covid19
3-
version = 0.4a0
3+
version = 0.4a1
44
description = An django app of the covid-19 API in countries around the world, provinces and cities in China, and states in the USA.
55
long_description = file: README.md
66
long-description-content-type = text/markdown

0 commit comments

Comments
 (0)