Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,4 @@
)

OSMCHA_URL = env('OSMCHA_URL', default='https://osmcha.org')
OSM_API_USER_AGENT = {"User-Agent": "OSMCha osmcha-django v4"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO "v4" would ideally get read from __version__.py or similar somewhere in the repository, so we don't forget to update this string when v5 is released. But I grepped and it looks like the current version isn't actually stored anywhere in the codebase (just in the git tags) so I'm fine with hard-coding for now. I'll try to make a mental note that this is something that needs updating next time there's a major version bump. 🙂

10 changes: 7 additions & 3 deletions osmchadjango/changeset/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def import_replications(start, end):

def get_last_replication_id():
"""Get the id of the last replication file available on Planet OSM."""
state = requests.get("{}state.yaml".format(settings.OSM_PLANET_BASE_URL)).content
state = requests.get(
"{}state.yaml".format(settings.OSM_PLANET_BASE_URL),
headers=settings.OSM_API_USER_AGENT
).content
state = yaml.load(state, Loader)
return state.get("sequence")

Expand Down Expand Up @@ -132,11 +135,12 @@ def __init__(self, user, changeset_id):
def post_comment(self, message=None):
"""Post comment to changeset."""
response = self.client.request(
'POST',
"POST",
self.url,
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote(message)).encode("utf-8"),
client_id=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
)
if response.status_code == 200:
print(
Expand Down
3 changes: 3 additions & 0 deletions osmchadjango/changeset/tests/test_comment_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class MockResponse():
"{}/api/0.6/changeset/{}/comment/".format(
settings.OSM_SERVER_URL, self.harmful_changeset.id
),
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote(message)).encode("utf-8"),
client_id=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
Expand Down Expand Up @@ -105,6 +106,7 @@ class MockResponse():
"https://www.openstreetmap.org/api/0.6/changeset/{}/comment/".format(
self.good_changeset.id
),
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote(message)).encode("utf-8"),
client_id=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
Expand Down Expand Up @@ -137,6 +139,7 @@ class MockResponse():
"https://www.openstreetmap.org/api/0.6/changeset/{}/comment/".format(
self.changeset.id
),
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote(message)).encode("utf-8"),
client_id=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
Expand Down
1 change: 1 addition & 0 deletions osmchadjango/changeset/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class MockRequest():
mock_oauth_client.assert_called_with(
"POST",
"https://www.openstreetmap.org/api/0.6/changeset/123456/comment/",
headers=settings.OSM_API_USER_AGENT,
data="text={}".format(quote("Reviewed in OSMCha and set as GOOD!")).encode(
"utf-8"
),
Expand Down
11 changes: 4 additions & 7 deletions osmchadjango/users/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ParseError

from django.conf import settings

from social_django.models import UserSocialAuth
Expand All @@ -25,9 +22,9 @@ def update_user_name(user):
"""
try:
uid = user.social_auth.get(provider='openstreetmap-oauth2').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')
url = f'{settings.OSM_SERVER_URL}/api/0.6/user/{uid}.json'
data = requests.get(url, headers=settings.OSM_API_USER_AGENT).json()
display_name = data['user']['display_name']
if user.name != display_name:
user.name = display_name
user.save(update_fields=['name'])
Expand All @@ -36,5 +33,5 @@ def update_user_name(user):
print(
'User {} does not have a social_auth instance.'.format(user.username)
)
except ParseError:
except requests.exceptions.JSONDecodeError:
print('It was not possible to update user with uid {}.'.format(uid))
1 change: 1 addition & 0 deletions osmchadjango/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def get_access_token(self, code):
token_url=self.token_url,
code=code,
client_secret=settings.SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET,
headers=settings.OSM_API_USER_AGENT,
)

def get_user_token(self, request, access_token, *args, **kwargs):
Expand Down