Merge pull request #97 from sagidayan/API_Dev

added data verification
This commit is contained in:
Sagi Dayan 2015-06-29 20:20:51 +03:00
commit 4dda9175d9
6 changed files with 192 additions and 94 deletions

View file

@ -123,7 +123,12 @@ def joinCampus(token, campusId):
user = get_user_by_token(token) user = get_user_by_token(token)
campus = Campus.get_by_id(int(campusId)) try:
campus = Campus.get_by_id(int(campusId))
except Exception as e:
return bad_request("Bad id format")
if campus is None: if campus is None:
return bad_request("No such course") return bad_request("No such course")
@ -294,7 +299,12 @@ def getCampusesByUserID(token, id):
if user is None: if user is None:
return forbidden("Invalid Token") return forbidden("Invalid Token")
user = get_user_by_id(int(id))
try:
user = get_user_by_id(int(id))
except Exception as e:
return bad_request("Bad id format")
if user is None: if user is None:
return no_content("No User") return no_content("No User")
@ -351,7 +361,11 @@ def deleteCampus(token,campusId):
return forbidden("Invalid token or not a lecturer!") return forbidden("Invalid token or not a lecturer!")
user = get_user_by_token(token) user = get_user_by_token(token)
camp = Campus.get_by_id(int(campusId))
try:
camp = Campus.get_by_id(int(campusId))
except Exception as e:
return bad_request("Bad id format")
if camp is None: if camp is None:
return bad_request("no such campus") return bad_request("no such campus")

View file

@ -145,7 +145,11 @@ def joinCourse(token, courseId):
if user is None: if user is None:
return bad_request("Wrong user Token") return bad_request("Wrong user Token")
course = Course.get_by_id(int(courseId)) try:
course = Course.get_by_id(int(courseId))
except Exception as e:
return bad_request("Bad id format")
if course is None: if course is None:
return bad_request("No such course") return bad_request("No such course")
@ -205,7 +209,12 @@ def getAllCoursesByCampus(token, campusId):
arr = [] arr = []
query = Course.all() query = Course.all()
query.filter("campusId = ", int(campusId))
try:
query.filter("campusId = ", int(campusId))
except Exception as e:
return bad_request("Bad id format")
for c in query.run(): for c in query.run():
arr.append(dict(json.loads(c.to_JSON()))) arr.append(dict(json.loads(c.to_JSON())))
@ -258,7 +267,11 @@ def getUserCoursesByCampus(token, campusId):
if user is None: if user is None:
return bad_request("Bad user Token") return bad_request("Bad user Token")
campus = Campus.get_by_id(int(campusId)) try:
campus = Campus.get_by_id(int(campusId))
except Exception as e:
return bad_request("Bad id format")
if campus is None: if campus is None:
return bad_request("No such Campus") return bad_request("No such Campus")
@ -318,6 +331,11 @@ def getCoursesByUser(token, userId):
if user is None: if user is None:
return bad_request("Bad user Token") return bad_request("Bad user Token")
try:
otherUser = User.get_by_id(int(userId))
except Exception as e:
return bad_request("Bad id format")
otherUser = User.get_by_id(int(userId)) otherUser = User.get_by_id(int(userId))
if otherUser is None: if otherUser is None:
return bad_request("Bad user Id") return bad_request("Bad user Id")
@ -349,9 +367,9 @@ def getCoursesByUser(token, userId):
@course_routes.route('/api/courses/deleteCourse/<string:token>/<string:courseid>', methods=['DELETE']) @course_routes.route('/api/courses/deleteCourse/<string:token>/<string:courseId>', methods=['DELETE'])
@auto.doc() @auto.doc()
def deleteCourse(token, courseid): def deleteCourse(token, courseId):
""" """
<span class="card-title">This Call will delete a specific Course</span> <span class="card-title">This Call will delete a specific Course</span>
<br> <br>
@ -383,7 +401,11 @@ def deleteCourse(token, courseid):
return forbidden("Invalid token or not a lecturer!") return forbidden("Invalid token or not a lecturer!")
user = get_user_by_token(token) user = get_user_by_token(token)
c = Course.get_by_id(int(courseid))
try:
c = Course.get_by_id(int(courseId))
except Exception as e:
return bad_request("Bad id format")
if c is None: if c is None:
return bad_request("no such course") return bad_request("no such course")

View file

@ -153,7 +153,11 @@ def getMessagesByGroup(token, groupId):
arr = [] arr = []
query = Message.all() query = Message.all()
query.filter("groupId = ", int(groupId))
try:
query.filter("groupId = ", int(groupId))
except Exception as e:
return bad_request("Bad id format")
for m in query.run(): for m in query.run():
msgDic = dict(json.loads(m.to_JSON())) msgDic = dict(json.loads(m.to_JSON()))
@ -222,7 +226,12 @@ def deleteMessage(token, msgId):
if user is None: if user is None:
return bad_request("No such User") return bad_request("No such User")
msg = Message.get_by_id(int(msgId)) try:
msg = Message.get_by_id(int(msgId))
except Exception as e:
return bad_request("Bad id format")
if msg is None: if msg is None:
return bad_request("No such Message") return bad_request("No such Message")

View file

@ -86,7 +86,7 @@ def create_project(token):
print e print e
pass pass
project.info = get_github_data(project.gitRepository) project.info = json.dumps(get_github_data(project.gitRepository))
db.put(project) db.put(project)
#update user projects list #update user projects list
@ -94,8 +94,6 @@ def create_project(token):
db.put(user) db.put(user)
db.save db.save
t1 = threading.Thread(target=updateProjectInfo,args=(project.key().id(),))
t1.start()
return Response(response=project.to_JSON(), return Response(response=project.to_JSON(),
status=200, status=200,
@ -134,7 +132,12 @@ def joinProject(token, projectId):
if user is None: if user is None:
return bad_request("Wrong user Token") return bad_request("Wrong user Token")
project = Project.get_by_id(int(projectId)) try:
project = Project.get_by_id(int(projectId))
except Exception as e:
return bad_request("Bad id format")
if project is None: if project is None:
return bad_request("No such Project") return bad_request("No such Project")
@ -193,7 +196,11 @@ def getProjectsByCourse(token, courseId):
arr = [] arr = []
query = Project.all() query = Project.all()
query.filter("courseId = ", int(courseId))
try:
query.filter("courseId = ", int(courseId))
except Exception as e:
return bad_request("Bad id format")
for p in query.run(): for p in query.run():
proj = dict(json.loads(p.to_JSON())) proj = dict(json.loads(p.to_JSON()))
@ -305,7 +312,11 @@ def deleteProject(token,projectId):
user = get_user_by_token(token) user = get_user_by_token(token)
if user is None: if user is None:
return bad_request("Bad user Token") return bad_request("Bad user Token")
p = Project.get_by_id(int(projectId))
try:
p = Project.get_by_id(int(projectId))
except Exception as e:
return bad_request("Bad id format")
if p is None: if p is None:
return bad_request("no such Project") return bad_request("no such Project")

View file

@ -192,7 +192,11 @@ def getAllTasksByCourse(token, courseId):
arr = [] arr = []
query = Task.all() query = Task.all()
query.filter("courseId = ", courseId)
try:
query.filter("courseId = ", int(courseId))
except Exception as e:
return bad_request("Bad id format")
for t in query.run(): for t in query.run():
taskDic =dict(json.loads(t.to_JSON())) taskDic =dict(json.loads(t.to_JSON()))
@ -254,7 +258,11 @@ def getAllFutureTasks(token, courseId):
arr = [] arr = []
query = Task.all() query = Task.all()
query.filter("courseId = ", courseId)
try:
query.filter("courseId = ", int(courseId))
except Exception as e:
return bad_request("Bad id format")
for t in query.run(): for t in query.run():
taskDic =dict(json.loads(t.to_JSON())) taskDic =dict(json.loads(t.to_JSON()))
@ -324,7 +332,11 @@ def getTaskComponents(token, taskId):
arr = [] arr = []
query = TaskComponent.all() query = TaskComponent.all()
query.filter("taskId = ", taskId)
try:
query.filter("taskId = ", int(taskId))
except Exception as e:
return bad_request("Bad id format")
for tc in query.run(): for tc in query.run():
arr.append(dict(json.loads(tc.to_JSON()))) arr.append(dict(json.loads(tc.to_JSON())))
@ -384,7 +396,11 @@ def deleteTask(token, taskId):
#return forbidden("lecturer is not owner of course") #return forbidden("lecturer is not owner of course")
user = get_user_by_token(token) user = get_user_by_token(token)
c = Task.get_by_id(int(taskId))
try:
c = Task.get_by_id(int(taskId))
except Exception as e:
return bad_request("Bad id format")
if c is None: if c is None:
return bad_request("no such Task") return bad_request("no such Task")
@ -432,7 +448,12 @@ def deleteTaskComponents(token,taskId):
#return forbidden("lecturer is not owner of course") #return forbidden("lecturer is not owner of course")
user = get_user_by_token(token) user = get_user_by_token(token)
t = Task.get_by_id(int(taskId))
try:
t = Task.get_by_id(int(taskId))
except Exception as e:
return bad_request("Bad id format")
if t is None: if t is None:
return bad_request("no such Task") return bad_request("no such Task")

View file

@ -145,63 +145,63 @@ def updateUser(token):
@user_routes.route('/api/users/addUserToCampus/<string:token>', methods=["PUT"]) # @user_routes.route('/api/users/addUserToCampus/<string:token>', methods=["PUT"])
@auto.doc() # @auto.doc()
def addUserToCampus(token): # def addUserToCampus(token):
""" # """
<span class="card-title">>This Call will add a Campus to user Campus list</span> # <span class="card-title">>This Call will add a Campus to user Campus list</span>
<br> # <br>
<b>Route Parameters</b><br> # <b>Route Parameters</b><br>
- seToken: 'seToken' # - seToken: 'seToken'
<br> # <br>
<br> # <br>
<b>Payload</b><br> # <b>Payload</b><br>
- JSON Object, Example: <br> # - JSON Object, Example: <br>
{<br> # {<br>
'campusId': 1234567890<br> # 'campusId': 1234567890<br>
}<br> # }<br>
<br> # <br>
<b>Response</b> # <b>Response</b>
<br> # <br>
200 - User updated # 200 - User updated
<br> # <br>
400 - Bad Request # 400 - Bad Request
""" # """
#
if not request.data: # if not request.data:
return bad_request() # return bad_request()
#
try: # try:
payload = json.loads(request.data) # payload = json.loads(request.data)
except Exception as e: # except Exception as e:
print e # print e
return bad_request() # return bad_request()
#
if not is_lecturer(token): #todo: change to lecturer id # if not is_lecturer(token): #todo: change to lecturer id
return forbidden("Invalid token or not a lecturer!") # return forbidden("Invalid token or not a lecturer!")
#
user = get_user_by_token(token) # user = get_user_by_token(token)
#
#check Campus Exists # #check Campus Exists
campus = Campus.get_by_id(payload['campusId']) # campus = Campus.get_by_id(payload['campusId'])
if campus is None: # if campus is None:
return bad_request("No such Campus!") # return bad_request("No such Campus!")
#
try: # try:
if str(payload['campusId']) in user.campuses_id_list: # if str(payload['campusId']) in user.campuses_id_list:
return accepted("Already a member of that campus") # return accepted("Already a member of that campus")
#
user.campuses_id_list.append(str(payload['campusId'])) # user.campuses_id_list.append(str(payload['campusId']))
except Exception as e: # except Exception as e:
print e # print e
return bad_request() # return bad_request()
#
#
db.put(user) # db.put(user)
db.save # db.save
return Response(response=user.to_JSON(), # return Response(response=user.to_JSON(),
status=200, # status=200,
mimetype="application/json") # Real response! # mimetype="application/json") # Real response!
#---------------------------------------------------------- #----------------------------------------------------------
@ -314,7 +314,11 @@ def getUserById(token, id):
if get_user_by_token(token) is None: if get_user_by_token(token) is None:
return forbidden('Invalid Token') return forbidden('Invalid Token')
u = get_user_by_id(int(id)) try:
u = get_user_by_id(int(id))
except Exception as e:
return bad_request("Bad id format")
if u is None: if u is None:
return no_content('No user Found') return no_content('No user Found')
@ -371,7 +375,11 @@ def getUsersByCampus(token, campusId):
if user is None: if user is None:
return bad_request("Bad User Token") return bad_request("Bad User Token")
campus = Campus.get_by_id(int(campusId)) try:
campus = Campus.get_by_id(int(campusId))
except Exception as e:
return bad_request("Bad id format")
if campus is None: if campus is None:
return bad_request("No such Campus") return bad_request("No such Campus")
@ -432,7 +440,11 @@ def getUsersByCourse(token, courseId):
if user is None: if user is None:
return bad_request("Bad User Token") return bad_request("Bad User Token")
course = Course.get_by_id(int(courseId)) try:
course = Course.get_by_id(int(courseId))
except Exception as e:
return bad_request("Bad id format")
if course is None: if course is None:
return bad_request("No such Course") return bad_request("No such Course")
@ -493,7 +505,11 @@ def getUsersByProject(token, projectId):
if user is None: if user is None:
return bad_request("Bad User Token") return bad_request("Bad User Token")
project = Project.get_by_id(int(projectId)) try:
project = Project.get_by_id(int(projectId))
except Exception as e:
return bad_request("Bad id format")
if project is None: if project is None:
return bad_request("No such Project") return bad_request("No such Project")
@ -544,7 +560,11 @@ def removeUserFromCampus(token, userId, campusId):
if requestingUser is None: if requestingUser is None:
return bad_request("Bad User Token") return bad_request("Bad User Token")
userToRemove = User.get_by_id(int(userId)) try:
userToRemove = User.get_by_id(int(userId))
except Exception as e:
return bad_request("Bad id format")
if userToRemove is None: if userToRemove is None:
return bad_request("No such user to remove") return bad_request("No such user to remove")
@ -573,9 +593,6 @@ def removeUserFromCampus(token, userId, campusId):
@user_routes.route('/api/users/removeUserFromCourse/<string:token>/<string:userId>/<string:courseId>', methods=["PUT"]) @user_routes.route('/api/users/removeUserFromCourse/<string:token>/<string:userId>/<string:courseId>', methods=["PUT"])
@auto.doc() @auto.doc()
def removeUserFromCourse(token, userId, courseId): def removeUserFromCourse(token, userId, courseId):
@ -604,7 +621,11 @@ def removeUserFromCourse(token, userId, courseId):
if requestingUser is None: if requestingUser is None:
return bad_request("Bad User Token") return bad_request("Bad User Token")
userToRemove = User.get_by_id(int(userId)) try:
userToRemove = User.get_by_id(int(userId))
except Exception as e:
return bad_request("Bad id format")
if userToRemove is None: if userToRemove is None:
return bad_request("No such user to remove") return bad_request("No such user to remove")
@ -635,9 +656,6 @@ def removeUserFromCourse(token, userId, courseId):
mimetype="application/json") # Real response! mimetype="application/json") # Real response!
@user_routes.route('/api/users/removeUserFromProject/<string:token>/<string:userId>/<string:projectId>', methods=["PUT"]) @user_routes.route('/api/users/removeUserFromProject/<string:token>/<string:userId>/<string:projectId>', methods=["PUT"])
@auto.doc() @auto.doc()
def removeUserFromProject(token, userId, projectId): def removeUserFromProject(token, userId, projectId):
@ -666,7 +684,12 @@ def removeUserFromProject(token, userId, projectId):
if requestingUser is None: if requestingUser is None:
return bad_request("Bad User Token") return bad_request("Bad User Token")
userToRemove = User.get_by_id(int(userId)) try:
userToRemove = User.get_by_id(int(userId))
except Exception as e:
return bad_request("Bad id format")
if userToRemove is None: if userToRemove is None:
return bad_request("No such user to remove") return bad_request("No such user to remove")
@ -698,8 +721,6 @@ def removeUserFromProject(token, userId, projectId):
#---------------------------------------------------------- #----------------------------------------------------------
# DOCUMENTATION # DOCUMENTATION
#---------------------------------------------------------- #----------------------------------------------------------