__author__ = 'sagi' import json from GithubAPI.GithubAPI import GitHubAPI_Keys from google.appengine.ext import db import requests import uuid 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.User import User from models.Course import Course from models.Project import Project from models.Campus import Campus #Validation Utils Libs from SE_API.Validation_Utils import * from SE_API.Respones_Utils import * app = Flask(__name__, static_folder='../templates') githubKeys = GitHubAPI_Keys() app.config['GITHUB_CLIENT_ID'] = githubKeys.getId() app.config['GITHUB_CLIENT_SECRET'] = githubKeys.getSecret() github = GitHub(app) cross = CORS(app) auto = Autodoc(app) @app.errorhandler(404) def page_not_found(e): return app.send_static_file('views/404/index.html') @app.route('/') def wellcomePage(): return app.send_static_file('index.html') @app.route('/api/validation/confirm/', methods=["GET"]) @auto.doc() def confirm_user_to_campus(validation_token): """ This Function is will Activate a user and add tha campus to it
Route Parameters
- validation_token: 'seToken|email_suffix'

Payload
- NONE

Response
200 - redirect to home + new cookie
403 - Invalid Token """ #TODO token = str(validation_token).split('|')[0] email_sufix = '@'+str(validation_token).split('|')[1] user = get_user_by_token(token) if user is None: return forbidden('Forbidden: invalid Token') else: campus = get_campus_by_suffix(email_sufix) if campus is None: return bad_request('Bad Request: Email Suffix ' + email_sufix + ' Not Found') user.isFirstLogin = False user.seToken = str(uuid.uuid4()) if str(campus.key().id()) not in user.campuses_id_list: user.campuses_id_list.append(str(campus.key().id())) db.put(user) return cookieMonster(user.seToken) @app.route('/api/validation/sendmail/', methods=['POST']) @auto.doc() def send_activation(token): """ This Method Will Send An Email To The User - To Confirm his Account
Route Parameters
- token: 'seToken'

Payload
- JSON object Example
{email: 'academic@email.ac.com'}

Response
200 - Email Sent - No Response
400 - Bad Request
403 - Invalid Token
""" if not request.data: return Response(response=json.dumps({'message': 'Bad Request'}), status=400, mimetype="application/json") payload = json.loads(request.data) if not is_user_token_valid(token): return Response(response=json.dumps({'message': 'Not A Valid Token!'}), status=403, mimetype="application/json") query = User.all() query.filter('seToken =', token) for u in query.run(limit=1): try: send_validation_email(token=token, name=u.username, email=payload["email"]) except Exception: return Response(response=json.dumps({'message': 'Bad Request'}), status=400, mimetype="application/json") return Response(status=200) @app.route('/api/help') def documentation(): return auto.html() @app.route('/home') def returnHome(): try: return app.send_static_file('views/index.html') except Exception: abort(404) @app.route('/api/getUserByToken/', methods=["GET"]) @auto.doc() def getUserByToken(token): ''' This Function is will Activate a user and add tha campus to it
Route Parameters
- validation_token: 'seToken|email_suffix'

Payload
- NONE

Response
200 - JSON Example:
{
'username' : 'github_username',
'name' : 'Bob Dylan',
'email' : 'email@domain.com',
'isLecturer' : true,
'seToken' : 'dds2d-sfvvsf-qqq-fdf33-sfaa',
'avatar_url' : 'http://location.domain.com/image.jpg',
'isFirstLogin' : false,
'campuses_id_list': ['22314','243512',...,'356'],
'classes_id_list': ['22314','243512',...,'356']
}

403 - Invalid Token ''' query = User.all() query.filter("seToken = ", token) for u in query.run(limit=5): return Response(response=u.to_JSON(), status=201, mimetype="application/json") # Real response! return Response(response=json.dumps({'message' : 'No User Found'}), status=400, mimetype="application/json") @app.route('/githubOAuth') @cross_origin('*') @github.authorized_handler def oauth(oauth_token): if oauth_token is None: return render_template("index.html", messages={'error': 'OAuth Fail'}) try: response = requests.get("https://api.github.com/user?access_token=" + oauth_token) user_data = json.loads(response.content) response = requests.get("https://api.github.com/user/emails?access_token=" + oauth_token) userEmails = json.loads(response.content) except Exception: return "

Max Retries connection To Github

github has aborted connection due to to many retries. you need to wait

" resault = User.all() resault.filter("username =", str(user_data["login"])) print user_data["login"] for u in resault.run(): print "Exists!!!" u.seToken = str(uuid.uuid4()) u.accessToken = oauth_token u.put() return cookieMonster(u.seToken) tempName = ";" if user_data["email"] == "": for email in userEmails: if email["primary"] and email["verified"]: tempEmail = email["email"] else: tempEmail = user_data["email"] user = User(username=user_data["login"], name=tempName, avatar_url=user_data["avatar_url"], email=tempEmail, isLecturer=False, accessToken=oauth_token, seToken=str(uuid.uuid4())) db.put(user) db.save return cookieMonster(user.seToken) @app.route('/api/Campuses/create/', methods=['POST']) @auto.doc() def create_campus(token): """ This call will create a new campus in the DB
Route Parameters
- seToken: 'seToken'

Payload
- JSON Object, Example:
{
'title': 'Campus name',
'email_ending': '@campus.ac.com',
'avatar_url': 'http://location.domain.com/image.jpg'
}


Response
201 - Created
403 - Invalid Token/Forbidden """ if not request.data: return Response(response=json.dumps({'message': 'Bad Request'}), status=400, mimetype="application/json") payload = json.loads(request.data) if not is_lecturer(token): #todo: change to lecturer id return Response(response=json.dumps({'message': 'Invalid token or not a lecturer!'}), status=403, mimetype="application/json") user = get_user_by_token(token) #todo: check legality try: campus = Campus(title=payload['title'], email_ending=payload['email_ending'], master_user_id=user.key().id(), avatar_url=payload['avatar_url']) except Exception: return Response(response=json.dumps({'message': 'Bad Request'}), status=400, mimetype="application/json") db.put(campus) db.save return Response(response=json.dumps(campus.to_JSON()), status=201, mimetype="application/json") # @app.route('/api/Courses/create/', methods=['POST']) # @auto.doc() # def create_course(token): # """ # This call will create a new campus in the DB # :param token: user seToken # Payload # { # 'courseName': self.courseName, # 'campusName': self.campusName, # 'projects': self.projects # 'startDate': self.startDate # 'endDate': self.endDate # 'taskFlag': self.taskFlag # } # # :return: # code 200 # """ # if not request.data: # return Response(response=json.dumps({'message': 'Bad Request'}), # status=400, # mimetype="application/json") # payload = json.loads(request.data) # if not is_lecturer(token): #todo: change to lecturer id # return Response(response=json.dumps({'message': 'Invalid token or not a lecturer!'}), # status=403, # mimetype="application/json") # # user = get_user_by_token(token) # # #todo: check legality # # # try: # course = Course(courseName=payload['courseName'], campusName=payload['campusName'], projects=payload['projects'], startDate=payload['startDate'], endDate=payload['endDate'], taskFlag=payload['taskFlag']) # except Exception: # return Response(response=json.dumps({'message': 'Bad Request'}), # status=400, # mimetype="application/json") # # db.put(course) # db.save # return Response(response=json.dumps(course.to_JSON()), # status=200, # mimetype="application/json") # # # @app.route('/api/Campuses/', methods=['GET']) @auto.doc() def get_campuses(token): """ This Call will return an array of all Campuses available
Route Parameters
- seToken: 'seToken'

Payload
- NONE


Response
200 - JSON Array, Example:
[
{ 'title': 'JCE',
'email_ending': '@post.jce.ac.il',
'master_user_id': 123453433341, (User that created the campus)
'avatar_url': 'http://some.domain.com/imagefile.jpg'
},
....
{
...
}req
]

403 - Invalid Token
500 - Server Error """ if is_user_token_valid(token): arr = [] query = Campus.all() for c in query.run(): arr.append(dict(json.loads(c.to_JSON()))) 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") else: return Response(response=json.dumps({'message': 'Invalid Token'}), status=403, mimetype="application/json") @app.route('/login') @cross_origin('*') def login(): return github.authorize() @app.route('/api/qa/init') def init_QA(): is_student_exist = False is_lecturer_exist = False qa_student = User(username='qa_student', name='Student QA', avatar_url='http://ava.com', email='just@mail.com', isLecturer=False, accessToken='student_token_', seToken='_QA_TOKEN_TEST_STUDENT') qa_lecturer = User(username='qa_lecturer', name='Student QA', avatar_url='http://ava.com', email='just@mail.com', isLecturer=False, accessToken='student_token_', seToken='_QA_TOKEN_TEST_LECTURER') query = User.all().filter('username =', qa_student.username) for u in query.run(): is_student_exist = True u.isLecturer = qa_student.isLecturer u.seToken = qa_student.seToken u.campuses_id_list = [] u.classes_id_list = [] db.put(u) query = User.all().filter('username =', qa_lecturer.username) for u in query.run(): is_lecturer_exist = True u.isLecturer = qa_lecturer.isLecturer u.seToken = qa_lecturer.seToken u.campuses_id_list = [] u.classes_id_list = [] db.put(u) if not is_lecturer_exist: db.put(qa_lecturer) if not is_student_exist: db.put(qa_student) return Response(status=200) def cookieMonster(uid): redirect_to_home = redirect('/home') response = app.make_response(redirect_to_home ) response.set_cookie('com.sehub.www',value=uid) return response