Skip to content

Commit

Permalink
Make OSM server urls configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
willemarcel committed Sep 1, 2023
1 parent 24f0cf7 commit 59779c8
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 19 deletions.
7 changes: 7 additions & 0 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@
CACHALOT_TIMEOUT = 180
CACHALOT_ENABLED = True

# OSM SERVER SETTINGS
OSM_URL = env('OSM_SERVER_URL', default='https://www.openstreetmap.org')
OSM_PLANET_BASE_URL = env(
'OSM_PLANET_BASE_URL',
default='https://planet.openstreetmap.org/replication/changesets/'
)

# FRONTEND SETTINGS
# -----------------------------------------------------------------------------
# Version or any valid git branch tag of front-end code
Expand Down
11 changes: 5 additions & 6 deletions osmchadjango/changeset/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.postgres.indexes import GinIndex
from django.contrib.postgres.fields import JSONField
from django.utils.translation import ugettext, ugettext_lazy as _
from django.conf import settings

from ..users.models import User

Expand Down Expand Up @@ -98,24 +99,22 @@ def save(self, *args, **kwargs):

def osm_link(self):
"""Return the link to the changeset page on OSM website."""
return 'https://www.openstreetmap.org/changeset/{}'.format(self.id)
return '{}/changeset/{}'.format(settings.OSM_URL, self.id)

def josm_link(self):
"""Return link to open changeset in JOSM."""
josm_base = "http://127.0.0.1:8111/import?url="
changeset_url = "{}/{}/{}".format(
"https://www.openstreetmap.org/api/0.6/changeset",
changeset_url = "{}/api/0.6/changeset/{}/download".format(
settings.OSM_URL,
self.id,
"download"
)
return "{}{}".format(josm_base, changeset_url)

def id_link(self):
"""Return link to open the area of the changeset in iD editor."""
id_base = "https://www.openstreetmap.org/edit?editor=id#map=16"
if self.bbox:
centroid = [round(c, 5) for c in self.bbox.centroid.coords]
return "{}/{}/{}".format(id_base, centroid[1], centroid[0])
return "{}/edit?editor=id#map=16/{}/{}".format(settings.OSM_URL, centroid[1], centroid[0])
else:
return ""

Expand Down
8 changes: 4 additions & 4 deletions osmchadjango/changeset/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def get_filter_changeset_file(url, geojson_filter=settings.CHANGESETS_FILTER):
def format_url(n):
"""Return the url of a replication file."""
n = str(n)
base_url = 'https://planet.openstreetmap.org/replication/changesets/'
return join(base_url, '00%s' % n[0], n[1:4], '%s.osm.gz' % n[4:])
return join(settings.OSM_PLANET_BASE_URL, '00%s' % n[0], n[1:4], '%s.osm.gz' % n[4:])


@shared_task
Expand All @@ -73,7 +72,7 @@ def get_last_replication_id():
"""Get the id number of the last replication file available on Planet OSM.
"""
state = requests.get(
'https://planet.openstreetmap.org/replication/changesets/state.yaml'
'{}state.yaml'.format(settings.OSM_PLANET_BASE_URL)
).content
state = yaml.load(state)
return state.get('sequence')
Expand Down Expand Up @@ -113,7 +112,8 @@ def __init__(self, user, changeset_id):
resource_owner_key=user_token['oauth_token'],
resource_owner_secret=user_token['oauth_token_secret']
)
self.url = 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.url = '{}/api/0.6/changeset/{}/comment/'.format(
settings.OSM_URL,
changeset_id
)

Expand Down
12 changes: 7 additions & 5 deletions osmchadjango/changeset/tests/test_comment_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.urls import reverse
from django.test import override_settings
from django.conf import settings

from social_django.models import UserSocialAuth
from rest_framework.test import APITestCase
Expand Down Expand Up @@ -72,9 +73,10 @@ class MockResponse():
self.assertEqual(response.status_code, 201)
mock_oauth_client.assert_called_with(
'POST',
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.harmful_changeset.id
),
'{}/api/0.6/changeset/{}/comment/'.format(
settings.OSM_URL,
self.harmful_changeset.id
),
data='text={}'.format(quote(message)).encode('utf-8'),
json=None
)
Expand Down Expand Up @@ -102,7 +104,7 @@ class MockResponse():
self.assertEqual(response.status_code, 201)
mock_oauth_client.assert_called_with(
'POST',
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
'https://www.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.good_changeset.id
),
data='text={}'.format(quote(message)).encode('utf-8'),
Expand Down Expand Up @@ -133,7 +135,7 @@ class MockResponse():
self.assertEqual(response.status_code, 201)
mock_oauth_client.assert_called_with(
'POST',
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
'https://www.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.changeset.id
),
data='text={}'.format(quote(message)).encode('utf-8'),
Expand Down
4 changes: 2 additions & 2 deletions osmchadjango/changeset/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_changeset_comment_init(self):
changeset_comment = ChangesetCommentAPI(self.user, 123456)
self.assertEqual(
changeset_comment.url,
'https://api.openstreetmap.org/api/0.6/changeset/123456/comment/'
'https://www.openstreetmap.org/api/0.6/changeset/123456/comment/'
)
self.assertIsInstance(changeset_comment.client, OAuth1Session)
self.assertEqual(
Expand All @@ -114,7 +114,7 @@ class MockRequest():

mock_oauth_client.assert_called_with(
'POST',
'https://api.openstreetmap.org/api/0.6/changeset/123456/comment/',
'https://www.openstreetmap.org/api/0.6/changeset/123456/comment/',
data='text={}'.format(quote('Reviewed in OSMCha and set as GOOD!')).encode('utf-8'),
json=None
)
4 changes: 3 additions & 1 deletion osmchadjango/users/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ParseError

from django.conf import settings

from social_django.models import UserSocialAuth

import requests
Expand All @@ -23,7 +25,7 @@ def update_user_name(user):
"""
try:
uid = user.social_auth.get(provider='openstreetmap').uid
url = 'https://www.openstreetmap.org/api/0.6/user/{}/'.format(uid)
url = '{}/api/0.6/user/{}/'.format(settings.OSM_SERVER_URL, uid)
data = ET.fromstring(requests.get(url).content)
display_name = data.find('user').get('display_name')
if user.name != display_name:
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ redis>=2.10.5

# Your custom requirements go here
PyYAML==5.4.1
osmcha==0.8.6
osmcha==0.9.0

# git python is required by the frontend management command
GitPython==3.1.18

0 comments on commit 59779c8

Please sign in to comment.