import copy __author__ = 'Aran' from flask import Blueprint import json 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 from flask.ext.github import GitHub from flask.ext.cors import CORS, cross_origin from flask.ext.autodoc import Autodoc # DB Models from models.Course import Course from models.Project import Project from models.Message import Message #Validation Utils Libs from SE_API.Validation_Utils import * from SE_API.Respones_Utils import * message_routes = Blueprint("message_routes", __name__) auto = Autodoc() #---------------------------------------------------------- # POST #---------------------------------------------------------- @message_routes.route('/api/messages/create/', 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:
{
'groupId' : 123456789,
'message' : 'Class is canceled',
'isProject' : true
}


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(groupId=payload['groupId'], message=payload['message'], msgDate=datetime.datetime.now(), master_id=user.key().id()) except Exception as e: print e return bad_request("there") try: msg['isProject'] = payload['isProject'] except Exception as e: pass db.put(msg) db.save return Response(response=json.dumps(msg), status=200, mimetype="application/json") #---------------------------------------------------------- # PUT #---------------------------------------------------------- #---------------------------------------------------------- # GET #---------------------------------------------------------- @message_routes.route('/api/messages/getMessagesByGroup//', methods=["GET"]) @auto.doc() def getMessagesByGroup(token, groupId): """ >This Call will return an array of all messages (sorted by date),
for a given group (course or project)

Route Parameters
- SeToken: token
- groupId: 1234567890

Payload
- NONE

Response
200 - JSON Example:
{
'groupId' : 1234567890,
'message' : 'hello all',
'date' : {
'year': 2015,
'month': 5,
'day': 5,
'hour': 5,
'minute': 5
},
'id' : 1234567890,
'master_id' : 1234567890,
'isProject' : false,
'user': {
'username': 'DarkLord',
'name': 'Darth Vader',
'email': 'darkLord@death.planet,
'isLecturer': 'True',
'seToken': 'xxxxxx-xxxxx-xxxxx-xxxxxx',
'avatar_url': 'http://location.git.com/somthing'
'isFirstLogin': False,
'campuses_id_list': [43243532532,5325325325,532532342],
'courses_id_list': [53523,43432423,432432432432]
'id': 1234567890
},
'group': {The Group Object Project OR Campus (according to isProject)}

}

""" if get_user_by_token(token) is None: return bad_request("No such User") arr = [] query = Message.all() 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())) #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) print arr arr = sorted(arr, key=itemgetter('forSortDate'), reverse=True) 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") @message_routes.route('/api/messages/getAllUserMessages/', methods=["GET"]) @auto.doc() def getAllUserMessages(token): """ >This Call will return an array of all messages (sorted by date),

Route Parameters
- SeToken: token
- groupId: 1234567890

Payload
- NONE

Response
200 - JSON Example:
[
{
'groupId' : 1234567890,
'message' : 'hello all',
'date' : {
'year': 2015,
'month': 5,
'day': 5,
'hour': 5,
'minute': 5
},
'id' : 1234567890,
'master_id' : 1234567890,
'isProject' : false,
'user': {
'username': 'DarkLord',
'name': 'Darth Vader',
'email': 'darkLord@death.planet,
'isLecturer': 'True',
'seToken': 'xxxxxx-xxxxx-xxxxx-xxxxxx',
'avatar_url': 'http://location.git.com/somthing'
'isFirstLogin': False,
'campuses_id_list': [43243532532,5325325325,532532342],
'courses_id_list': [53523,43432423,432432432432]
'id': 1234567890
},
'group': {The Group Object Project OR Campus (according to isProject)}

}
]

""" user = get_user_by_token(token) if user is None: return bad_request("No such User") arr = [] allMsgs = Message.all() projectMsgs = copy.deepcopy(allMsgs) projectMsgs.filter('isProject =', False) for m in projectMsgs.run(): if str(m.groupId) in user.courses_id_list: 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) allMsgs.filter('isProject =', True) for m in allMsgs.run(): if str(m.groupId) in user.projects_id_list: 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=True) 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") #---------------------------------------------------------- # DELETE #---------------------------------------------------------- @message_routes.route('/api/messages/deleteMessage//', methods=["DELETE"]) @auto.doc() def deleteMessage(token, msgId): """ >This Call will delete a message by owner token
Route Parameters
- SeToken: token - msgId: 1234567890

Payload
- NONE

Response
200 - JSON Example:
{
'groupId' : 1234567890,
'message' : 'hello all',
'date' : {
'year': 2015,
'month': 5,
'day': 5,
'hour': 5,
'minute': 5
},
'id' : 1234567890,
'master_id' : 1234567890,
'isProject' : false
}

""" user = get_user_by_token(token) if user is None: return bad_request("No such User") 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") if msg.master_id != user.key().id(): return forbidden("User is not the Creator of the message") db.delete(msg) db.save return no_content() #---------------------------------------------------------- # DOCUMENTATION #---------------------------------------------------------- @message_routes.route('/api/messages/help') def documentation(): return auto.html()