TG-59 : Ajout de l'API UserAPI + passage du role en VARCHAR
This commit is contained in:
55
backend/app/api/UserAPI.py
Normal file
55
backend/app/api/UserAPI.py
Normal file
@ -0,0 +1,55 @@
|
||||
from flask_restful import Resource, request
|
||||
|
||||
from app.model import *
|
||||
from app.utils import checkParams
|
||||
|
||||
|
||||
class UserAPI(Resource):
|
||||
"""
|
||||
User Api Resource
|
||||
"""
|
||||
|
||||
def post(self):
|
||||
args = request.get_json(cache=False, force=True)
|
||||
if not checkParams(['CASid', 'role'], args):
|
||||
return {"ERROR": "One or more parameters are missing !"}, 400
|
||||
|
||||
CASid = args['CASid']
|
||||
role = args['role']
|
||||
email = self.getEmailFromCAS(CASid)
|
||||
phone = None
|
||||
user = getUser(login=CASid)
|
||||
if user is not None:
|
||||
return {"UID": user["id"]}, 200
|
||||
|
||||
query = USER.insert().values(login=CASid, email=email, role=role, phone=phone)
|
||||
res = query.execute()
|
||||
return {"UID": res.lastrowid}, 201
|
||||
|
||||
def put(self, uid):
|
||||
args = request.get_json(cache=False, force=True)
|
||||
if not checkParams(['CASid', 'role', 'email', 'phone'], args):
|
||||
return {"ERROR": "One or more parameters are missing !"}, 400
|
||||
|
||||
if getUser(uid=uid) is None:
|
||||
return {"ERROR": "This user doesn't exists !"}, 405
|
||||
|
||||
CASid = args['CASid']
|
||||
role = args['role']
|
||||
email = args['email']
|
||||
phone = args['phone']
|
||||
query = USER.update().values(login=CASid, email=email, role=role, phone=phone).where(USER.c.id == uid)
|
||||
query.execute()
|
||||
return {"UID": uid}, 200
|
||||
|
||||
def get(self, uid=0, login="", email=""):
|
||||
if uid > 0:
|
||||
return {'USER': getUser(uid=uid)}, 200
|
||||
elif login != "":
|
||||
return {'USER': getUser(login=login)}, 200
|
||||
elif email != "":
|
||||
return {'USER': getUser(email=email)}, 200
|
||||
|
||||
@staticmethod
|
||||
def getEmailFromCAS(CASid):
|
||||
return ""
|
Reference in New Issue
Block a user