Julio Biason
11 years ago
5 changed files with 204 additions and 24 deletions
@ -0,0 +1,103 @@ |
|||||||
|
#!/usr/bin/env python |
||||||
|
# -*- encoding: utf-8 -*- |
||||||
|
|
||||||
|
from flask import Blueprint |
||||||
|
from flask import request |
||||||
|
from flask import jsonify |
||||||
|
|
||||||
|
from luncho.server import Place |
||||||
|
from luncho.server import db |
||||||
|
|
||||||
|
from luncho.helpers import auth |
||||||
|
from luncho.helpers import ForceJSON |
||||||
|
|
||||||
|
from luncho.exceptions import AccountNotVerifiedException |
||||||
|
|
||||||
|
places = Blueprint('places', __name__) |
||||||
|
|
||||||
|
|
||||||
|
@places.route('', methods=['POST']) |
||||||
|
@ForceJSON(required=['name']) |
||||||
|
@auth |
||||||
|
def create_place(): |
||||||
|
"""*Authenticated request* Create a new place. The user becomes the |
||||||
|
maintainer of the place once it is created. |
||||||
|
|
||||||
|
**Example request**: |
||||||
|
|
||||||
|
.. sourcecode:: http |
||||||
|
|
||||||
|
{ "name": "<place name>" } |
||||||
|
|
||||||
|
**Success (200)** |
||||||
|
|
||||||
|
.. sourcecode:: http |
||||||
|
|
||||||
|
HTTP/1.1 200 OK |
||||||
|
Content-Type: text/json |
||||||
|
|
||||||
|
{ "status": "OK", "id": <new group id> } |
||||||
|
|
||||||
|
**User not found (via token) (404)**: |
||||||
|
:py:class:`UserNotFoundException` |
||||||
|
|
||||||
|
**Authorization required (412)**: |
||||||
|
:py:class:`AuthorizationRequiredException` |
||||||
|
|
||||||
|
**Account not verified (412)**: |
||||||
|
:py:class:`AccountNotVerifiedException` |
||||||
|
""" |
||||||
|
if not request.user.verified: |
||||||
|
raise AccountNotVerifiedException() |
||||||
|
|
||||||
|
json = request.get_json(force=True) |
||||||
|
new_place = Place(name=json['name'], owner=request.user.username) |
||||||
|
db.session.add(new_place) |
||||||
|
db.session.commit() |
||||||
|
|
||||||
|
return jsonify(status='OK', |
||||||
|
id=new_place.id) |
||||||
|
|
||||||
|
|
||||||
|
@places.route('', methods=['GET']) |
||||||
|
@auth |
||||||
|
def get_places(): |
||||||
|
"""*Authenticated request* Return the list of places the user is the |
||||||
|
maintainer or belongs to one of the user's groups. |
||||||
|
|
||||||
|
**Success (200)** |
||||||
|
|
||||||
|
.. sourcecode:: http |
||||||
|
|
||||||
|
HTTP/1.1 200 OK |
||||||
|
Content-Type: text/json |
||||||
|
|
||||||
|
{ "status": "OK", "places": [ { "id": "<placeId>", |
||||||
|
"name": "<place name>", |
||||||
|
"maintainer": <true if the user is the |
||||||
|
group maintainer>}, |
||||||
|
...] } |
||||||
|
|
||||||
|
**User not found (via token) (404)**: |
||||||
|
:py:class:`UserNotFoundException` |
||||||
|
|
||||||
|
**Authorization required (412)**: |
||||||
|
:py:class:`AuthorizationRequiredException` |
||||||
|
""" |
||||||
|
user = request.user |
||||||
|
places = {} |
||||||
|
for group in user.groups: |
||||||
|
for place in group.places: |
||||||
|
maintainer = place.owner == user.username |
||||||
|
places[place.id] = {'id': place.id, |
||||||
|
'name': place.name, |
||||||
|
'maintainer': maintainer} |
||||||
|
|
||||||
|
for place in Place.query.filter_by(owner=user.username): |
||||||
|
maintainer = place.owner == user.username |
||||||
|
places[place.id] = {'id': place.id, |
||||||
|
'name': place.name, |
||||||
|
'maintainer': maintainer} |
||||||
|
|
||||||
|
return jsonify(status='OK', |
||||||
|
places=places.values()) |
@ -0,0 +1,51 @@ |
|||||||
|
#!/usr/bin/env python |
||||||
|
# -*- encoding: utf-8 -*- |
||||||
|
|
||||||
|
import unittest |
||||||
|
|
||||||
|
from json import loads |
||||||
|
|
||||||
|
from luncho import server |
||||||
|
|
||||||
|
from luncho.server import User |
||||||
|
|
||||||
|
from base import LunchoTests |
||||||
|
|
||||||
|
|
||||||
|
class TestPlaces(LunchoTests): |
||||||
|
"""Test places.""" |
||||||
|
|
||||||
|
def setUp(self): |
||||||
|
super(TestPlaces, self).setUp() |
||||||
|
self.user = User(username='test', |
||||||
|
fullname='Test User', |
||||||
|
passhash='hash') |
||||||
|
self.user.verified = True |
||||||
|
server.db.session.add(self.user) |
||||||
|
server.db.session.commit() |
||||||
|
self.user.get_token() |
||||||
|
|
||||||
|
def test_create_place(self): |
||||||
|
"""Try to create a place.""" |
||||||
|
request = {'name': 'New Place'} |
||||||
|
rv = self.post('/place/', |
||||||
|
request, |
||||||
|
token=self.user.token) |
||||||
|
self.assertJsonOk(rv) |
||||||
|
json = loads(rv.data) |
||||||
|
self.assertTrue('id' in json) |
||||||
|
|
||||||
|
def test_get_places(self): |
||||||
|
"""Try to get the user places.""" |
||||||
|
token = self.user.token |
||||||
|
self.test_create_place() # create a place |
||||||
|
rv = self.get('/place/', |
||||||
|
token=token) |
||||||
|
self.assertJsonOk(rv) |
||||||
|
json = loads(rv.data) |
||||||
|
self.assertTrue('places' in json) |
||||||
|
self.assertEqual(len(json['places']), 1) # just the new place |
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__': |
||||||
|
unittest.main() |
Loading…
Reference in new issue