From f68964a70685265d65c5d2164a4356054fce67f8 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 8 Apr 2014 10:19:54 -0300 Subject: [PATCH] delete places --- luncho/blueprints/places.py | 44 +++++++++++++++++++++++++++++++++++++ tests/place_tests.py | 7 +++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/luncho/blueprints/places.py b/luncho/blueprints/places.py index 5b4a55f..54f82d8 100644 --- a/luncho/blueprints/places.py +++ b/luncho/blueprints/places.py @@ -14,6 +14,8 @@ from luncho.helpers import ForceJSON from luncho.exceptions import AccountNotVerifiedException from luncho.exceptions import ElementNotFoundException +from luncho.exceptions import UserIsNotAdminException +from luncho.exceptions import NewMaintainerDoesNotExistException places = Blueprint('places', __name__) @@ -126,6 +128,8 @@ def update_place(placeId): HTTP/1.1 200 OK Content-Type: text/json + { "status": "OK" } + **Request not in JSON format (400)**: :py:class:`RequestMustBeJSONException` @@ -165,3 +169,43 @@ def update_place(placeId): db.session.commit() return jsonify(status='OK') + + +@places.route('/', methods=['DELETE']) +@auth +def delete_place(placeId): + """*Authenticated request* Delete the place. The user must be + the maintainer of the place to delete it. + + **Success (200)**: + + .. sourcecode:: http + + HTTP/1.1 200 OK + Content-Type: text/json + + { "status": "OK" } + + **User is not administrator of the group (403)**: + :py:class:`UserIsNotAdminException` + + **User not found (via token) (404)**: + :py:class:`UserNotFoundException` + + **The place does not exist (404)**: + :py:class:`ElementNotFoundException` + + **Authorization required (412)**: + :py:class:`AuthorizationRequiredException` + """ + place = Place.query.get(placeId) + if not place: + raise ElementNotFoundException('Place') + + if not place.owner == request.user.username: + raise UserIsNotAdminException() + + db.session.delete(place) + db.session.commit() + + return jsonify(status='OK') diff --git a/tests/place_tests.py b/tests/place_tests.py index be66116..31c0c5f 100644 --- a/tests/place_tests.py +++ b/tests/place_tests.py @@ -7,7 +7,6 @@ from json import loads from luncho import server -from luncho.server import User from luncho.server import Place from base import LunchoTests @@ -87,6 +86,12 @@ class TestExistingPlaces(LunchoTests): place = Place.query.get(placeId) self.assertEqual(place.owner, 'newUser') + def test_delete_place(self): + """Delete a place.""" + rv = self.delete('/place/{placeId}/'.format(placeId=self.place.id), + token=self.user.token) + self.assertJsonOk(rv) + if __name__ == '__main__': unittest.main()