From 88f96a21dd22c5dc475a9a83de3e72bb7ea42039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20ARNAUDEAU?= Date: Fri, 17 Mar 2017 09:08:50 +0100 Subject: [PATCH] TG-60 : Ajout de l'API GroupAPI --- API_Interfaces.txt | 38 +++++++ backend/app/api/GroupAPI.py | 109 +++++++++++++++++++ backend/app/api/{loginAPI.py => LoginAPI.py} | 4 +- backend/app/model.py | 22 ++++ backend/app/urls.py | 6 +- 5 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 backend/app/api/GroupAPI.py rename backend/app/api/{loginAPI.py => LoginAPI.py} (95%) diff --git a/API_Interfaces.txt b/API_Interfaces.txt index 4760495..cc876a7 100644 --- a/API_Interfaces.txt +++ b/API_Interfaces.txt @@ -55,3 +55,41 @@ GET -> Getting specified user infos Out: 200 -> USER = |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 = : The group already exists with the id GROUP_ID + 201 -> GID = : 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 doesn't exists !" : The given USER_ID for resp_id or sec_id is not found + +PUT -> Modify an existing group + In: (Suffix = /bygid/) + 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 = : 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 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/ | /byname/ ) + Out: + 200 -> GROUP = |null : Dictionary containing group infos or null \ No newline at end of file diff --git a/backend/app/api/GroupAPI.py b/backend/app/api/GroupAPI.py new file mode 100644 index 0000000..172ce89 --- /dev/null +++ b/backend/app/api/GroupAPI.py @@ -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 diff --git a/backend/app/api/loginAPI.py b/backend/app/api/LoginAPI.py similarity index 95% rename from backend/app/api/loginAPI.py rename to backend/app/api/LoginAPI.py index cb7828d..bc3917a 100644 --- a/backend/app/api/loginAPI.py +++ b/backend/app/api/LoginAPI.py @@ -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): """ diff --git a/backend/app/model.py b/backend/app/model.py index c56ae96..ee4d4c6 100644 --- a/backend/app/model.py +++ b/backend/app/model.py @@ -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 diff --git a/backend/app/urls.py b/backend/app/urls.py index 7f8083e..7c9afc6 100644 --- a/backend/app/urls.py +++ b/backend/app/urls.py @@ -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/') api.add_resource(LoginAPI, '/api/login') +api.add_resource(UserInfoAPI, '/api/userInfo') api.add_resource(UserAPI, '/api/user', '/api/user/byuid/', '/api/user/bylogin/', '/api/user/byemail/') -api.add_resource(UserInfoAPI, '/api/userInfo') +api.add_resource(GroupAPI, '/api/group', '/api/group/bygid/', '/api/group/byname/')