Skip to content

Commit c6493e8

Browse files
ray-oxdfatbird
authored andcommitted
DBC22-1484: cleanups and fixes
1 parent e4f0154 commit c6493e8

16 files changed

+253
-256
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ repos:
2121
- flake8-django==1.1.2
2222
- flake8-pytest-style==1.6.0
2323
- flake8-typing-imports==1.12.0
24-
args: [ --max-line-length=88 ]
24+
args: [ --max-line-length=128 ]
2525
stages: [commit]
2626
exclude: '^backend/\w+/migrations'
2727
- repo: https://github.com/PyCQA/bandit

src/backend/apps/feed/client.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@
33
from urllib.parse import urljoin
44

55
import httpx
6-
from apps.feed.constants import DIT, INLAND_FERRY, OPEN511, WEBCAM
7-
from apps.feed.constants import DIT, INLAND_FERRY, OPEN511, WEBCAM, REGIONAL_WEATHER, REGIONAL_WEATHER_AREAS
6+
import requests
7+
from apps.feed.constants import (
8+
DIT,
9+
INLAND_FERRY,
10+
OPEN511,
11+
REGIONAL_WEATHER,
12+
REGIONAL_WEATHER_AREAS,
13+
WEBCAM,
14+
)
815
from apps.feed.serializers import (
916
CarsClosureEventSerializer,
1017
EventAPISerializer,
1118
EventFeedSerializer,
1219
FerryAPISerializer,
13-
RegionalWeatherFeedSerializer,
1420
RegionalWeatherSerializer,
15-
RegionalWeatherAPISerializer,
1621
WebcamAPISerializer,
1722
WebcamFeedSerializer,
1823
)
1924
from django.conf import settings
2025
from rest_framework.exceptions import ValidationError
21-
import requests
2226
from rest_framework.response import Response
2327

2428
logger = logging.getLogger(__name__)
@@ -149,7 +153,7 @@ def get_closures_dict(self):
149153
{"format": "json", "limit": 500}
150154
)
151155

152-
return {event["id"]:True for event in events if event["closed"]}
156+
return {event["id"]: True for event in events if event["closed"]}
153157

154158
# Ferries
155159
def get_ferries_list(self):
@@ -252,13 +256,13 @@ def get_regional_weather_list_feed(self, resource_type, resource_name, serialize
252256
'condition': condition,
253257
'temperature_units': temperature_units,
254258
'temperature_value': temperature_value,
255-
'visibility_units' : visibility_units,
256-
'visibility_value' : visibility_value,
257-
'wind_speed_units' : wind_speed_units,
259+
'visibility_units': visibility_units,
260+
'visibility_value': visibility_value,
261+
'wind_speed_units': wind_speed_units,
258262
'wind_speed_value': wind_speed_value,
259-
'wind_gust_units' : wind_gust_units,
260-
'wind_gust_value' : wind_gust_value,
261-
'wind_direction' : wind_direction,
263+
'wind_gust_units': wind_gust_units,
264+
'wind_gust_value': wind_gust_value,
265+
'wind_direction': wind_direction,
262266
}
263267

264268
name_data = data.get("Location", {}).get("Name", {})
@@ -279,7 +283,8 @@ def get_regional_weather_list_feed(self, resource_type, resource_name, serialize
279283
forecast_group = forecast_group_data.get("Forecasts") if forecast_group_data else None
280284

281285
hourly_forecast_group_data = data.get("HourlyForecastGroup", {})
282-
hourly_forecast_group = hourly_forecast_group_data.get("HourlyForecasts") if hourly_forecast_group_data else None
286+
hourly_forecast_group = hourly_forecast_group_data.get(
287+
"HourlyForecasts") if hourly_forecast_group_data else None
283288

284289
regional_weather_data = {
285290
'code': code,
@@ -296,7 +301,8 @@ def get_regional_weather_list_feed(self, resource_type, resource_name, serialize
296301
'hourly_forecast_group': hourly_forecast_group,
297302
}
298303

299-
serializer = serializer_cls(data=regional_weather_data, many=isinstance(regional_weather_data, list))
304+
serializer = serializer_cls(data=regional_weather_data,
305+
many=isinstance(regional_weather_data, list))
300306
json_objects.append(regional_weather_data)
301307

302308
except requests.RequestException as e:
@@ -314,9 +320,8 @@ def get_regional_weather_list_feed(self, resource_type, resource_name, serialize
314320
for field, errors in field_errors.items():
315321
print(f"Field: {field}, Errors: {errors}")
316322

317-
318323
def get_regional_weather_list(self):
319324
return self.get_regional_weather_list_feed(
320325
REGIONAL_WEATHER, 'regionalweather', RegionalWeatherSerializer,
321326
{"format": "json", "limit": 500}
322-
)
327+
)

src/backend/apps/shared/tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from apps.cms.models import Ferry
55
from apps.event.models import Event
6+
from apps.weather.models import RegionalWeather
67
from apps.webcam.models import Webcam
78
from django.core.cache import cache
89
from django.test import TestCase
@@ -46,3 +47,4 @@ def tearDown(self):
4647
Webcam.objects.all().delete()
4748
Event.objects.all().delete()
4849
Ferry.objects.all().delete()
50+
RegionalWeather.objects.all().delete()

src/backend/apps/weather/admin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
class WeatherAdmin(ModelAdmin):
77
readonly_fields = ('id', )
88

9-
admin.site.register(RegionalWeather, WeatherAdmin)
10-
119

10+
admin.site.register(RegionalWeather, WeatherAdmin)

src/backend/apps/weather/management/__init__.py

Whitespace-only changes.

src/backend/apps/weather/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from apps.weather.tasks import populate_all_regional_weather_data
2+
from django.core.management.base import BaseCommand
3+
4+
5+
class Command(BaseCommand):
6+
def handle(self, *args, **options):
7+
populate_all_regional_weather_data()

src/backend/apps/weather/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.contrib.gis.db import models
33
from django.contrib.gis.geos import Point
44

5+
56
class RegionalWeather(BaseModel):
67
location = models.PointField(null=True)
78
code = models.CharField(max_length=10, null=True)
@@ -24,12 +25,12 @@ def get_forecasts(self):
2425

2526
def __str__(self):
2627
return f"Regional Forecast for {self.pk}"
27-
28+
2829
def save(self, *args, **kwargs):
2930
latitude, longitude = self.convert_coordinates(str(self.location_latitude), str(self.location_longitude))
3031
self.location = Point(longitude, latitude)
3132
super().save(*args, **kwargs)
32-
33+
3334
def convert_coordinates(self, latitude_str, longitude_str):
3435
latitude = float(latitude_str[:-1])
3536
longitude = float(longitude_str[:-1])
@@ -40,4 +41,3 @@ def convert_coordinates(self, latitude_str, longitude_str):
4041
longitude = -longitude
4142

4243
return latitude, longitude
43-
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from apps.weather.models import RegionalWeather
22
from rest_framework import serializers
33

4+
45
class RegionalWeatherSerializer(serializers.ModelSerializer):
56
class Meta:
67
model = RegionalWeather
7-
exclude = ['location_latitude', 'location_longitude']
8+
exclude = ['location_latitude', 'location_longitude']

src/backend/apps/weather/tasks.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
from collections import OrderedDict
21
import logging
3-
from apps.weather.models import RegionalWeather
4-
from apps.weather.serializers import RegionalWeatherSerializer
2+
53
from apps.feed.client import FeedClient
64
from apps.shared.enums import CacheKey
7-
from django.contrib.gis.geos import LineString, Point
5+
from apps.weather.models import RegionalWeather
86
from django.core.cache import cache
9-
from django.core.exceptions import ObjectDoesNotExist
107

118
logger = logging.getLogger(__name__)
9+
10+
1211
def populate_regional_weather_from_data(new_regional_weather_data):
1312
code = new_regional_weather_data.get('code')
1413
existing_record = RegionalWeather.objects.filter(code=code).first()

src/backend/apps/weather/tests/test_data/regional_weather_feed_list_of_one.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"code": "s0000341",
3-
"location_latitude": "58.66N",
4-
"location_longitude": "124.64W",
5-
"name": "Tetsa River (Provincial Park)",
6-
"region": "Muncho Lake Park - Stone Mountain Park",
7-
"observation_name": "observation",
8-
"observation_zone": "UTC",
9-
"observation_utc_offset": 0,
2+
"code": "s0000341",
3+
"location_latitude": "58.66N",
4+
"location_longitude": "124.64W",
5+
"name": "Tetsa River (Provincial Park)",
6+
"region": "Muncho Lake Park - Stone Mountain Park",
7+
"observation_name": "observation",
8+
"observation_zone": "UTC",
9+
"observation_utc_offset": 0,
1010
"observation_text_summary": "Friday January 19, 2024 at 15:00 UTC",
1111
"conditions": {
1212
"condition": "clear",
@@ -104,7 +104,8 @@
104104
},
105105
"TextSummary": "A few flurries"
106106
}
107-
}],
107+
}
108+
],
108109
"hourly_forecast_group": [
109110
{
110111
"Lop": {
@@ -169,5 +170,6 @@
169170
"Period": null
170171
},
171172
"HourlyTimeStampUtc": "2024-01-23T23:00:00.000Z"
172-
}]
173-
}
173+
}
174+
]
175+
}

0 commit comments

Comments
 (0)