__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',
'date' : {
'year': 2015,
'month': 3,
'day': 14,
'hour': 16,
'minute': 53
},
'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 created() #---------------------------------------------------------- # 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
}

""" if get_user_by_token(token) is None: return bad_request("No such User") arr = [] query = Message.all() query.filter("groupId = ", int(groupId)) 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") #---------------------------------------------------------- # 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") msg = Message.get_by_id(int(msgId)) 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()