<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 operator import itemgetter <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 . Course import Course <EOL> from models . Campus import Campus <EOL> from models . Message import Message <EOL> from SE_API . Validation_Utils import * <EOL> from SE_API . Respones_Utils import * <EOL> course_routes = Blueprint ( "<STR_LIT>" , __name__ ) <EOL> auto = Autodoc ( ) <EOL> @ course_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ auto . doc ( ) <EOL> def create_course ( token ) : <EOL> """<STR_LIT>""" <EOL> if not request . data : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if not is_lecturer ( token ) : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> user = get_user_by_token ( token ) <EOL> try : <EOL> payload = json . loads ( request . data ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> try : <EOL> start_date = datetime . date ( payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] , payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] , payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> end_date = datetime . date ( payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] , payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] , payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if end_date <= start_date : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> course = Course ( courseName = payload [ '<STR_LIT>' ] , campusId = payload [ '<STR_LIT>' ] , master_id = user . key ( ) . id ( ) , <EOL> startDate = start_date , endDate = end_date ) <EOL> try : <EOL> query = Course . all ( ) <EOL> query . filter ( '<STR_LIT>' , payload [ '<STR_LIT>' ] ) <EOL> query . filter ( "<STR_LIT>" , payload [ '<STR_LIT>' ] ) <EOL> for c in query . run ( limit = <NUM_LIT:1> ) : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> except Exception as e : <EOL> print e <EOL> except Exception as e : <EOL> print e <EOL> return bad_request ( <NUM_LIT:2> ) <EOL> course . membersId . append ( str ( user . key ( ) . id ( ) ) ) <EOL> db . put ( course ) <EOL> user . courses_id_list . append ( str ( course . key ( ) . id ( ) ) ) <EOL> db . put ( user ) <EOL> db . save <EOL> return Response ( response = course . to_JSON ( ) , <EOL> status = <NUM_LIT> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ course_routes . route ( '<STR_LIT>' , methods = [ "<STR_LIT>" ] ) <EOL> @ auto . doc ( ) <EOL> def joinCourse ( token , courseId ) : <EOL> """<STR_LIT>""" <EOL> user = get_user_by_token ( token ) <EOL> if user is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> try : <EOL> course = Course . get_by_id ( int ( courseId ) ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if course is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if str ( user . key ( ) . id ( ) ) in course . membersId : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> course . membersId . append ( str ( user . key ( ) . id ( ) ) ) <EOL> user . courses_id_list . append ( str ( course . key ( ) . id ( ) ) ) <EOL> db . put ( course ) <EOL> db . put ( user ) <EOL> db . save <EOL> return Response ( response = course . to_JSON ( ) , <EOL> status = <NUM_LIT> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ course_routes . route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" ] ) <EOL> @ auto . doc ( ) <EOL> def getAllCoursesByCampus ( token , campusId ) : <EOL> """<STR_LIT>""" <EOL> if get_user_by_token ( token ) is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> arr = [ ] <EOL> query = Course . all ( ) <EOL> try : <EOL> query . filter ( "<STR_LIT>" , int ( campusId ) ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> for c in query . run ( ) : <EOL> arr . append ( dict ( json . loads ( c . to_JSON ( ) ) ) ) <EOL> print arr <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> @ course_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ auto . doc ( ) <EOL> def getUserCoursesByCampus ( token , campusId ) : <EOL> """<STR_LIT>""" <EOL> user = get_user_by_token ( token ) <EOL> if user is None : <EOL> return bad_request ( "<STR_LIT>" ) <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> arr = [ ] <EOL> for i in user . courses_id_list : <EOL> course = Course . get_by_id ( int ( i ) ) <EOL> if course . campusId == campus . key ( ) . id ( ) : <EOL> arr . append ( dict ( json . loads ( course . 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 = '<STR_LIT>' , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ course_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ auto . doc ( ) <EOL> def getCoursesByUser ( token , userId ) : <EOL> """<STR_LIT>""" <EOL> user = get_user_by_token ( token ) <EOL> if user is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> try : <EOL> otherUser = User . get_by_id ( int ( userId ) ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> otherUser = User . get_by_id ( int ( userId ) ) <EOL> if otherUser is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> arr = [ ] <EOL> for i in otherUser . courses_id_list : <EOL> print i <EOL> course = Course . get_by_id ( int ( i ) ) <EOL> arr . append ( dict ( json . loads ( course . 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 = '<STR_LIT>' , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ course_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ auto . doc ( ) <EOL> def getCoursesByID ( token , courseId ) : <EOL> """<STR_LIT>""" <EOL> user = get_user_by_token ( token ) <EOL> if user is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> try : <EOL> course = Course . get_by_id ( int ( courseId ) ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if course is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> return Response ( response = course . to_JSON ( ) , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ course_routes . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ auto . doc ( ) <EOL> def deleteCourse ( token , courseId ) : <EOL> """<STR_LIT>""" <EOL> if not is_lecturer ( token ) : <EOL> return forbidden ( "<STR_LIT>" ) <EOL> user = get_user_by_token ( token ) <EOL> try : <EOL> c = Course . get_by_id ( int ( courseId ) ) <EOL> except Exception as e : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if c is None : <EOL> return bad_request ( "<STR_LIT>" ) <EOL> if c . master_id == user . key ( ) . id ( ) : <EOL> db . delete ( c ) <EOL> db . save <EOL> return accepted ( "<STR_LIT>" ) <EOL> return forbidden ( "<STR_LIT>" ) <EOL> @ course_routes . route ( '<STR_LIT>' ) <EOL> def documentation ( ) : <EOL> return auto . html ( ) </s>
