Skip to content

Commit

Permalink
Merge pull request #91 from HomebyTwo/feature/hb2-87
Browse files Browse the repository at this point in the history
HB2-87 add update button to refresh route from remote
  • Loading branch information
drixselecta committed Nov 16, 2020
2 parents e574378 + 7144a34 commit f2495da
Show file tree
Hide file tree
Showing 22 changed files with 1,069 additions and 767 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,10 @@
# Change Log
## [0.14.0] - 2020-11-12 update routes from remote
- Update route button from the route page
- Redirect to route update after Switzerland Mobility login
- Manage permissions for route update
- Remove Garmin upload interface for now

## [0.13.0] - 2020-11-08 import places from geonames.org
- Places can be imported from geonames.org for any country
- Import SwissNAMES3D from CSV file instead of shapefile
Expand Down
2 changes: 2 additions & 0 deletions config/settings/base.py
Expand Up @@ -39,6 +39,7 @@
"django.contrib.gis",
"django.contrib.humanize",
"social_django",
"rules",
"widget_tweaks",
"djgeojson",
"leaflet",
Expand Down Expand Up @@ -220,6 +221,7 @@

AUTHENTICATION_BACKENDS = (
"social_core.backends.strava.StravaOAuth",
"rules.permissions.ObjectPermissionBackend",
"django.contrib.auth.backends.ModelBackend",
)

Expand Down
21 changes: 12 additions & 9 deletions homebytwo/importers/decorators.py
Expand Up @@ -49,7 +49,7 @@ def remote_connection(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
try:
response = view_func(request, *args, **kwargs)
return view_func(request, *args, **kwargs)

except ConnectionError as error:
message = (
Expand Down Expand Up @@ -78,17 +78,20 @@ def _wrapped_view(request, *args, **kwargs):
# redirect to Switzerland Mobility log-in page
login_url = reverse("switzerland_mobility_login")

# add route_id as a GET param if trying to import a route
# add GET param if the athlete wanted to to import or update a route
match = request.resolver_match

# the athlete wanted to import a route
if match.url_name == "import_route":
route_id = match.kwargs["source_id"]
params = urlencode({"route_id": route_id})
params = urlencode({"import": match.kwargs["source_id"]})
return redirect(f"{login_url}?{params}")

# the athlete wanted to update a route
if match.app_name == "routes" and match.url_name == "update":
params = urlencode({"update": match.kwargs["pk"]})
return redirect(f"{login_url}?{params}")
else:
return redirect(login_url)

# no exception, return the response from the decorated view
else:
return response
# the athlete wanted something else
return redirect(login_url)

return _wrapped_view
64 changes: 29 additions & 35 deletions homebytwo/importers/forms.py
@@ -1,13 +1,13 @@
from json import dumps as json_dumps

import requests
from django import forms
from django.conf import settings
from django.contrib import messages
from django.contrib.gis.geos import LineString, Point

import gpxpy
from django.http import HttpRequest
from pandas import DataFrame
from requests import Session, codes
from requests import codes, HTTPError

from homebytwo.routes.models import Route
from homebytwo.routes.utils import get_distances
Expand Down Expand Up @@ -35,12 +35,11 @@ class SwitzerlandMobilityLogin(forms.Form):
),
)

def retrieve_authorization_cookie(self, request):
def login_to_switzerland_mobility(self, request: HttpRequest) -> bool:
"""
Retrieves auth cookies from Switzerland Mobility
and returns cookies or False
The cookies are required to display a user's list of saved routes.
saves auth cookies from Switzerland Mobility
returns a boolean value for success
Example response from the Switzerland Mobility login URL:
{
'loginErrorMsg': '',
Expand All @@ -64,34 +63,29 @@ def retrieve_authorization_cookie(self, request):
"password": self.cleaned_data["password"],
}

with Session() as session:

# Try to login to map.wanderland.ch
login_request = session.post(login_url, data=json_dumps(credentials))

if login_request.status_code == codes.ok:

# log-in was successful, return cookies
if login_request.json()["loginErrorCode"] == 200:
cookies = dict(login_request.cookies)
message = "Successfully logged-in to Switzerland Mobility"
messages.success(request, message)
return cookies

# log-in failed
else:
message = login_request.json()["loginErrorMsg"]
messages.error(request, message)
return False

# Some other server error
else:
message = (
"Error %s: logging to Switzerland Mobility. "
"Try again later" % login_request.status_code
)
messages.error(request, message)
return False
# Try to login to Switzerland Mobility
response = requests.post(login_url, data=credentials)

# log-in successful, save cookies to the session
if response.status_code == 200 and response.json()["loginErrorCode"] == 200:
request.session["switzerland_mobility_cookies"] = dict(response.cookies)
message = "Successfully logged-in to Switzerland Mobility"
messages.success(request, message)
return True

# response ok, but login failed
if response.status_code == codes.ok:
message = response.json()["loginErrorMsg"]
messages.error(request, message)
return False

# Some other HTTP error
try:
response.raise_for_status()
except HTTPError as error:
message = f"Error while logging-in to Switzerland Mobility. {error}. "
messages.error(request, message)
return False


class GpxUploadForm(forms.Form):
Expand Down
21 changes: 20 additions & 1 deletion homebytwo/importers/tests/test_gpx_import.py
Expand Up @@ -3,9 +3,15 @@

from pandas import DataFrame
from pytest import approx
from pytest_django.asserts import assertRedirects, assertTemplateUsed
from pytest_django.asserts import (
assertContains,
assertNotContains,
assertRedirects,
assertTemplateUsed,
)

from ...routes.models import Route
from ...routes.tests.factories import RouteFactory
from ..forms import GpxUploadForm


Expand Down Expand Up @@ -69,3 +75,16 @@ def test_upload_gpx_view_empty(athlete, client, uploaded_file):
url = reverse("upload_gpx")
response = client.post(url, data={"gpx": gpx_file})
assertTemplateUsed(response, "importers/index.html")


def test_get_gpx_route_no_update_button(athlete, client):
route = RouteFactory(data_source="homebytwo", athlete=athlete)
response = client.get(route.get_absolute_url())
assertContains(response, route.edit_url)
assertNotContains(response, route.get_absolute_url("update"))


def test_get_update_gpx_route(athlete, client):
route = RouteFactory(data_source="homebytwo", athlete=athlete)
response = client.get(route.update_url)
assert response.status_code == 404

0 comments on commit f2495da

Please sign in to comment.