added data verification

This commit is contained in:
Aran Zaiger 2015-06-29 20:11:09 +03:00
parent ec01ac7a94
commit e8647534ac
6 changed files with 191 additions and 91 deletions

View file

@ -123,7 +123,12 @@ def joinCampus(token, campusId):
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:
return bad_request("No such course")
@ -294,7 +299,12 @@ def getCampusesByUserID(token, id):
if user is None:
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:
return no_content("No User")
@ -351,7 +361,11 @@ def deleteCampus(token,campusId):
return forbidden("Invalid token or not a lecturer!")
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:
return bad_request("no such campus")

View file

@ -145,7 +145,11 @@ def joinCourse(token, courseId):
if user is None:
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:
return bad_request("No such course")
@ -205,7 +209,12 @@ def getAllCoursesByCampus(token, campusId):
arr = []
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():
arr.append(dict(json.loads(c.to_JSON())))
@ -258,7 +267,11 @@ def getUserCoursesByCampus(token, campusId):
if user is None:
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:
return bad_request("No such Campus")
@ -318,6 +331,11 @@ def getCoursesByUser(token, userId):
if user is None:
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))
if otherUser is None:
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()
def deleteCourse(token, courseid):
def deleteCourse(token, courseId):
"""
<span class="card-title">This Call will delete a specific Course</span>
<br>
@ -383,7 +401,11 @@ def deleteCourse(token, courseid):
return forbidden("Invalid token or not a lecturer!")
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:
return bad_request("no such course")

View file

@ -153,7 +153,11 @@ def getMessagesByGroup(token, groupId):
arr = []
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():
msgDic = dict(json.loads(m.to_JSON()))
@ -222,7 +226,12 @@ def deleteMessage(token, msgId):
if user is None:
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:
return bad_request("No such Message")

View file

@ -134,7 +134,12 @@ def joinProject(token, projectId):
if user is None:
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:
return bad_request("No such Project")
@ -193,7 +198,11 @@ def getProjectsByCourse(token, courseId):
arr = []
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():
proj = dict(json.loads(p.to_JSON()))
@ -305,7 +314,11 @@ def deleteProject(token,projectId):
user = get_user_by_token(token)
if user is None:
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:
return bad_request("no such Project")

View file

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