Skip to content

Commit

Permalink
add view to list comments of a changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
willemarcel committed Oct 31, 2018
1 parent 6032358 commit ee9cb1a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': (
Expand Down
49 changes: 49 additions & 0 deletions osmchadjango/changeset/tests/test_changeset_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
)
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, [])
2 changes: 1 addition & 1 deletion osmchadjango/changeset/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
re_path(
r'^changesets/(?P<pk>\w+)/comment/$',
view=views.ChangesetCommentAPIView.as_view(
{'post': 'post_comment'}
{'post': 'post_comment', 'get': 'get_comments'}
),
name='comment'
),
Expand Down
17 changes: 16 additions & 1 deletion osmchadjango/changeset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit ee9cb1a

Please sign in to comment.