def addUserCPIs(request): # sets headers for cors headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST', 'Access-Control-Allow-Headers': 'Content-Type, x-canvas-authorization ' } if request.method == 'OPTIONS': return '', 204, headers headers = {'Access-Control-Allow-Origin': '*'} # get course id from request jsonobject = request.get_json("force=true") courseId = jsonobject['courseId'] access_token = request.headers.get('X-Canvas-Authorization') canvas.translate_access_token(access_token) result = canvas.call(access_token, "users/self", MultiDict()) decoded_response = json.loads(result) # get user id userId = decoded_response['message']['id'] # calling canvas user account done = False unfiltererd = [] pageCounter = 1 while done == False: result = canvas.call(access_token, f"courses/{courseId}/rubrics?page={pageCounter}", MultiDict()) pageCounter += 1 decoded_response = json.loads(result) if decoded_response['message'] != []: unfiltererd.extend(decoded_response['message']) else: done = True CPIs = filterKPIs(unfiltererd) conn = psycopg2.connect(os.environ.get('DATABASE_URI')) cursor = conn.cursor() for cpi in CPIs: context = call_emotion_api(CPIs) cursor.execute( "INSERT INTO public.cpi(id,userId,kpi,description,context) values(%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING;", (cpi[0], userId, cpi[1], cpi[2], context)) conn.commit() cursor.close() conn.close() return "okay", 200, headers
def addUser(request): # sets headers for cors headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': 'Content-Type, x-canvas-authorization ' } if request.method == 'OPTIONS': return '', 204, headers headers = {'Access-Control-Allow-Origin': '*'} access_token = request.headers.get('X-Canvas-Authorization') canvas.translate_access_token(access_token) result = canvas.call(access_token, "/users/self", MultiDict()) decoded_response = json.loads(result) id = decoded_response['message']['id'] conn = psycopg2.connect(os.environ.get('DATABASE_URI')) cursor = conn.cursor() cursor.execute("SELECT * FROM public.user WHERE id=%s;", [id]) user = cursor.fetchone() if user is None: name = decoded_response['message']['name'] cursor.execute("INSERT INTO public.user(id,name) values(%s,%s);", (id, name)) conn.commit() cursor.execute("SELECT * FROM public.user WHERE id=%s;", [id]) user = cursor.fetchone() cursor.close() conn.close() return jsonify(user)
def call_canvas(access_token, params): access_token = access_token course_id = params.get('course_id') assignment_id = params.get('assignment_id') submission_id = params.get('submission_id') endpoint = f"courses/{course_id}/assignments/{assignment_id}/submissions/{submission_id}" json_response = canvas.call(access_token, endpoint) response = json.loads(json_response) return response
def getStudentExpertRequests(request): #sets headers for cors headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': 'Content-Type, x-canvas-authorization ' } if request.method == 'OPTIONS': return '', 204, headers headers = { 'Access-Control-Allow-Origin': '*' } #calling canvas user account access_token = request.headers.get('X-Canvas-Authorization'); canvas.translate_access_token(access_token) result = canvas.call(access_token, "users/self", MultiDict()) decoded_response = json.loads(result) #get user id id = decoded_response['message']['id'] #connect and commit to database conn = psycopg2.connect(os.environ.get('DATABASE_URI'), cursor_factory=psycopg2.extras.DictCursor) cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) cursor.execute("SELECT userId, kpi, description, id FROM public.expertrequest WHERE userId=%s;", [id]) expertRequests = [] for row in cursor: print(row) dict = { "userId": row[0], "kpi": row[1], "description": row[2], "id": row[3] } expertRequests.append(dict) cursor.close() conn.close() #return succes return jsonify(expertRequests),200, headers
def requestStudentExperts(request): #sets headers for cors headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST', 'Access-Control-Allow-Headers': 'Content-Type, x-canvas-authorization ' } if request.method == 'OPTIONS': return '', 204, headers headers = {'Access-Control-Allow-Origin': '*'} #read json from request jsonobject = request.get_json("force=true") #calling canvas user account access_token = request.headers.get('X-Canvas-Authorization') canvas.translate_access_token(access_token) result = canvas.call(access_token, "users/self", MultiDict()) decoded_response = json.loads(result) #get user id id = decoded_response['message']['id'] #get outcomeId courseId = jsonobject["courseId"] rubricId = jsonobject["rubricId"] outcomeId = jsonobject["outcomeId"] str = f"courses/{courseId}/rubrics/{rubricId}" rubric = canvas.call(access_token, str, MultiDict()) decoded_response = json.loads(rubric) #get outcome from rubric outcomes = decoded_response['message']['data'] foundOutcome = None for i in range(len(outcomes)): if outcomes[i]["id"] == outcomeId: foundOutcome = outcomes[i] #find not empty description for i in range(len(foundOutcome['ratings'])): temp = foundOutcome['ratings'][i]['long_description'] if temp != '': description = temp #connect and commit to database conn = psycopg2.connect(os.environ.get('DATABASE_URI')) cursor = conn.cursor() cursor.execute( "SELECT * FROM public.expertrequest WHERE userId=%s AND KPI=%s;", (id, foundOutcome['description'])) expertRequest = cursor.fetchone() if expertRequest is None: cursor.execute( "INSERT INTO public.expertrequest(userId,KPI,description) values(%s,%s,%s);", (id, foundOutcome['description'], description)) conn.commit() cursor.close() conn.close() #return succes return "succes", 200, headers