diff --git a/SE_API/CourseRoutes.py b/SE_API/CourseRoutes.py index 51a54a4..93c2db6 100644 --- a/SE_API/CourseRoutes.py +++ b/SE_API/CourseRoutes.py @@ -7,6 +7,8 @@ from GithubAPI.GithubAPI import GitHubAPI_Keys from google.appengine.ext import db import requests import datetime +from operator import itemgetter + from flask import Flask, request, render_template, redirect, abort, Response @@ -16,6 +18,8 @@ from flask.ext.autodoc import Autodoc # DB Models from models.Course import Course +from models.Campus import Campus +from models.Message import Message #Validation Utils Libs from SE_API.Validation_Utils import * @@ -27,6 +31,10 @@ course_routes = Blueprint("course_routes", __name__) auto = Autodoc() + +#---------------------------------------------------------- +# POST +#---------------------------------------------------------- @course_routes.route('/api/courses/create/', methods=['POST']) @auto.doc() def create_course(token): @@ -92,8 +100,6 @@ def create_course(token): print e return bad_request() - - db.put(course) db.save return Response(response=course.to_JSON(), @@ -102,6 +108,63 @@ def create_course(token): + +@course_routes.route('/api/courses/createMessage/', methods=['POST']) +@auto.doc() +def createMessage(token): + """ + This call will create a new Message in the DB +
+ Route Parameters
+ - seToken: 'seToken' +
+
+ Payload
+ - JSON Object, Example:
+ {
+ 'courseName': 'Advance Math',
+ 'message': 'The lecture today is canceled'
+ }
+
+
+ Response +
+ 201 - Created +
+ 400 - Bad Request +
+ 403 - Invalid token or not a lecturer + """ + if not request.data: + return bad_request("no data") + if not is_lecturer(token): #todo: change to lecturer id + return forbidden("Invalid token or not a lecturer!") + + user = get_user_by_token(token) + + #try to parse payload + try: + payload = json.loads(request.data) + except Exception as e: + return bad_request("here") + + try: + msg = Message(courseName=payload['courseName'], message=payload['message'], msgDate=datetime.datetime.now()) + except Exception as e: + print e + return bad_request("there") + + db.save(msg) + db.save + return created() + + + +#---------------------------------------------------------- +# GET +#---------------------------------------------------------- + + @course_routes.route('/api/courses/getCourseByCampusName/', methods=["GET"]) @auto.doc() def getCourseByCampusName(name): @@ -132,7 +195,7 @@ def getCourseByCampusName(name): """ arr = [] query = Course.all() - query.filter("campusName = ", name) + query.filter("campusName=", name) for c in query.run(): arr.append(dict(json.loads(c.to_JSON()))) @@ -146,6 +209,184 @@ def getCourseByCampusName(name): status=200, mimetype="application/json") +@course_routes.route('/api/courses/getMessagesByCourseName/', methods=["GET"]) +@auto.doc() +def getMessagesByCourseName(name): + """ + >This Call will return an array of all courses in a given campus +
+ Route Parameters
+ - name: 'campus name' +
+
+ Payload
+ - NONE +
+
+ Response +
+ 200 - JSON Example:
+ + {
+ 'courseName': 'Advance Math',
+ 'campusName': 'JCE',
+ 'startDate': '2015-14-3'
+ 'endDate': '2015-29-6'
+ 'taskFlag': 'False'
+ } +
+
+ """ + arr = [] + query = Message.all() + query.filter("courseName = ", name) + + for m in query.run(): + msgDic = dict(json.loads(m.to_JSON())) + #add a key 'forSortDate' for sorting dates + msgTime = datetime.datetime(msgDic['date']['year'], msgDic['date']['month'], msgDic['date']['day'], msgDic['date']['hour'], msgDic['date']['minute']) + msgDic['forSortDate'] = msgTime + arr.append(msgDic) + + arr = sorted(arr, key=itemgetter('forSortDate'), reverse=False) + for i in arr: + del i['forSortDate'] + print arr + + if len(arr) != 0: + return Response(response=json.dumps(arr), + status=200, + mimetype="application/json") + else: + return Response(response=[], + status=200, + mimetype="application/json") + +#---------------------------------------------------------- +# PUT +#---------------------------------------------------------- + + + + +#---------------------------------------------------------- +# DELETE +#---------------------------------------------------------- +@course_routes.route('/api/courses/deleteCourse//', methods=['DELETE']) +@auto.doc() +def deleteCourse(token,courseName): + """ + This Call will delete a specific course +
+ Route Parameters
+ - seToken: 'seToken' + - title: 'courseName' +
+
+ Payload
+ - NONE
+
+
+ Response +
+ 202 - Deleted campus +
+ 204 - No Matching Campus Found +
+ ....
+ {
+ ...
+ }req
+ + ]
+ 400 - Bad Request +
+ 403 - Invalid token or not a lecturer!
+ """ + + if not is_lecturer(token): #todo: change to lecturer id + return forbidden("Invalid token or not a lecturer!") + + + user = get_user_by_token(token) + query = Course.all() + query.filter('master_id =',user.key().id()) + + try: + query.filter('courseName =', courseName) + except Exception as e: + print e + return bad_request("invalid course title attribute") + + + for c in query.run(): + db.delete(c) + db.save + return accepted("course deleted") + + + return bad_request("no such course or not owner of course") + + +@course_routes.route('/api/courses/deleteCoursesByCampus//', methods=['DELETE']) +@auto.doc() +def deleteCoursesByCampus(token,campusName): + """ + This Call will delete a specific campus's courses +
+ Route Parameters
+ - seToken: 'seToken' + - title: 'campusName' +
+
+ Payload
+ - NONE
+
+
+ Response +
+ 202 - Deleted campus +
+ 204 - No Matching Campus Found +
+ ....
+ {
+ ...
+ }req
+ + ]
+ 400 - Bad Request +
+ 403 - Invalid token or not a lecturer!
+ """ + + if not is_lecturer(token): #todo: change to lecturer id + return forbidden("Invalid token or not a lecturer!") + + + user = get_user_by_token(token) + campus = get_campus_by_campusName(campusName) + if campus is None: + return bad_request("Not a campus!") + + #check user is owner of campus + if campus.master_user_id != user.key().id(): + return forbidden("lecturer is not owner of campus!") + + query = Course.all() + + try: + query.filter('campusName =', campusName) + except Exception as e: + print e + return bad_request("invalid course title attribute") + + for c in query.run(): + db.delete(c) + db.save + + return no_content() + @course_routes.route('/api/courses/help') def documentation(): diff --git a/models/Message.py b/models/Message.py new file mode 100644 index 0000000..fa1a456 --- /dev/null +++ b/models/Message.py @@ -0,0 +1,23 @@ +import json + +__author__ = 'Aran' +from google.appengine.ext import db + +class Message(db.Model): + courseName = db.StringProperty(required=True) + message = db.StringProperty(required=True) + msgDate = db.DateTimeProperty(required=True) + + def to_JSON(self): + data = { + 'courseName' : self.courseName, + 'message' : self.message, + 'date' : { + 'year': self.msgDate.year, + 'month': self.msgDate.month, + 'day': self.msgDate.day, + 'hour': self.msgDate.hour, + 'minute': self.msgDate.minute + } + } + return json.dumps(data)