This commit is contained in:
parent
3e331ab8cc
commit
0294ad3a66
3 changed files with 75 additions and 17 deletions
5
.github/workflows/ci.yaml
vendored
5
.github/workflows/ci.yaml
vendored
|
@ -1,6 +1,9 @@
|
||||||
name: ci
|
name: ci
|
||||||
|
|
||||||
on: push
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|
|
@ -1,26 +1,49 @@
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
import requests
|
import requests
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
API_KEY = 'a7cc162fc60a76d2e31461071634b8ce'
|
API_KEY = 'a7cc162fc60a76d2e31461071634b8ce'
|
||||||
|
|
||||||
|
def get_weather_data(city):
|
||||||
|
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric&lang=en'
|
||||||
|
logger.info(f"[weather_request] city={city}")
|
||||||
|
response = requests.get(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
logger.debug(f"[weather_response] city={city} response={data}")
|
||||||
|
return {
|
||||||
|
'city': city,
|
||||||
|
'temperature': data['main']['temp'],
|
||||||
|
'description': data['weather'][0]['description'],
|
||||||
|
'icon': data['weather'][0]['icon'],
|
||||||
|
}
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
weather_data = None
|
weather_data = None
|
||||||
error = None
|
error = None
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == "POST":
|
||||||
city = request.POST.get('city')
|
city = request.POST.get("city")
|
||||||
if city:
|
start = time.time()
|
||||||
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric&lang=eng'
|
|
||||||
response = requests.get(url)
|
|
||||||
if response.status_code == 200:
|
|
||||||
data = response.json()
|
|
||||||
weather_data = {
|
|
||||||
'city': city,
|
|
||||||
'temperature': data['main']['temp'],
|
|
||||||
'description': data['weather'][0]['description'],
|
|
||||||
'icon': data['weather'][0]['icon']
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
error = 'City not found'
|
|
||||||
|
|
||||||
return render(request, 'weather/index.html', {'weather': weather_data, 'error': error})
|
try:
|
||||||
|
weather_data = get_weather_data(city)
|
||||||
|
duration = time.time() - start
|
||||||
|
logger.info(f"[weather_success] city={city} duration={duration:.3f}s temp={weather_data['temperature']}")
|
||||||
|
|
||||||
|
except requests.HTTPError as http_err:
|
||||||
|
error = f"HTTP error: {http_err}"
|
||||||
|
logger.warning(f"[weather_http_error] city={city} error={http_err}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error = "Something went wrong."
|
||||||
|
logger.exception(f"[weather_unexpected_error] city={city} error={e}")
|
||||||
|
|
||||||
|
return render(request, 'weather/index.html', {
|
||||||
|
'weather': weather_data,
|
||||||
|
'error': error,
|
||||||
|
})
|
||||||
|
|
|
@ -122,3 +122,35 @@ STATIC_URL = 'static/'
|
||||||
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
|
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
|
LOGGING = {
|
||||||
|
'version': 1,
|
||||||
|
'disable_existing_loggers': False,
|
||||||
|
'formatters': {
|
||||||
|
'standard': {
|
||||||
|
'format': '[%(levelname)s] %(asctime)s %(name)s: %(message)s',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'handlers': {
|
||||||
|
'console': {
|
||||||
|
'class': 'logging.StreamHandler',
|
||||||
|
'formatter': 'standard',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'root': {
|
||||||
|
'handlers': ['console'],
|
||||||
|
'level': 'INFO',
|
||||||
|
},
|
||||||
|
'loggers': {
|
||||||
|
'django': {
|
||||||
|
'handlers': ['console'],
|
||||||
|
'level': 'WARNING',
|
||||||
|
'propagate': False,
|
||||||
|
},
|
||||||
|
'weather': {
|
||||||
|
'handlers': ['console'],
|
||||||
|
'level': 'INFO',
|
||||||
|
'propagate': False,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue