Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add weather is forecast, update cron job, add logging for weather #213

Merged
merged 4 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ jobs:
- name: Black Lint
run: poetry run black --line-length 120 --exclude '/migrations/' --check apps project

- name: Isort Lint
run: poetry run isort --check-only --diff apps project -l 120

- name: Ruff Lint
run: poetry run ruff check .

Expand Down
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ repos:
- id: black


- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)

- repo: https://github.com/compilerla/conventional-pre-commit
rev: 'v2.1.1'
hooks:
Expand Down
3 changes: 2 additions & 1 deletion apps/weather/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

@admin.register(Weather)
class WeatherAdmin(admin.ModelAdmin):
list_filter = ["date_time", "location"]
list_filter = ["date_time", "location", "is_forecast"]
read_only_fields = ["is_forecast", "created", "updated"]
14 changes: 13 additions & 1 deletion apps/weather/management/commands/populate_weather.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import logging

import requests
from django.contrib.gis.geos import Point
from django.core.management.base import BaseCommand
from django.utils.timezone import datetime, now
from pytz import utc
from weather.models import Weather

# Get an instance of a logger
logger = logging.getLogger(__name__)


class Command(BaseCommand):
"""Command for populating weather into the database.
Expand All @@ -18,7 +25,9 @@ class Command(BaseCommand):
def handle(self, *args, **options):
# TODO get lat/lon from database.
for lat, lon in [(52.52, 13.41), (52.40, -2.01)]:
logger.debug(f"Fetching data for {lat}, {lon}")
resp = requests.get(self.url.format(lat, lon)).json()
time_now = now()

for time, temp, apparent_temp, rain, weather_code, cloud_cover in zip(
resp["hourly"]["time"],
Expand All @@ -28,15 +37,18 @@ def handle(self, *args, **options):
resp["hourly"]["weathercode"],
resp["hourly"]["cloudcover"],
):
tz_aware_time = datetime.fromisoformat(time)
tz_aware_time = utc.localize(tz_aware_time)
# this is bad! optimise later!
Weather.objects.update_or_create(
date_time=time,
date_time=tz_aware_time,
location=Point(lat, lon),
defaults={
"temperature": temp,
"apparent_temperature": apparent_temp,
"rain": rain,
"cloud_cover": cloud_cover,
"weather_code": weather_code,
"is_forecast": tz_aware_time > time_now,
},
)
17 changes: 17 additions & 0 deletions apps/weather/migrations/0003_weather_is_forecast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-04-18 17:02

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("weather", "0002_rename_apparent_temperate_weather_apparent_temperature"),
]

operations = [
migrations.AddField(
model_name="weather",
name="is_forecast",
field=models.BooleanField(default=False),
),
]
17 changes: 17 additions & 0 deletions apps/weather/migrations/0004_set_default_weather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import migrations
from django.utils.timezone import now


def update_weather(apps, schema_editor):
Weather = apps.get_model("weather", "Weather")
for weather in Weather.objects.all():
weather.is_forecast = weather.date_time > now()
weather.save()


class Migration(migrations.Migration):
dependencies = [
("weather", "0003_weather_is_forecast"),
]

operations = [migrations.RunPython(update_weather)]
17 changes: 17 additions & 0 deletions apps/weather/migrations/0005_alter_weather_is_forecast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-04-18 17:06

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("weather", "0004_set_default_weather"),
]

operations = [
migrations.AlterField(
model_name="weather",
name="is_forecast",
field=models.BooleanField(),
),
]
24 changes: 24 additions & 0 deletions apps/weather/migrations/0006_weather_created_weather_updated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2 on 2023-04-18 17:09

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("weather", "0005_alter_weather_is_forecast"),
]

operations = [
migrations.AddField(
model_name="weather",
name="created",
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name="weather",
name="updated",
field=models.DateTimeField(auto_now=True),
),
]
5 changes: 5 additions & 0 deletions apps/weather/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Weather(models.Model):
cloud_cover = models.FloatField()
weather_code = models.FloatField() # TODO there are choices.

is_forecast = models.BooleanField()

created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

class Meta:
constraints = [
models.UniqueConstraint(name="unique_weather_for_location_time", fields=["date_time", "location"])
Expand Down
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ parameterized = "^0.9.0"
psycopg = "^3.1.8"
whitenoise = "^6.4.0"
crispy-bootstrap5 = "^0.7"
pytz = "^2023.3"


[tool.poetry.group.local.dependencies]
Expand Down