Skip to content

Commit

Permalink
DBC22-1484: cleanups and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd authored and fatbird committed Feb 13, 2024
1 parent e4f0154 commit c6493e8
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 256 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
- flake8-django==1.1.2
- flake8-pytest-style==1.6.0
- flake8-typing-imports==1.12.0
args: [ --max-line-length=88 ]
args: [ --max-line-length=128 ]
stages: [commit]
exclude: '^backend/\w+/migrations'
- repo: https://github.com/PyCQA/bandit
Expand Down
37 changes: 21 additions & 16 deletions src/backend/apps/feed/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
from urllib.parse import urljoin

import httpx
from apps.feed.constants import DIT, INLAND_FERRY, OPEN511, WEBCAM
from apps.feed.constants import DIT, INLAND_FERRY, OPEN511, WEBCAM, REGIONAL_WEATHER, REGIONAL_WEATHER_AREAS
import requests
from apps.feed.constants import (
DIT,
INLAND_FERRY,
OPEN511,
REGIONAL_WEATHER,
REGIONAL_WEATHER_AREAS,
WEBCAM,
)
from apps.feed.serializers import (
CarsClosureEventSerializer,
EventAPISerializer,
EventFeedSerializer,
FerryAPISerializer,
RegionalWeatherFeedSerializer,
RegionalWeatherSerializer,
RegionalWeatherAPISerializer,
WebcamAPISerializer,
WebcamFeedSerializer,
)
from django.conf import settings
from rest_framework.exceptions import ValidationError
import requests
from rest_framework.response import Response

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -149,7 +153,7 @@ def get_closures_dict(self):
{"format": "json", "limit": 500}
)

return {event["id"]:True for event in events if event["closed"]}
return {event["id"]: True for event in events if event["closed"]}

# Ferries
def get_ferries_list(self):
Expand Down Expand Up @@ -252,13 +256,13 @@ def get_regional_weather_list_feed(self, resource_type, resource_name, serialize
'condition': condition,
'temperature_units': temperature_units,
'temperature_value': temperature_value,
'visibility_units' : visibility_units,
'visibility_value' : visibility_value,
'wind_speed_units' : wind_speed_units,
'visibility_units': visibility_units,
'visibility_value': visibility_value,
'wind_speed_units': wind_speed_units,
'wind_speed_value': wind_speed_value,
'wind_gust_units' : wind_gust_units,
'wind_gust_value' : wind_gust_value,
'wind_direction' : wind_direction,
'wind_gust_units': wind_gust_units,
'wind_gust_value': wind_gust_value,
'wind_direction': wind_direction,
}

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

hourly_forecast_group_data = data.get("HourlyForecastGroup", {})
hourly_forecast_group = hourly_forecast_group_data.get("HourlyForecasts") if hourly_forecast_group_data else None
hourly_forecast_group = hourly_forecast_group_data.get(
"HourlyForecasts") if hourly_forecast_group_data else None

regional_weather_data = {
'code': code,
Expand All @@ -296,7 +301,8 @@ def get_regional_weather_list_feed(self, resource_type, resource_name, serialize
'hourly_forecast_group': hourly_forecast_group,
}

serializer = serializer_cls(data=regional_weather_data, many=isinstance(regional_weather_data, list))
serializer = serializer_cls(data=regional_weather_data,
many=isinstance(regional_weather_data, list))
json_objects.append(regional_weather_data)

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


def get_regional_weather_list(self):
return self.get_regional_weather_list_feed(
REGIONAL_WEATHER, 'regionalweather', RegionalWeatherSerializer,
{"format": "json", "limit": 500}
)
)
2 changes: 2 additions & 0 deletions src/backend/apps/shared/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from apps.cms.models import Ferry
from apps.event.models import Event
from apps.weather.models import RegionalWeather
from apps.webcam.models import Webcam
from django.core.cache import cache
from django.test import TestCase
Expand Down Expand Up @@ -46,3 +47,4 @@ def tearDown(self):
Webcam.objects.all().delete()
Event.objects.all().delete()
Ferry.objects.all().delete()
RegionalWeather.objects.all().delete()
3 changes: 1 addition & 2 deletions src/backend/apps/weather/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
class WeatherAdmin(ModelAdmin):
readonly_fields = ('id', )

admin.site.register(RegionalWeather, WeatherAdmin)


admin.site.register(RegionalWeather, WeatherAdmin)
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from apps.weather.tasks import populate_all_regional_weather_data
from django.core.management.base import BaseCommand


class Command(BaseCommand):
def handle(self, *args, **options):
populate_all_regional_weather_data()
6 changes: 3 additions & 3 deletions src/backend/apps/weather/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point


class RegionalWeather(BaseModel):
location = models.PointField(null=True)
code = models.CharField(max_length=10, null=True)
Expand All @@ -24,12 +25,12 @@ def get_forecasts(self):

def __str__(self):
return f"Regional Forecast for {self.pk}"

def save(self, *args, **kwargs):
latitude, longitude = self.convert_coordinates(str(self.location_latitude), str(self.location_longitude))
self.location = Point(longitude, latitude)
super().save(*args, **kwargs)

def convert_coordinates(self, latitude_str, longitude_str):
latitude = float(latitude_str[:-1])
longitude = float(longitude_str[:-1])
Expand All @@ -40,4 +41,3 @@ def convert_coordinates(self, latitude_str, longitude_str):
longitude = -longitude

return latitude, longitude

3 changes: 2 additions & 1 deletion src/backend/apps/weather/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from apps.weather.models import RegionalWeather
from rest_framework import serializers


class RegionalWeatherSerializer(serializers.ModelSerializer):
class Meta:
model = RegionalWeather
exclude = ['location_latitude', 'location_longitude']
exclude = ['location_latitude', 'location_longitude']
9 changes: 4 additions & 5 deletions src/backend/apps/weather/tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from collections import OrderedDict
import logging
from apps.weather.models import RegionalWeather
from apps.weather.serializers import RegionalWeatherSerializer

from apps.feed.client import FeedClient
from apps.shared.enums import CacheKey
from django.contrib.gis.geos import LineString, Point
from apps.weather.models import RegionalWeather
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist

logger = logging.getLogger(__name__)


def populate_regional_weather_from_data(new_regional_weather_data):
code = new_regional_weather_data.get('code')
existing_record = RegionalWeather.objects.filter(code=code).first()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"code": "s0000341",
"location_latitude": "58.66N",
"location_longitude": "124.64W",
"name": "Tetsa River (Provincial Park)",
"region": "Muncho Lake Park - Stone Mountain Park",
"observation_name": "observation",
"observation_zone": "UTC",
"observation_utc_offset": 0,
"code": "s0000341",
"location_latitude": "58.66N",
"location_longitude": "124.64W",
"name": "Tetsa River (Provincial Park)",
"region": "Muncho Lake Park - Stone Mountain Park",
"observation_name": "observation",
"observation_zone": "UTC",
"observation_utc_offset": 0,
"observation_text_summary": "Friday January 19, 2024 at 15:00 UTC",
"conditions": {
"condition": "clear",
Expand Down Expand Up @@ -104,7 +104,8 @@
},
"TextSummary": "A few flurries"
}
}],
}
],
"hourly_forecast_group": [
{
"Lop": {
Expand Down Expand Up @@ -169,5 +170,6 @@
"Period": null
},
"HourlyTimeStampUtc": "2024-01-23T23:00:00.000Z"
}]
}
}
]
}
Loading

0 comments on commit c6493e8

Please sign in to comment.