From dd35d910b5e65126bac282ac45786102adb31b66 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 9 Apr 2014 17:56:08 -0300 Subject: [PATCH] unplugging places from groups works --- luncho/blueprints/groups.py | 30 +++++++++++++++++++----------- tests/group_tests.py | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/luncho/blueprints/groups.py b/luncho/blueprints/groups.py index 86d48af..7598d7f 100644 --- a/luncho/blueprints/groups.py +++ b/luncho/blueprints/groups.py @@ -144,7 +144,7 @@ def create_group(): id=new_group.id) -@groups.route('/', methods=['PUT']) +@groups.route('/', methods=['PUT']) @ForceJSON() @auth def update_group(group_id): @@ -204,7 +204,7 @@ def update_group(group_id): return jsonify(status='OK') -@groups.route('/', methods=['DELETE']) +@groups.route('/', methods=['DELETE']) @auth def delete_group(group_id): """*Authenticated request* @@ -243,7 +243,7 @@ def delete_group(group_id): group_users = Blueprint('group_users', __name__) -@group_users.route('/users/', methods=['PUT']) +@group_users.route('/users/', methods=['PUT']) @ForceJSON(required=['usernames']) @auth def add_users_to_group(group_id): @@ -304,7 +304,7 @@ def add_users_to_group(group_id): not_found=unknown) -@group_users.route('/users/', methods=['GET']) +@group_users.route('/users/', methods=['GET']) @auth def list_group_members(group_id): """*Authenticated request* @@ -359,7 +359,7 @@ def list_group_members(group_id): group_places = Blueprint('group_places', __name__) -@group_places.route('/places/', methods=['GET']) +@group_places.route('/places/', methods=['GET']) @auth def get_group_places(group_id): """*Authenticated request* @@ -407,7 +407,7 @@ def get_group_places(group_id): places=places) -@group_places.route('/places/', methods=['POST']) +@group_places.route('/places/', methods=['POST']) @ForceJSON(required=['places']) @auth def group_add_places(group_id): @@ -477,7 +477,8 @@ def group_add_places(group_id): rejected=rejected) -@group_places.route('/places//', methods=['DELETE']) +@group_places.route('/places//', + methods=['DELETE']) @auth def group_remove_place(group_id, place_id): """*Authenticated request* @@ -504,10 +505,17 @@ def group_remove_place(group_id, place_id): if not group: raise ElementNotFoundException('Group') - index = None - try: - index = group.places.index(place_id) - except ValueError: + index = -1 + LOG.debug('Places: {places}'.format(places=group.places)) + for (pos, place) in enumerate(group.places): + LOG.debug('Place {pos} = {place} (search {search})'.format( + pos=pos, place=place.id, search=place_id)) + if place.id == place_id: + index = pos + break + + LOG.debug('Index: {index}'.format(index=index)) + if index == -1: raise ElementNotFoundException('Place') del group.places[index] diff --git a/tests/group_tests.py b/tests/group_tests.py index 06219d5..49a4681 100644 --- a/tests/group_tests.py +++ b/tests/group_tests.py @@ -499,5 +499,28 @@ class TestPlacesInGroup(LunchoTests): self.assertTrue('places' in json) self.assertEquals(place.id, json['places'][0]['id']) + def test_delete_place(self): + """Delete a place from a group.""" + group = self._group() + place = self._place() + group.places.append(place) + server.db.session.commit() + + group_id = group.id + place_id = place.id + + url = '/group/{group_id}/places/{place_id}/'.format( + group_id=group_id, place_id=place_id) + rv = self.delete(url, + token=self.user.token) + + self.assertJsonOk(rv) + + # check if it was really removed in the database + group = Group.query.get(group_id) + for place in group.places: + if place.id == place_id: + self.fail('Place still connected to group') + if __name__ == '__main__': unittest.main()