diff --git a/README.rst b/README.rst index edc0f203..ca7cd1ce 100644 --- a/README.rst +++ b/README.rst @@ -60,6 +60,7 @@ DJANGO_NON_STAFF_USER_THROTTLE_RATE NON_STAFF_USER_THROTTLE_RATE 3/min OAUTH_REDIRECT_URI OAUTH_REDIRECT_URI http://localhost:8000/oauth-landing.html http://localhost:8000/oauth-landing.html OSMCHA_FRONTEND_VERSION OSMCHA_FRONTEND_VERSION oh-pages oh-pages DJANGO_ENABLE_CHANGESET_COMMENTS ENABLE_POST_CHANGESET_COMMENTS False False +DJANGO_OSM_COMMENTS_API_KEY OSM_COMMENTS_API_KEY '' '' ======================================= ================================= ========================================= =========================================== You can set each of these variables with: diff --git a/config/settings/common.py b/config/settings/common.py index f6058ea2..516d4348 100644 --- a/config/settings/common.py +++ b/config/settings/common.py @@ -324,6 +324,9 @@ # are reviewed ENABLE_POST_CHANGESET_COMMENTS = env('DJANGO_ENABLE_CHANGESET_COMMENTS', default=False) +# Authorization token to access the OSM-COMMENTS-API +OSM_COMMENTS_API_KEY = env('DJANGO_OSM_COMMENTS_API_KEY', default='') + # Your common stuff: Below this line define 3rd party library settings REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( diff --git a/osmchadjango/changeset/tests/test_changeset_views.py b/osmchadjango/changeset/tests/test_changeset_views.py index 4969e9ca..cd117d39 100644 --- a/osmchadjango/changeset/tests/test_changeset_views.py +++ b/osmchadjango/changeset/tests/test_changeset_views.py @@ -1263,3 +1263,52 @@ def test_set_harmful_by_staff_user(self): ) self.assertEqual(response.status_code, 200) self.assertEqual(Changeset.objects.filter(checked=True).count(), 5) + +class TestChangesetCommentsView(APITestCase): + def setUp(self): + self.changeset = HarmfulChangesetFactory( + id=55736682, + ) + HarmfulChangesetFactory(id=1343) + self.user = User.objects.create_user( + username='test', + password='password', + email='a@a.com', + ) + UserSocialAuth.objects.create( + user=self.user, + provider='openstreetmap', + uid='123123', + ) + + def test_unauthenticated_changeset_detail_response(self): + response = self.client.get( + reverse('changeset:comment-list', args=[self.changeset.id]) + ) + self.assertEqual(response.status_code, 401) + + def test_authenticated_changeset_detail_response(self): + self.client.login(username=self.user.username, password='password') + response = self.client.get( + reverse('changeset:comment', args=[self.changeset.id]) + ) + + self.assertEqual(response.status_code, 200) + self.assertTrue(len(response.data) > 0) + + def test_changeset_does_not_exist(self): + self.client.login(username=self.user.username, password='password') + response = self.client.get( + reverse('changeset:comment', args=[1234]) + ) + + self.assertEqual(response.status_code, 404) + + def test_changeset_without_comments(self): + self.client.login(username=self.user.username, password='password') + response = self.client.get( + reverse('changeset:comment', args=[1343]) + ) + + self.assertEqual(response.status_code, 200) + self.assertTrue(response.data, []) diff --git a/osmchadjango/changeset/urls.py b/osmchadjango/changeset/urls.py index bd4bcf03..71e0ce76 100644 --- a/osmchadjango/changeset/urls.py +++ b/osmchadjango/changeset/urls.py @@ -72,7 +72,7 @@ re_path( r'^changesets/(?P\w+)/comment/$', view=views.ChangesetCommentAPIView.as_view( - {'post': 'post_comment'} + {'post': 'post_comment', 'get': 'get_comments'} ), name='comment' ), diff --git a/osmchadjango/changeset/views.py b/osmchadjango/changeset/views.py index baca5d5c..e7b44dcf 100644 --- a/osmchadjango/changeset/views.py +++ b/osmchadjango/changeset/views.py @@ -24,6 +24,7 @@ from rest_framework_gis.pagination import GeoJsonPagination from rest_framework_csv.renderers import CSVRenderer from rest_framework.exceptions import APIException +import requests from .models import Changeset, UserWhitelist, SuspicionReasons, Tag from .filters import ChangesetFilter @@ -465,13 +466,27 @@ def get_queryset(self): class ChangesetCommentAPIView(ModelViewSet): - """Post a comment to a changeset in the OpenStreetMap website.""" queryset = Changeset.objects.all() permission_classes = (IsAuthenticated,) serializer_class = ChangesetCommentSerializer + @detail_route(methods=['get']) + def get_comments(self, request, pk): + "List comments received by a changeset on the OpenStreetMap website." + self.changeset = self.get_object() + headers = { + 'apiKey': settings.OSM_COMMENTS_API_KEY, + 'Content-Type': 'application/json' + } + data = requests.get( + 'https://osm-comments-api.mapbox.com/api/v1/changesets/{}'.format(pk), + headers=headers + ) + return data.get('properties').get('comments') + @detail_route(methods=['post']) def post_comment(self, request, pk): + "Post a comment to a changeset in the OpenStreetMap website." self.changeset = self.get_object() serializer = self.get_serializer(data=request.data) if serializer.is_valid():