1
1
import requests
2
- from datetime import datetime , timezone , timedelta
2
+ from datetime import datetime , timedelta
3
3
from collections import defaultdict
4
- from django .shortcuts import render , get_object_or_404
4
+ from django .shortcuts import render
5
5
from django .http import Http404
6
6
from django .conf import settings
7
- from main .enums import Dimension , SensorModel , OutputFormat , Precision , Order
7
+ from main .enums import OutputFormat , Precision , Order
8
+ from requests .exceptions import HTTPError , RequestException
9
+ from django .core .paginator import Paginator , EmptyPage , PageNotAnInteger
8
10
9
11
def StationDetailView (request , pk ):
10
12
# Beispiel API-URL, die von der Station-ID abhängt
@@ -61,33 +63,70 @@ def StationDetailView(request, pk):
61
63
62
64
def StationListView (request ):
63
65
"""
64
- max_station: List[Tuple[device,time_measured,dimension,value]]
65
- min_stations: List[Tuple[device,time_measured,dimension,value]]
66
+ Fetches two lists: stations with the highest PM2.5 values and
67
+ stations with the lowest PM2.5 values.
66
68
"""
67
-
68
69
url_min = f"{ settings .API_URL } /station/topn?n=10&dimension=3&order={ Order .MIN .value } &output_format={ OutputFormat .CSV .value } "
69
70
url_max = f"{ settings .API_URL } /station/topn?n=10&dimension=3&order={ Order .MAX .value } &output_format={ OutputFormat .CSV .value } "
70
-
71
- resp_min = requests .get (url_min )
72
- resp_max = requests .get (url_max )
73
71
74
- # TODO: try catch
75
- resp_min .raise_for_status ()
76
- resp_max .raise_for_status ()
72
+ error_message = None
73
+ try :
74
+ resp_min = requests .get (url_min )
75
+ resp_max = requests .get (url_max )
77
76
78
- min_stations = [
79
- line .split ("," )
80
- for i , line in enumerate (resp_min .text .splitlines ())
81
- if i
82
- ]
77
+ # Raise HTTPError for bad responses
78
+ resp_min .raise_for_status ()
79
+ resp_max .raise_for_status ()
83
80
84
- max_stations = [
85
- line .split ("," )
86
- for i , line in enumerate (resp_max .text .splitlines ())
81
+ # Skip header line (i == 0)
82
+ min_stations = [
83
+ line .split ("," )
84
+ for i , line in enumerate (resp_min .text .splitlines ())
87
85
if i
88
- ]
86
+ ]
87
+ max_stations = [
88
+ line .split ("," )
89
+ for i , line in enumerate (resp_max .text .splitlines ())
90
+ if i
91
+ ]
92
+
93
+ except (HTTPError , RequestException ) as e :
94
+ # Instead of raising a 404, store an error message in the context.
95
+ error_message = "There was an error fetching station data: 404."
96
+ # Optionally, you can log the error:
97
+ print (f"Error fetching station data: { e } " )
98
+ min_stations = []
99
+ max_stations = []
100
+
101
+ # Paginate each list separately (example: 5 stations per page)
102
+ paginator_top = Paginator (max_stations , 5 )
103
+ paginator_low = Paginator (min_stations , 5 )
104
+
105
+ page_top = request .GET .get ('page_top' )
106
+ page_low = request .GET .get ('page_low' )
107
+
108
+ try :
109
+ top_stations_page = paginator_top .page (page_top )
110
+ except PageNotAnInteger :
111
+ top_stations_page = paginator_top .page (1 )
112
+ except EmptyPage :
113
+ top_stations_page = paginator_top .page (paginator_top .num_pages )
114
+
115
+ try :
116
+ lowest_stations_page = paginator_low .page (page_low )
117
+ except PageNotAnInteger :
118
+ lowest_stations_page = paginator_low .page (1 )
119
+ except EmptyPage :
120
+ lowest_stations_page = paginator_low .page (paginator_low .num_pages )
121
+
122
+ context = {
123
+ 'top_stations' : top_stations_page ,
124
+ 'lowest_stations' : lowest_stations_page ,
125
+ 'paginator_top' : paginator_top ,
126
+ 'paginator_low' : paginator_low ,
127
+ 'page_top' : top_stations_page ,
128
+ 'page_low' : lowest_stations_page ,
129
+ 'error' : error_message ,
130
+ }
89
131
90
- return render (request , 'stations/list.html' , {
91
- 'top_stations' : max_stations ,
92
- 'lowest_stations' : min_stations ,
93
- })
132
+ return render (request , 'stations/list.html' , context )
0 commit comments