TG-60 : Ajout de l'API GroupAPI

This commit is contained in:
Clément ARNAUDEAU 2017-03-17 09:08:50 +01:00
parent 65ea8579f9
commit 88f96a21dd
5 changed files with 175 additions and 4 deletions

View File

@ -55,3 +55,41 @@ GET -> Getting specified user infos
Out:
200 -> USER = <USER_OBJECT>|null : Dictionary containing user infos or null
########################
GroupAPI (api/group)
########################
POST -> Create a group if it not already exists
In:
name = Name of the group (must be unique)
year = Parameter setting the year
class_short = Parameter setting the short name of the class
class_long = Parameter setting the full name of the class
department = Parameter setting the name of the class's department
resp_id = UID of the group's responsible
sec_id = UID of the group's secretary
Out:
200 -> GID = <GROUP_ID> : The group already exists with the id GROUP_ID
201 -> GID = <GROUP_ID> : The group has been successfully created with the id GROUP_ID
400 -> ERROR = "One or more parameters are missing" : Bad request
400 -> ERROR = "The user with id <USER_ID> doesn't exists !" : The given USER_ID for resp_id or sec_id is not found
PUT -> Modify an existing group
In: (Suffix = /bygid/<GROUP_ID>)
name = Name of the group (must be unique)
year = Parameter setting the year
class_short = Parameter setting the short name of the class
class_long = Parameter setting the full name of the class
department = Parameter setting the name of the class's department
resp_id = UID of the group's responsible
sec_id = UID of the group's secretary
Out:
200 -> GID = <GROUP_ID> : The group has been modified sucessfully with the id GROUP_ID
400 -> ERROR = "One or more parameters are missing !" : Bad request
400 -> ERROR = "The user with id <USER_ID> doesn't exists !" : The given USER_ID for resp_id or sec_id is not found
405 -> ERROR = "This group doesn't exists !" : Bad GROUP_ID provided
405 -> ERROR = "A group with this name already exists !" : A group with this name already exists
GET -> Getting specified group infos
In: (Suffixes = /bygid/<GROUP_ID> | /byname/<GROUP_NAME> )
Out:
200 -> GROUP = <GROUP_OBJECT>|null : Dictionary containing group infos or null

109
backend/app/api/GroupAPI.py Normal file
View File

@ -0,0 +1,109 @@
import os
from flask_restful import Resource, request
from app.config import Config
from app.core import app
from app.model import *
from app.utils import checkParams
class GroupAPI(Resource):
"""
Group Api Resource
"""
def post(self):
args = request.get_json(cache=False, force=True)
if not checkParams(['name', 'year', 'class_short', 'class_long', 'department', 'resp_id', 'sec_id'], args):
return {"ERROR": "One or more parameters are missing !"}, 400
name = args['name']
year = args['year']
class_short = args['class_short']
class_long = args['class_long']
department = args['department']
resp_id = args['resp_id']
sec_id = args['sec_id']
res_dir = Config.BASE_RESSOURCES_DIR + name + "/"
group = getGroup(name=name)
if group is not None:
return {"GID": group["id"]}, 200
user = getUser(uid=resp_id)
if user is None:
return {"ERROR": "The user with id " + str(resp_id) + " does not exists !"}, 400
else:
if "2" not in user['role'].split('-'):
role = user['role'] + "-2"
query = USER.update().values(role=role).where(USER.c.id == resp_id)
query.execute()
user = getUser(uid=sec_id)
if user is None:
return {"ERROR": "The user with id " + str(sec_id) + " does not exists !"}, 400
else:
if "1" not in user['role'].split('-'):
role = user['role'] + "-1"
query = USER.update().values(role=role).where(USER.c.id == sec_id)
query.execute()
query = GROUP.insert().values(name=name, year=year, class_short=class_short, class_long=class_long,
department=department, resp_id=resp_id, sec_id=sec_id, ressources_dir=res_dir)
res = query.execute()
os.mkdir(res_dir)
return {"GID": res.lastrowid}, 201
def put(self, gid):
args = request.get_json(cache=False, force=True)
if not checkParams(['name', 'year', 'class_short', 'class_long', 'department', 'resp_id', 'sec_id'], args):
return {"ERROR": "One or more parameters are missing !"}, 400
name = args['name']
year = args['year']
class_short = args['class_short']
class_long = args['class_long']
department = args['department']
resp_id = args['resp_id']
sec_id = args['sec_id']
res_dir = app.config['BASE_RESSOURCES_DIR'] + name + "/"
group = getGroup(gid=gid)
if group is None:
return {"ERROR": "This group does not exists !"}, 405
group = getGroup(name=name)
if group is None:
return {"ERROR": "A group with this name already exists !"}, 405
user = getUser(uid=resp_id)
if user is None:
return {"ERROR": "The user with id " + str(resp_id) + " does not exists !"}, 400
else:
if "2" not in user['role'].split('-'):
role = user['role'] + "-2"
query = USER.update().values(role=role).where(USER.c.id == resp_id)
query.execute()
user = getUser(uid=sec_id)
if user is None:
return {"ERROR": "The user with id " + str(sec_id) + " does not exists !"}, 400
else:
if "1" not in user['role'].split('-'):
role = user['role'] + "-1"
query = USER.update().values(role=role).where(USER.c.id == sec_id)
query.execute()
query = GROUP.update().values(name=name, year=year, class_short=class_short, class_long=class_long,
department=department, resp_id=resp_id, sec_id=sec_id, ressources_dir=res_dir) \
.where(GROUP.c.id == gid)
res = query.execute()
os.mkdir(res_dir)
return {"GID": gid}, 200
def get(self, gid=0, name=""):
if gid > 0:
return {'GROUP': getGroup(gid=gid)}, 200
elif name != "":
return {'GROUP': getGroup(name=name)}, 200

View File

@ -1,9 +1,9 @@
from flask import session
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from app.model import *
from app.core import cas
from app.model import *
class LoginAPI(Resource):
"""

View File

@ -50,3 +50,25 @@ def isUserAllowed(uid):
.filter(or_(tutorship_class.student_id == uid, group_class.resp_id == uid))
res = query.all()
return res is not None and len(res) > 0
def getGroup(gid=0, name=""):
res = None
if gid == 0 and name == "":
raise Exception("getUser must be called with one argument !")
else:
if gid != 0:
res = db.session.query(group_class).get(gid)
elif name != "":
query = GROUP.select(GROUP.c.name == name)
rows = query.execute()
res = rows.first()
if res is not None:
return {"id": res.id, "name": res.name, "year": res.year, "class_short": res.class_short,
"class_long": res.class_long, "department": res.department, "resp_id": getUser(uid=res.resp_id),
"sec_id": getUser(uid=res.sec_id), "ressources_dir": res.ressources_dir}
else:
return None

View File

@ -1,12 +1,14 @@
from app.api.GroupAPI import GroupAPI
from app.api.LoginAPI import LoginAPI
from app.api.UserAPI import UserAPI
from app.api.UserInfoAPI import UserInfoAPI
from app.api.exampleapi import SomeApi
from app.api.loginAPI import LoginAPI
from app.core import api
# Some Api resource
api.add_resource(SomeApi, '/api/someapi', '/api/someapi/<int:id>')
api.add_resource(LoginAPI, '/api/login')
api.add_resource(UserInfoAPI, '/api/userInfo')
api.add_resource(UserAPI, '/api/user', '/api/user/byuid/<int:uid>', '/api/user/bylogin/<string:login>',
'/api/user/byemail/<string:email>')
api.add_resource(UserInfoAPI, '/api/userInfo')
api.add_resource(GroupAPI, '/api/group', '/api/group/bygid/<int:gid>', '/api/group/byname/<string:name>')