|
12 | 12 | from rest_framework.views import APIView |
13 | 13 | from rest_framework_extensions.cache.decorators import cache_response |
14 | 14 |
|
15 | | -from .serializers import StatisticsSerializer, CitySerializer, \ |
16 | | - ProvinceSerializer, CountrySerializer, \ |
17 | | - RecommendSerializer, TimelineSerializer |
| 15 | +from .serializers import LatestStatisticsSerializer, StatisticsSerializer, \ |
| 16 | + CitySerializer, ProvinceSerializer, \ |
| 17 | + CountrySerializer, RecommendSerializer, \ |
| 18 | + TimelineSerializer |
18 | 19 | from .models import Crawler, Statistics, WHOArticle, Recommend, \ |
19 | 20 | City, Province, Country |
20 | 21 | from .filters import CityFilter, ProvinceFilter, CountryFilter |
21 | 22 |
|
| 23 | +from collections import OrderedDict |
22 | 24 |
|
23 | 25 | TIMEOUT = 60 * 60 |
24 | 26 |
|
25 | | -class StatisticsView(APIView): |
26 | | - """统计信息""" |
| 27 | +class LatestStatisticsView(APIView): |
| 28 | + |
| 29 | + """最新统计信息""" |
27 | 30 |
|
28 | 31 | def get_object(self): |
29 | 32 | try: |
@@ -77,9 +80,37 @@ def get_object(self): |
77 | 80 | @method_decorator(cache_page(TIMEOUT)) |
78 | 81 | def get(self, request): |
79 | 82 | data = self.get_object() |
80 | | - serializer = StatisticsSerializer(data) |
| 83 | + serializer = LatestStatisticsSerializer(data) |
81 | 84 | return Response(serializer.data) |
82 | 85 |
|
| 86 | +class StatisticsListView(ListAPIView): |
| 87 | + |
| 88 | + """统计信息列表""" |
| 89 | + |
| 90 | + serializer_class = StatisticsSerializer |
| 91 | + |
| 92 | + def get_queryset(self): |
| 93 | + result = OrderedDict() |
| 94 | + for inst in Statistics.objects.all(): |
| 95 | + crawler_id = inst.crawler_id |
| 96 | + country_type = inst.countryType |
| 97 | + if country_type == Statistics.GLOBAL: |
| 98 | + statistics = result.setdefault(crawler_id, {}) |
| 99 | + statistics['globalStatistics'] = inst |
| 100 | + elif country_type == Statistics.DOMESTIC: |
| 101 | + statistics = result.setdefault(crawler_id, {}) |
| 102 | + statistics['domesticStatistics'] = inst |
| 103 | + elif country_type == Statistics.INTERNATIONAL: |
| 104 | + statistics = result.setdefault(crawler_id, {}) |
| 105 | + statistics['internationalStatistics'] = inst |
| 106 | + statistics = result.setdefault(crawler_id, {}) |
| 107 | + statistics['modifyTime'] = inst.crawler.modifyTime |
| 108 | + statistics['createTime'] = inst.crawler.createTime |
| 109 | + return reversed(result.values()) |
| 110 | + |
| 111 | + @method_decorator(cache_page(TIMEOUT)) |
| 112 | + def dispatch(self, *args, **kwargs): |
| 113 | + return super(StatisticsListView, self).dispatch(*args, **kwargs) |
83 | 114 |
|
84 | 115 | class ProvinceListView(ListAPIView): |
85 | 116 |
|
|
0 commit comments