Skip to content

Commit

Permalink
Merge pull request #141 from willemarcel/develop
Browse files Browse the repository at this point in the history
add new fields to User model and endpoint to post comments to changesets
  • Loading branch information
willemarcel authored Feb 1, 2018
2 parents 6128ae2 + b67102d commit c1432e0
Show file tree
Hide file tree
Showing 17 changed files with 353 additions and 146 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ Change Log

Log of changes since the 2.0 version

[2.7.0] - 2018-02-01

- Disable automatic comments
- Add endpoint to allow user to post comments to changesets from inside OSMCha
- Modify user model to include message_good, message_bad and comment_feature fields

[2.6.0] - 2017-12-27

- Post comment to OSM Changeset page when a review is added to a changeset
- Post a comment to OSM Changeset page when a review is added to a changeset (to use it, set the ENV variable ``DJANGO_ENABLE_CHANGESET_COMMENTS`` to True)
- Return the correct username of users in views

[2.5.0] - 2017-10-09
Expand Down
13 changes: 6 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ How to login using the OAuth api
Frontend
^^^^^^^^

`osmcha-frontend https://github.com/mapbox/osmcha-frontend`_ is one web interface
`osmcha-frontend <https://github.com/mapbox/osmcha-frontend>`_ is a web interface
that you can use to interact with the API. We have a django management command
to get the last version of osmcha-frontend and serve it with the API.

Expand All @@ -148,7 +148,7 @@ Feature creation endpoint
^^^^^^^^^^^^^^^^^^^^^^^^^

The feature creation endpoint allows only admin users to create features. You can
use the admin site to create a token to the user.
use the admin site to create a token to a user.

Instances
---------
Expand All @@ -166,9 +166,8 @@ Deployment
Check the `Deploy <DEPLOY.rst>`_ file for instructions on how to deploy with Heroku and Dokku.


Management Commands
--------------------
Get in contact
---------------

1. Export a CSV of all harmful changesets

$ python manage.py generate_harmful_csv filename.csv
If you use, deploy or are interested in help to develop OSMCha, subscribe to our
`mailing list <https://lists.openstreetmap.org/listinfo/osmcha-dev>`_.
9 changes: 7 additions & 2 deletions osmchadjango/changeset/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json

from rest_framework.fields import ReadOnlyField, SerializerMethodField
from rest_framework.fields import ReadOnlyField, SerializerMethodField, CharField
from rest_framework.serializers import (
ModelSerializer, ListSerializer, BaseSerializer, PrimaryKeyRelatedField
ModelSerializer, ListSerializer, BaseSerializer, PrimaryKeyRelatedField,
Serializer
)
from rest_framework_gis.serializers import GeoFeatureModelSerializer

Expand Down Expand Up @@ -233,3 +234,7 @@ class ChangesetTagsSerializer(ModelSerializer):
class Meta:
model = Changeset
fields = ('tags',)


class ChangesetCommentSerializer(Serializer):
comment = CharField(max_length=1000)
46 changes: 2 additions & 44 deletions osmchadjango/changeset/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,6 @@ def __init__(self, user, changeset_id):
self.url = 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
changeset_id
)
self.good_changeset_message = """Hello!
I reviewed your changeset on OSMCha and it looks great!
Thank you very much for your contributions to OpenStreetMap!
#REVIEWED_GOOD #OSMCHA
Published using OSMCha: https://osmcha.mapbox.com/changesets/{}
""".format(changeset_id)
self.bad_changeset_message = """Hello!
Thank you very much for your contributions to OpenStreetMap!
I reviewed your changeset on OSMCha and found some errors or elements
that could be mapped in a better way. Feel free to message me
to know more about it or visit http://learnosm.org/ to get started.
#REVIEWED_BAD #OSMCHA
Published using OSMCha: https://osmcha.mapbox.com/changesets/{}
""".format(changeset_id)
self.unchecked_changeset_message = """Hello!
My previous review of your changeset was wrong, so I'm changing its
status to unreviewed on OSMCHA. Sorry for the error.
Published using OSMCha: https://osmcha.mapbox.com/changesets/{}
""".format(changeset_id)

def post_comment(self, message=None):
"""Post comment to changeset."""
Expand All @@ -152,31 +133,8 @@ def post_comment(self, message=None):
self.changeset_id
)
)
return {'success': True}
else:
print("""Some error occurred and it wasn't possible to post the
comment to the changeset {}.""".format(self.changeset_id))

def post_good_changeset_review(self, message=None):
"""Post a comment to a changeset reviewed as good. If the message is not
defined, it will use the default good changeset review message.
"""
if message is None:
message = self.good_changeset_message
self.post_comment(message)

def post_bad_changeset_review(self, message=None):
"""Post a comment to a changeset reviewed as bad. If the message is not
defined, it will use the default bad changeset review message.
"""
if message is None:
message = self.bad_changeset_message
self.post_comment(message)

def post_undo_changeset_review(self, message=None):
"""Post a comment to a changeset whose review was undone. If the message
is not defined, it will use the default unchecked changeset review
message.
"""
if message is None:
message = self.unchecked_changeset_message
self.post_comment(message)
return {'success': False}
39 changes: 3 additions & 36 deletions osmchadjango/changeset/tests/test_changeset_views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import json
import mock

from django.contrib.gis.geos import Polygon
from django.core.urlresolvers import reverse
from django.test import TestCase, override_settings

from social_django.models import UserSocialAuth
from rest_framework.test import APITestCase
import oauth2 as oauth

from ...users.models import User
from ...feature.tests.modelfactories import FeatureFactory
Expand Down Expand Up @@ -658,16 +656,12 @@ def test_set_harmful_changeset_get(self):
self.assertIsNone(self.changeset_2.check_user)
self.assertIsNone(self.changeset_2.check_date)

@override_settings(ENABLE_POST_CHANGESET_COMMENTS=True)
@mock.patch.object(oauth.Client, 'request')
def test_set_harmful_changeset_put(self, mock_oauth_client):
def test_set_harmful_changeset_put(self):
"""User can set a changeset of another user as harmful with a PUT request.
We can also set the tags of the changeset sending it as data.
"""
mock_oauth_client.return_value = [{'status': '200'}]
self.client.login(username=self.user.username, password='password')
data = {'tags': [self.tag_1.id, self.tag_2.id]}
# ENABLE POST CHANGESET COMMENTS for this test
response = self.client.put(
reverse('changeset:set-harmful', args=[self.changeset_2.pk]),
data
Expand All @@ -688,13 +682,6 @@ def test_set_harmful_changeset_put(self, mock_oauth_client):
self.tag_2,
self.changeset_2.tags.all()
)
mock_oauth_client.assert_called_with(
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.changeset_2.pk
),
method='POST',
body=mock.ANY
)

def test_set_harmful_changeset_with_invalid_tag_id(self):
"""Return a 400 error if a user try to add a invalid tag id to a changeset.
Expand Down Expand Up @@ -745,13 +732,10 @@ def test_set_good_changeset_get(self):
self.assertIsNone(self.changeset_2.check_user)
self.assertIsNone(self.changeset_2.check_date)

@override_settings(ENABLE_POST_CHANGESET_COMMENTS=True)
@mock.patch.object(oauth.Client, 'request')
def test_set_good_changeset_put(self, mock_oauth_client):
def test_set_good_changeset_put(self):
"""User can set a changeset of another user as good with a PUT request.
We can also set the tags of the changeset sending it as data.
"""
mock_oauth_client.return_value = [{'status': '200'}]
self.client.login(username=self.user.username, password='password')
data = {'tags': [self.tag_1.id, self.tag_2.id]}
response = self.client.put(
Expand All @@ -773,13 +757,6 @@ def test_set_good_changeset_put(self, mock_oauth_client):
self.tag_2,
self.changeset_2.tags.all()
)
mock_oauth_client.assert_called_with(
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.changeset_2.pk
),
method='POST',
body=mock.ANY
)

def test_set_good_changeset_with_invalid_tag_id(self):
"""Return a 400 error if a user try to add a invalid tag id to a changeset.
Expand Down Expand Up @@ -892,10 +869,7 @@ def test_unauthenticated_response(self):
self.assertEqual(self.harmful_changeset.tags.count(), 1)
self.assertIn(self.tag, self.harmful_changeset.tags.all())

@override_settings(ENABLE_POST_CHANGESET_COMMENTS=True)
@mock.patch.object(oauth.Client, 'request')
def test_uncheck_harmful_changeset(self, mock_oauth_client):
mock_oauth_client.return_value = [{'status': '200'}]
def test_uncheck_harmful_changeset(self):
self.client.login(username=self.user.username, password='password')
response = self.client.put(
reverse('changeset:uncheck', args=[self.harmful_changeset.pk]),
Expand All @@ -907,13 +881,6 @@ def test_uncheck_harmful_changeset(self, mock_oauth_client):
self.assertIsNone(self.harmful_changeset.check_user)
self.assertIsNone(self.harmful_changeset.check_date)
self.assertEqual(self.harmful_changeset.tags.count(), 1)
mock_oauth_client.assert_called_with(
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.harmful_changeset.pk
),
method='POST',
body=mock.ANY
)

def test_uncheck_good_changeset(self):
self.client.login(username=self.user.username, password='password')
Expand Down
143 changes: 143 additions & 0 deletions osmchadjango/changeset/tests/test_comment_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
from django.core.urlresolvers import reverse
from django.test import override_settings

from social_django.models import UserSocialAuth
from rest_framework.test import APITestCase
import oauth2 as oauth
import mock

from ...users.models import User
from ..models import Changeset
from .modelfactories import (
ChangesetFactory, GoodChangesetFactory, HarmfulChangesetFactory
)


class TestCommentChangesetAPIView(APITestCase):
def setUp(self):
self.user = User.objects.create_user(
username='test_2',
password='password',
email='[email protected]'
)
UserSocialAuth.objects.create(
user=self.user,
provider='openstreetmap',
uid='123123',
extra_data={
'id': '123123',
'access_token': {
'oauth_token': 'aaaa',
'oauth_token_secret': 'bbbb'
}
}
)
self.changeset = ChangesetFactory(id=31982802)
self.harmful_changeset = HarmfulChangesetFactory(id=31982803)
self.good_changeset = GoodChangesetFactory(id=31982804)

def test_comment_changeset_unauthenticated(self):
self.client.login(username=self.user.username, password='password')
comment = {'comment': 'Hello! I found an error in your edit'}
response = self.client.post(
reverse('changeset:comment', args=[self.harmful_changeset.id]),
data=comment
)

self.assertEqual(response.status_code, 403)

@override_settings(ENABLE_POST_CHANGESET_COMMENTS=True)
@mock.patch.object(oauth.Client, 'request')
def test_comment_harmful_changeset(self, mock_oauth_client):
mock_oauth_client.return_value = [{'status': '200'}]
self.client.login(username=self.user.username, password='password')
comment = {'comment': 'Hello! I found an error in your edit'}
message = """Hello! I found an error in your edit
---
#REVIEWED_BAD #OSMCHA
Published using OSMCha: https://osmcha.mapbox.com/changesets/31982803
"""
response = self.client.post(
reverse('changeset:comment', args=[self.harmful_changeset.id]),
data=comment)

self.assertEqual(response.status_code, 201)
mock_oauth_client.assert_called_with(
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.harmful_changeset.id
),
method='POST',
body='text={}'.format(message)
)

@override_settings(ENABLE_POST_CHANGESET_COMMENTS=True)
@mock.patch.object(oauth.Client, 'request')
def test_comment_good_changeset(self, mock_oauth_client):
mock_oauth_client.return_value = [{'status': '200'}]
self.client.login(username=self.user.username, password='password')
comment = {'comment': 'Hello! Awesome edit'}
message = """Hello! Awesome edit
---
#REVIEWED_GOOD #OSMCHA
Published using OSMCha: https://osmcha.mapbox.com/changesets/31982804
"""
response = self.client.post(
reverse('changeset:comment', args=[self.good_changeset.id]),
data=comment
)

self.assertEqual(response.status_code, 201)
mock_oauth_client.assert_called_with(
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.good_changeset.id
),
method='POST',
body='text={}'.format(message)
)

@override_settings(ENABLE_POST_CHANGESET_COMMENTS=True)
@mock.patch.object(oauth.Client, 'request')
def test_comment_unreviewed_changeset(self, mock_oauth_client):
"""Unreviewed changeset should not receive the #OSMCHA_(GOOD or BAD)
hashtag.
"""
mock_oauth_client.return_value = [{'status': '200'}]
self.client.login(username=self.user.username, password='password')
comment = {'comment': 'Hello! Do you know this area?'}
message = """Hello! Do you know this area?
---\n \n Published using OSMCha: https://osmcha.mapbox.com/changesets/31982802
"""
response = self.client.post(
reverse('changeset:comment', args=[self.changeset.id]),
data=comment
)

self.assertEqual(response.status_code, 201)
mock_oauth_client.assert_called_with(
'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format(
self.changeset.id
),
method='POST',
body='text={}'.format(message)
)

def test_comment_good_changeset_wrong_data(self):
self.client.login(username=self.user.username, password='password')
comment = {'message': 'Hello! Awesome edit'}
response = self.client.post(
reverse('changeset:comment', args=[self.good_changeset.id]),
data=comment
)

self.assertEqual(response.status_code, 400)

def test_comment_changeset_doesnt_exist(self):
"""Request should fail if the changeset id is not on our database."""
self.client.login(username=self.user.username, password='password')
comment = {'message': 'Hello! Awesome edit'}
response = self.client.post(
reverse('changeset:comment', args=[2323]),
data=comment
)

self.assertEqual(response.status_code, 404)
Loading

0 comments on commit c1432e0

Please sign in to comment.