fix create Task issues

This commit is contained in:
Aran Zaiger 2015-07-02 22:48:01 +03:00
parent fd8b7ed7e7
commit 2c646b289a

View file

@ -48,7 +48,7 @@ def create_task(token):
- JSON Object, Example: <br> - JSON Object, Example: <br>
{<br> {<br>
"title":"task1",<br> "title":"task1",<br>
"courseName":"aran",<br> "courseId":1234567890,<br>
"description":"pls fddfsdfdsk",<br> "description":"pls fddfsdfdsk",<br>
"dueDate":{"year":2010,<br> "dueDate":{"year":2010,<br>
"month":2,<br> "month":2,<br>
@ -87,47 +87,44 @@ def create_task(token):
403 - Invalid token or not a lecturer 403 - Invalid token or not a lecturer
""" """
if not request.data: if not request.data:
return bad_request() return bad_request("no payload")
payload = json.loads(request.data) payload = json.loads(request.data)
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)
#TODO: add seconds and minutes
#check the user(lecturer) is owner of the course
# try:
# arr = []
# query = Course.all()
# query.filter('courseName =',payload['courseName'])
# for t in query.run():
# arr.append(dict(json.loads(t.to_JSON())))
# if len(arr) == 0:
# return bad_request("No such course")
# except Exception as e:
# print e
# return bad_request("Missing courseName")
#check lecturer is owner of course
#todo: check legality
#create Task object
try: try:
courseId = payload['courseId']
except Exception as e:
print e
return bad_request("invalid courseId format")
course = Course.get_by_id(int(courseId))
if course is None:
return bad_request("No such Course")
if course.master_id != user.key().id():
return forbidden("Lecturer is not owner of Course")
#parse dueDate #parse dueDate
try: try:
date = datetime.date(payload['dueDate']['year'],payload['dueDate']['month'],payload['dueDate']['day']) date = datetime.date(payload['dueDate']['year'],payload['dueDate']['month'],payload['dueDate']['day'])
except Exception: except Exception:
return bad_request("invalid dueDate format") return bad_request("invalid dueDate format")
#create Task object
task = Task(title=payload['title'], courseName=payload['courseName'], description=payload['description'], dueDate=date) try:
task = Task(title=payload['title'], courseId=payload['courseId'], description=payload['description'], dueDate=date)
# print "id: ",task.key().id() except Exception as e:
#parse isPersonal print e
return bad_request("bad")
try: try:
task.isPersonal = payload['isPersonal'] task.isPersonal = payload['isPersonal']
except Exception: except Exception:
pass pass
except Exception as e:
print e
return bad_request()
db.put(task) db.put(task)
db.save db.save
@ -141,7 +138,9 @@ def create_task(token):
db.put(component) db.put(component)
db.save db.save
return created() return Response(response=task.to_JSON(),
status=200,
mimetype="application/json")
#---------------------------------------------------------- #----------------------------------------------------------