Skip to content

Instantly share code, notes, and snippets.

@noirbizarre
Last active August 29, 2015 14:18
Show Gist options
  • Save noirbizarre/fe30e4578f312853638f to your computer and use it in GitHub Desktop.
Save noirbizarre/fe30e4578f312853638f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from lisa_api import db, api, core
from flask.ext.restplus import Resource, fields
from lisa_api.models.users.user import Role, user_datastore
from lisa_api.lib.login import login_api
role_api = api.model('Role', {
'name': fields.String(required=True, description='Role name (must be unique)'),
'description': fields.String(required=True, description='Role description')
})
role_parser = api.parser()
role_parser.add_argument('name', type=str, required=True,
help='Role name (must be unique)', location='form')
role_parser.add_argument('description', type=str, required=True,
help='Role description', location='form')
@core.route('/role/<int:id>')
@api.doc(params={'id': 'The Role ID'})
@api.response(404, 'Role not found')
@api.response(401, 'Unauthorized access')
class RoleResource(Resource):
""" Show a single role item and lets you modify or delete it """
decorators = [login_api]
@api.marshal_with(role_api)
def get(self, id):
"""
This function return a single role object
:param id: The id of the role
:type id: int
:returns: a role object or a 404 string + int if role not found
:rtype: object or string + int
"""
role = Role.query.get(id)
if role is None:
api.abort(404, 'Role not found')
return role
@api.response(204, 'Role deleted')
def delete(self, id):
"""
This function delete the given role object
:param id: The id of the role
:type id: int
:returns: a 204 string + int or a 404 string + int if role is not found
:rtype: string + int
"""
role = Role.query.get(id)
if role is None:
api.abort(404, 'Role not found')
db.session.delete(role)
db.session.commit()
return 'Role has been deleted', 204
# TODO Bug on this method, swagger send a {id} instead of the true id
@api.doc('update_role', parser=role_parser)
@api.marshal_with(role_api, description="Role updated")
def put(self, id):
"""
This function modify a role object
:param id: The id of the role
:type id: int
:returns: a 200 string + int or a 404 string + int if role is not found
:rtype: string + int
"""
args = role_parser.parse_args()
role = Role.query.get(id)
if role is None:
api.abort(404, 'Role not found')
role.name = args['name']
role.description = args['description']
db.session.commit()
return role
@core.route('/role')
@api.response(404, 'Role not found')
@api.response(401, 'Unauthorized access')
class RoleListResource(Resource):
""" This class return all roles and is also responsible to handle the
creation of a role """
decorators = [login_api]
@api.marshal_list_with(role_api, description="Roles list")
def get(self):
"""
This function return all role objects
:return: a list of role objects
:rtype: list of role objects
"""
return Role.query.all()
@api.doc('create_role', parser=role_parser)
@api.marshal_with(role_api, code=201, description="Role added")
def post(self):
"""
This function create a role object
:returns: a 201 string + int
:rtype: string + int
"""
args = role_parser.parse_args()
role = user_datastore.find_or_create_role(name=args['name'],
description=args['description'])
db.session.commit()
return role, 201
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment