def get_air_quality(request):
geolocator = Nominatim()
context = request['context']
entities = request['entities']
loc = first_entity_value(entities, 'location')
for context_key in (
'missingLocation',
'outOfGermany',
'subscribed',
'notSubscribed',
'location',
'station',
'stationId',
'lastAlertValue',
'lastAlertDate',
'kind',
'clean',
'notFound',
):
try:
del context[context_key]
except KeyError:
pass
if not loc:
context['missingLocation'] = True
else:
loc = geolocator.geocode(loc, language='en')
if not loc:
loc = geolocator.geocode('{}, Germany'.format(loc), language='en')
if loc:
closest_station = get_closest_station(loc.latitude, loc.longitude)
# is subscribed ?
if is_subscribed(request['session_id'], closest_station):
context['subscribed'] = True
else:
context['notSubscribed'] = True
# oldest alert we want
max_date = datetime.datetime.now() - datetime.timedelta(days=2)
last_alert = Alert.objects.filter(station=closest_station, report__date__gte=max_date).last()
context['location'] = loc.address
context['station'] = closest_station.name
context['stationId'] = closest_station.pk
cache.set(request['session_id'], closest_station.pk)
if last_alert:
context['lastAlertValue'] = last_alert.value
context['lastAlertDate'] = last_alert.report.date.strftime('%x')
context['kind'] = last_alert.report.kind
else:
context['clean'] = True
else:
context['notFound'] = True
return context
# Setup Actions
评论列表
文章目录