<s> __author__ = '<STR_LIT>' <EOL> from flask import Blueprint <EOL> import json <EOL> from GithubAPI . GithubAPI import GitHubAPI_Keys <EOL> from google . appengine . ext import db <EOL> import requests <EOL> import datetime <EOL> from flask import Flask , request , render_template , redirect , abort , Response <EOL> from flask . ext . github import GitHub <EOL> from flask . ext . cors import CORS , cross_origin <EOL> from flask . ext . autodoc import Autodoc <EOL> from models . Campus import Campus <EOL> from SE_API . Validation_Utils import * <EOL> from SE_API . Respones_Utils import * <EOL> from SE_API . Email_Utils import * <EOL> campus_routes = Blueprint ( "<STR_LIT>" , __name__ ) <EOL> auto = Autodoc ( ) <EOL> @ campus_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ auto . doc ( ) <EOL> def create_campus ( token ) : <EOL> """<STR_LIT>""" <EOL> if not request . data : <EOL> return bad_request ( ) <EOL> if not is_lecturer ( token ) : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> try : <EOL> payload = json . loads ( request . data ) <EOL> except Exception as e : <EOL> return bad_request ( e ) <EOL> try : <EOL> query = Campus . all ( ) <EOL> query . filter ( "<STR_LIT>" , payload [ '<STR_LIT:title>' ] ) <EOL> for c in query . run ( limit = <NUM_LIT:1> ) : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> except Exception as e : <EOL> print e <EOL> user = get_user_by_token ( token ) <EOL> arr = [ ] <EOL> arr . append ( str ( user . key ( ) . id ( ) ) ) <EOL> try : <EOL> campus = Campus ( title = payload [ '<STR_LIT:title>' ] , email_ending = payload [ '<STR_LIT>' ] , master_user_id = user . key ( ) . id ( ) , avatar_url = payload [ '<STR_LIT>' ] , membersId = arr ) <EOL> except Exception : <EOL> return bad_request ( ) <EOL> send_create_campus_request ( user . email , user . name , campus . title ) <EOL> db . put ( campus ) <EOL> notify_se_hub_campus_request ( campus , campus . title ) <EOL> db . delete ( campus ) <EOL> return ok ( ) <EOL> @ campus_routes . route ( '<STR_LIT>' , methods = [ "<STR_LIT>" ] ) <EOL> @ auto . doc ( ) <EOL> def joinCampus ( token , campusId ) : <EOL> """<STR_LIT>""" <EOL> if not is_lecturer ( token ) : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> user = get_user_by_token ( token ) <EOL> try : <EOL> campus = Campus . get_by_id ( int ( campusId ) ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if campus is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if str ( user . key ( ) . id ( ) ) in campus . membersId : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> campus . membersId . append ( str ( user . key ( ) . id ( ) ) ) <EOL> user . courses_id_list . append ( str ( campus . key ( ) . id ( ) ) ) <EOL> db . put ( campus ) <EOL> db . put ( user ) <EOL> db . save <EOL> return Response ( response = campus . to_JSON ( ) , <EOL> status = <NUM_LIT> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ campus_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ auto . doc ( ) <EOL> def get_campuses ( token ) : <EOL> """<STR_LIT>""" <EOL> if is_user_token_valid ( token ) : <EOL> arr = [ ] <EOL> query = Campus . all ( ) <EOL> for c in query . run ( ) : <EOL> arr . append ( dict ( json . loads ( c . to_JSON ( ) ) ) ) <EOL> print "<STR_LIT>" <EOL> print arr <EOL> for c in arr : <EOL> print "<STR_LIT>" <EOL> print c <EOL> if len ( arr ) != <NUM_LIT:0> : <EOL> return Response ( response = json . dumps ( arr ) , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> else : <EOL> return Response ( response = [ ] , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> else : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> @ campus_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ auto . doc ( ) <EOL> def getCampusesByUser ( token ) : <EOL> """<STR_LIT>""" <EOL> user = get_user_by_token ( token ) <EOL> if user is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> arr = [ ] <EOL> for i in user . campuses_id_list : <EOL> campus = Campus . get_by_id ( int ( i ) ) <EOL> arr . append ( dict ( json . loads ( campus . to_JSON ( ) ) ) ) <EOL> if len ( arr ) != <NUM_LIT:0> : <EOL> return Response ( response = json . dumps ( arr ) , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> else : <EOL> return Response ( response = [ ] , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ campus_routes . route ( '<STR_LIT>' , defaults = { '<STR_LIT>' : None , '<STR_LIT:id>' : None } ) <EOL> @ campus_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ auto . doc ( ) <EOL> def getCampusesByUserID ( token , id ) : <EOL> """<STR_LIT>""" <EOL> user = get_user_by_token ( token ) <EOL> if user is None : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> try : <EOL> user = get_user_by_id ( int ( id ) ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if user is None : <EOL> return no_content ( "<STR_LIT>" ) <EOL> arr = [ ] <EOL> for i in user . campuses_id_list : <EOL> campus = Campus . get_by_id ( int ( i ) ) <EOL> arr . append ( dict ( json . loads ( campus . to_JSON ( ) ) ) ) <EOL> if len ( arr ) != <NUM_LIT:0> : <EOL> return Response ( response = json . dumps ( arr ) , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> else : <EOL> return Response ( response = [ ] , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ campus_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ auto . doc ( ) <EOL> def deleteCampus ( token , campusId ) : <EOL> """<STR_LIT>""" <EOL> if not is_lecturer ( token ) : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> user = get_user_by_token ( token ) <EOL> try : <EOL> camp = Campus . get_by_id ( int ( campusId ) ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if camp is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if camp . master_user_id == user . key ( ) . id ( ) : <EOL> db . delete ( camp ) <EOL> db . save <EOL> return accepted ( "<STR_LIT>" ) <EOL> return forbidden ( "<STR_LIT>" ) <EOL> @ campus_routes . route ( '<STR_LIT>' ) <EOL> def documentation ( ) : <EOL> return auto . html ( ) </s>
