Private
Public Access
1
0

mocking request to weather API

This commit is contained in:
Sander Roosendaal
2020-02-08 09:03:34 +01:00
parent 3c7a89f822
commit 6bdf9216f2
11 changed files with 238 additions and 12743 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,138 +1,217 @@
amqp==2.5.2 amqp==2.4.2
anytree==2.8.0 apipkg==1.5
apparmor==2.13.3
appdirs==1.4.3 appdirs==1.4.3
asn1crypto==1.3.0 arcgis==1.6.0
attrs==19.3.0 arrow==0.13.1
asn1crypto==0.24.0
atomicwrites==1.3.0
attrs==19.1.0
backcall==0.1.0 backcall==0.1.0
beautifulsoup4==4.8.2 beautifulsoup4==4.7.1
billiard==3.6.1.0 billiard==3.6.0.0
bleach==3.1.0 bleach==3.1.0
bokeh==1.0.4
boto==2.49.0 boto==2.49.0
btrfsutil==1.1.1 braintree==3.55.0
CacheControl==0.12.6 cairocffi==1.0.2
case==1.5.3
catfish==1.4.13
celery==4.3.0 celery==4.3.0
ceph-volume==1.0.0 certifi==2019.3.9
cephfs==2.0.0 cffi==1.12.2
cephfs-shell==0.0.1
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4 chardet==3.0.4
colorama==0.4.3 Click==7.0
contextlib2==0.6.0 cloudpickle==1.2.2
cryptography==2.8 colorama==0.4.1
colorclass==2.2.0
cookies==2.2.1
coreapi==2.3.3
coreschema==0.0.4
coverage==4.5.3
cryptography==2.6.1
cycler==0.10.0 cycler==0.10.0
decorator==4.4.1 dask==2.6.0
defusedxml==0.6.0 decorator==4.4.0
distlib==0.3.0 defusedxml==0.5.0
distro==1.4.0 Django==2.1.7
django-analytical==2.5.0
django-async-messages==0.3.1
django-braces==1.13.0
django-classy-tags==0.8.0
django-cookie-law==2.0.1
django-cors-headers==2.5.2
django-countries==5.3.3
django-datetime-widget==0.9.3
django-debug-toolbar==2.0
django-extensions==2.1.6
django-htmlmin==0.11.0
django-leaflet==0.24.0
django-mailbox==4.8.0
django-oauth-toolkit==1.2.0
django-oauth2-provider==0.2.6.1
django-redis==4.10.0
django-rest-framework==0.1.0
django-rest-swagger==2.2.0
django-rq==1.3.1
django-rq-dashboard==0.3.3
django-ses==0.8.10
django-shell-plus==1.1.7
django-social-share==1.3.2
django-suit==0.2.26
django-suit-rq==1.0.1
django-tz-detect==0.2.9
djangorestframework==3.9.2
docopt==0.6.2 docopt==0.6.2
docutils==0.14
entrypoints==0.3 entrypoints==0.3
gufw==19.10.0 execnet==1.5.0
factory-boy==2.11.1
Faker==1.0.4
fastparquet==0.3.2
fitparse==1.1.0
Flask==1.0.2
fsspec==0.5.2
future==0.17.1
geocoder==1.38.1
geos==0.2.1
grpcio==1.26.0
grpcio-tools==1.26.0
holoviews==1.11.3
html5lib==1.0.1 html5lib==1.0.1
htmlmin==0.1.12 htmlmin==0.1.12
httplib2==0.14.0 HTMLParser==0.0.2
httplib2==0.12.1
hvplot==0.4.0
icalendar==4.0.3
idna==2.8 idna==2.8
importlib-metadata==1.4.0 image==1.5.27
ipykernel==5.1.3 importlib-resources==1.0.2
ipython==7.11.1 ipykernel==5.1.0
ipython-genutils==0.1.0 ipython==7.4.0
ipywidgets==7.5.1 ipython-genutils==0.2.0
Jade-Application-Kit==2.1.3 ipywidgets==7.4.2
jedi==0.15.1 iso8601==0.1.12
Jinja2==2.10.3 isodate==0.6.0
jsonschema==3.2.0 itsdangerous==1.1.0
jupyter-client==5.3.4 itypes==1.1.0
jedi==0.13.3
jeepney==0.4
Jinja2==2.10
json5==0.8.5
jsonschema==3.0.1
jupyter==1.0.0
jupyter-client==5.2.4
jupyter-console==6.0.0 jupyter-console==6.0.0
jupyter-core==4.6.1 jupyter-core==4.4.0
keyutils==0.6 jupyterlab==0.35.4
kiwisolver==1.1.0 jupyterlab-server==0.3.0
kombu==4.6.7 keyring==18.0.0
lensfun==0.3.2 kiwisolver==1.0.1
LibAppArmor==2.13.3 kombu==4.5.0
lightdm-gtk-greeter-settings==1.2.2 llvmlite==0.30.0
lockfile==0.12.2 lxml==4.3.2
louis==3.12.0 Markdown==3.0.1
lxml==4.4.2
MarkupSafe==1.1.1 MarkupSafe==1.1.1
matplotlib==3.1.2 matplotlib==3.0.3
menulibre==2.2.1 minify==0.1.4
MiniMockTest==0.5
mistune==0.8.4 mistune==0.8.4
more-itertools==8.1.0 mock==2.0.0
msgpack==0.6.2 more-itertools==6.0.0
mugshot==0.4.2 mpld3==0.3
nbconvert==5.6.1 mysqlclient==1.4.2.post1
nbconvert==5.4.1
nbformat==4.4.0 nbformat==4.4.0
newrelic==5.2.1.129
nose==1.3.7 nose==1.3.7
notebook==6.0.2 nose-parameterized==0.6.0
npyscreen==4.10.5 notebook==5.7.6
numpy==1.18.1 numba==0.46.0
openshot-qt==2.4.4 numpy==1.16.2
ordered-set==3.1.1 oauth2==1.9.0.post1
packaging==20.0 oauthlib==3.0.1
pacman-mirrors==4.16.2 openapi-codec==1.3.2
packaging==19.0
pandas==0.24.2
pandocfilters==1.4.2 pandocfilters==1.4.2
parso==0.5.2 param==1.8.2
pep517==0.6.0 parso==0.3.4
pexpect==4.7.0 pathspec==0.5.9
pbr==5.1.3
pexpect==4.6.0
pickleshare==0.7.5 pickleshare==0.7.5
Pillow==6.2.1 Pillow==5.4.1
pkgconfig==1.5.1 pip-upgrader==1.4.6
pkginfo==1.5.0.1 pluggy==0.9.0
ply==3.11 prometheus-client==0.6.0
progress==1.5 prompt-toolkit==2.0.9
prometheus-client==0.7.1 protobuf==3.11.1
prompt-toolkit==3.0.2 psycopg2==2.8.1
psutil==5.6.7
ptyprocess==0.6.0 ptyprocess==0.6.0
pycairo==1.18.2 py==1.8.0
pyarrow==0.15.0
pycairo==1.19.0
pycparser==2.19 pycparser==2.19
pycups==1.9.74 Pygments==2.3.1
pycurl==7.43.0.3 pyparsing==2.3.1
Pygments==2.5.2 pyrsistent==0.14.11
PyGObject==3.34.0 pyshp==2.1.0
pymongo==3.10.1 pytest==4.3.1
pyOpenSSL==19.1.0 pytest-django==3.4.8
pyparsing==2.4.6 pytest-forked==1.0.2
PyQt5==5.14.1 pytest-runner==4.4
PyQt5-sip==12.7.0 pytest-sugar==0.9.2
PyQtWebEngine==5.14.0 pytest-xdist==1.27.0
Pyro4==4.77 python-dateutil==2.8.0
pyrsistent==0.15.7 python-memcached==1.59
python-dateutil==2.8.1 python-twitter==3.5
python-distutils-extra==2.39 pytz==2018.9
pytoml==0.1.21 pyviz-comms==0.7.1
pytz==2019.3 pywin32-ctypes==0.2.0
pyxdg==0.26 pywinpty==0.5.5
PyYAML==5.3 PyYAML==5.1
pyzmq==18.1.1 pyzmq==18.0.1
rados==2.0.0 qtconsole==4.4.3
rbd==2.0.0 ratelim==0.1.6
redis==3.3.11 redis==3.2.1
requests==2.22.0 requests==2.21.0
retrying==1.3.3 requests-oauthlib==1.2.0
rgw==2.0.0 rowingdata==2.6.6
scipy==1.4.1 rowingphysics==0.5.0
rq==0.13.0
rules==2.1
scipy==1.2.1
SecretStorage==3.1.1
Send2Trash==1.5.0 Send2Trash==1.5.0
serpent==1.27 shell==1.0.1
six==1.13.0 shortuuid==0.5.0
soupsieve==1.9.5 simplejson==3.16.0
SQLAlchemy==1.3.12 six==1.12.0
team==1.0 soupsieve==1.8
terminado==0.8.3 SQLAlchemy==1.3.1
testpath==0.4.4 sqlparse==0.3.0
tornado==6.0.3 stravalib==0.10.2
traitlets==4.3.3 termcolor==1.1.0
udiskie==2.0.2 terminado==0.8.1
ufw==0.36 terminaltables==3.1.0
urllib3==1.25.7 testpath==0.4.2
text-unidecode==1.2
thrift==0.11.0
timezonefinder==4.0.1
tk==0.1.0
toolz==0.10.0
tornado==6.0.1
tqdm==4.31.1
traitlets==4.3.2
units==0.7
uritemplate==3.0.0
urllib3==1.24.1
VerbalExpressions==0.0.2
vine==1.3.0 vine==1.3.0
virtualenv==16.1.0 wcwidth==0.1.7
wcwidth==0.1.8
webencodings==0.5.1 webencodings==0.5.1
widgetsnbextension==3.5.1 Werkzeug==0.15.1
wrapt==1.11.2 widgetsnbextension==3.4.2
xlrd==1.2.0 xlrd==1.2.0
zipp==0.6.0 xmltodict==0.12.0
yamjam==0.1.7
yamllint==1.15.0
yuicompressor==2.4.8

View File

@@ -610,6 +610,7 @@ def mocked_requests(*args, **kwargs):
def __init__(self, json_data, status_code): def __init__(self, json_data, status_code):
self.json_data = json_data self.json_data = json_data
self.status_code = status_code self.status_code = status_code
self.ok = True
def json(self): def json(self):
return self.json_data return self.json_data

View File

@@ -242,9 +242,9 @@ class WorkoutViewTest(TestCase):
@patch('rowers.dataprep.create_engine') @patch('rowers.dataprep.create_engine')
@patch('rowers.dataprep.getsmallrowdata_db') @patch('rowers.dataprep.getsmallrowdata_db')
# @patch('rowers.weather.requests.get', side_effect=mocked_requests) @patch('rowers.weather.requests.get', side_effect=mocked_requests)
def test_waterworkout_view(self, def test_waterworkout_view(self,
mocked_sqlalchemy, mocked_getsmallrowdata_db): mocked_sqlalchemy, mocked_getsmallrowdata_db, mocked_requests):
login = self.c.login(username=self.u.username, password=self.password) login = self.c.login(username=self.u.username, password=self.password)
self.assertTrue(login) self.assertTrue(login)

View File

@@ -777,7 +777,7 @@ class PermissionsViewTests(TestCase):
login = self.c.login(username=self.ucoach.username, password=self.ucoachpassword) login = self.c.login(username=self.ucoach.username, password=self.ucoachpassword)
self.assertTrue(login) self.assertTrue(login)
url = reverse('rower_favoritecharts_view',kwargs={'id':self.ubasic.id}) url = reverse('rower_favoritecharts_view',kwargs={'userid':self.ubasic.id})
response = self.c.get(url) response = self.c.get(url)
self.assertEqual(response.status_code,200) self.assertEqual(response.status_code,200)
@@ -788,7 +788,7 @@ class PermissionsViewTests(TestCase):
login = self.c.login(username=self.ucoach.username, password=self.ucoachpassword) login = self.c.login(username=self.ucoach.username, password=self.ucoachpassword)
self.assertTrue(login) self.assertTrue(login)
url = reverse('rower_favoritecharts_view',kwargs={'id':self.ubasic.id}) url = reverse('rower_favoritecharts_view',kwargs={'userid':self.ubasic.id})
response = self.c.get(url) response = self.c.get(url)
self.assertEqual(response.status_code,403) self.assertEqual(response.status_code,403)

View File

@@ -2214,8 +2214,21 @@ def workout_smoothenpace_view(request,id=0,message="",successmessage=""):
if row == 0: if row == 0:
return HttpResponse("Error: CSV Data File Not Found") return HttpResponse("Error: CSV Data File Not Found")
pace = row.df[' Stroke500mPace (sec/500m)'].values try:
velo = 500./pace pace = row.df[' Stroke500mPace (sec/500m)'].values
velo = 500./pace
except KeyError:
messages.error(request,'Unable to find the data')
if previousurl:
url = previousurl
else:
url = reverse(r.defaultlandingpage,
kwargs = {
'id':id,
}
)
return HttpResponseRedirect(url)
if not 'originalvelo' in row.df: if not 'originalvelo' in row.df:
row.df['originalvelo'] = velo row.df['originalvelo'] = velo

View File

@@ -4,6 +4,7 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
import requests import requests
from requests.exceptions import ConnectionError
import json import json
from lxml import objectify,etree from lxml import objectify,etree
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
@@ -26,13 +27,16 @@ def get_airport_code(lat,lon):
newlon = a.iloc[0]['lon'] newlon = a.iloc[0]['lon']
distance = geo_distance(lat,lon,newlat,newlon) distance = geo_distance(lat,lon,newlat,newlon)
return airport_code,newlat,newlon,distance return airport_code,newlat,newlon,distance
# Get weather data from the DarkSky API # Get weather data from the DarkSky API
def get_weather_data(long,lat,unixtime): def get_weather_data(long,lat,unixtime):
url = "https://api.darksky.net/forecast/"+FORECAST_IO_KEY+"/" url = "https://api.darksky.net/forecast/"+FORECAST_IO_KEY+"/"
url += str(long)+","+str(lat)+","+str(unixtime) url += str(long)+","+str(lat)+","+str(unixtime)
s = requests.get(url) try:
s = requests.get(url)
except ConnectionError:
return 0
if s.ok: if s.ok:
return s.json() return s.json()
@@ -55,10 +59,14 @@ def get_metar_data(airportcode,unixtime):
except: except:
message = 'Failed to download METAR data' message = 'Failed to download METAR data'
return [0,0,message,'',''] return [0,0,message,'','']
if s.ok: if s.ok:
doc = etree.fromstring(s.content) try:
doc = etree.fromstring(s.content)
except AttributeError:
message = 'METAR data content error'
return [0,0,message,'',0]
lengte = len(doc.xpath('data/METAR/station_id')) lengte = len(doc.xpath('data/METAR/station_id'))
idnr = int(lengte/2) idnr = int(lengte/2)
try: try:
@@ -77,7 +85,7 @@ def get_metar_data(airportcode,unixtime):
wind_knots = float(wind_speed) wind_knots = float(wind_speed)
wind_ms = 0.514444*wind_knots wind_ms = 0.514444*wind_knots
wind_ms = int(10*wind_ms)/10. wind_ms = int(10*wind_ms)/10.
temperaturec = float(temp_c) temperaturec = float(temp_c)
temp_f = 32.+(9./5.)*temperaturec temp_f = 32.+(9./5.)*temperaturec
temp_f = str(int(10*temp_f)/10.) temp_f = str(int(10*temp_f)/10.)
@@ -87,17 +95,19 @@ def get_metar_data(airportcode,unixtime):
message += '. Wind: '+str(wind_ms)+' m/s ('+str(wind_knots)+' kt)' message += '. Wind: '+str(wind_ms)+' m/s ('+str(wind_knots)+' kt)'
message +='. Wind Bearing: '+str(windbearing)+' degrees' message +='. Wind Bearing: '+str(windbearing)+' degrees'
# message +='\n'+rawtext # message +='\n'+rawtext
return [wind_ms,windbearing,message,rawtext,timestamp] return [wind_ms,windbearing,message,rawtext,timestamp]
message = 'Failed to download METAR data' message = 'Failed to download METAR data'
return [0,0,message,'',timestamp] return [0,0,message,'',timestamp]
# Get wind data (and translate from knots to m/s) # Get wind data (and translate from knots to m/s)
def get_wind_data(lat,long,unixtime): def get_wind_data(lat,long,unixtime):
data = get_weather_data(lat,long,unixtime) data = get_weather_data(lat,long,unixtime)
summary = ''
temperature = 20
if data: if data:
try: try:
# we are getting wind in mph # we are getting wind in mph
@@ -107,11 +117,13 @@ def get_wind_data(lat,long,unixtime):
windspeed = 0 windspeed = 0
windbearing = 0 windbearing = 0
try: try:
airports = data['flags']['madis-stations'] airports = data['flags']['madis-stations']
except KeyError: except KeyError:
airports = ['unknown'] airports = ['unknown']
try: try:
temperature = data['currently']['temperature'] temperature = data['currently']['temperature']
# Temp is given in Fahrenheit, so convert to Celsius for Europeans # Temp is given in Fahrenheit, so convert to Celsius for Europeans
@@ -121,6 +133,7 @@ def get_wind_data(lat,long,unixtime):
temperature = 'unknown' temperature = 'unknown'
temperaturec = 'unknown' temperaturec = 'unknown'
try: try:
summary = data['currently']['summary'] summary = data['currently']['summary']
except KeyError: except KeyError:
@@ -128,15 +141,19 @@ def get_wind_data(lat,long,unixtime):
else: else:
windspeed = 0 windspeed = 0
windbearing = 0 windbearing = 0
summary = 'unknown'
airports = ['unknown']
temparature = 'unknown'
temperaturec = 'unknown'
message = 'Not able to get weather data' message = 'Not able to get weather data'
# apply Hellman's coefficient for neutral air above human # apply Hellman's coefficient for neutral air above human
# inhabitated areas # inhabitated areas
windspeed = windspeed*(0.1)**0.34 windspeed = windspeed*(0.1)**0.34
windspeed = 0.01*int(100*windspeed) windspeed = 0.01*int(100*windspeed)
timestamp = arrow.get(unixtime).isoformat() timestamp = arrow.get(unixtime).isoformat()
message = 'Summary for your location at '+timestamp+': '+summary message = 'Summary for your location at '+timestamp+': '+summary
message += '. Temperature '+str(temperature)+'F/'+str(temperaturec)+'C' message += '. Temperature '+str(temperature)+'F/'+str(temperaturec)+'C'