def createBook(): status = 200 res = ResponseCreation.ControllerResponse() if g.authenticatedUser['userRole'] != "admin": res.addError('Role Error', 'Only admins can create books') status = 403 else: dynamoDBInstance = getDatabaseClient() res = book_ctrl.createBook(request.json, dynamoDBInstance) if res.hasErrors(): status = 400 return ResponseCreation.createResponse(res,status)
def taughtClassList(): status = 200 res = ResponseCreation.ControllerResponse() role = g.authenticatedUser['userRole'] if role != 'teacher' and role != 'admin' : res.addError('Role error', 'Only teachers can view a list of classes they are teaching') status = 403 else: dynamoDBInstance = getDatabaseClient() res = class_ctrl.getTaughtClassList(dynamoDBInstance) if res.hasErrors(): status = 400 return ResponseCreation.createResponse(res, status)
def updateClassDetails(): status = 200 res = ResponseCreation.ControllerResponse() role = g.authenticatedUser['userRole'] if role != 'admin' and role != 'teacher': res.addError('Role Error', 'Only teachers or admins can update class details') status = 403 else: dynamoDBInstance = getDatabaseClient() res = class_ctrl.updateClassDetails(request.json, dynamoDBInstance) if res.hasErrors(): status = 400 return ResponseCreation.createResponse(res,status)
def getBookList(): status = 200 res = ResponseCreation.ControllerResponse() role = g.authenticatedUser['userRole'] if role != "teacher" and role != "admin" : res.addError('Role error', 'Only those with teacher privileges can get all books.') status = 403 else: dynamoDBInstance = getDatabaseClient() res = book_ctrl.getBookList(dynamoDBInstance) if res.hasErrors(): status = 400 return ResponseCreation.createResponse(res,status)
def create_class(): status = 200 role = g.authenticatedUser['userRole'] if role != "teacher" and role != "admin" : res = ResponseCreation.ControllerResponse() res.addError('Role error', 'Only teachers can create classes') status = 403 else: dynamoDBInstance = getDatabaseClient() res = class_ctrl.createClass(dynamoDBInstance, request.json) if res.hasErrors(): status = 400 return ResponseCreation.createResponse(res, status)
def removeStudentFromClass(): status = 200 role = g.authenticatedUser['userRole'] if role != "teacher" and role != "admin" : res = ResponseCreation.ControllerResponse() res.addError('Role error', 'Only those with teacher privileges can remove students from classes') status = 403 else: dynamoDBInstance = getDatabaseClient() res = class_ctrl.removeStudent(dynamoDBInstance, request.json) if res.hasErrors(): status = 400 else: #send email class_ctrl.sendClassRemovalEmail(dynamoDBInstance, mail, request.json) return ResponseCreation.createResponse(res, status)
def problemSteps(classId, activity): res = ResponseCreation.ControllerResponse() res.addError('Invalid request', 'An error occurred') status = 400 userId = g.authenticatedUser['email'] if request.method == 'GET': #Get the problem tree status = 200 dynamoDBInstance = getDatabaseClient() index, problem = problem_ctrl.getProblemTemplate(classId, activity, userId, dynamoDBInstance) res = algebra.getProblemTree(problem) if res.hasErrors(): status = 461 else: res.addToPayload('problemIndex', index) elif request.method == 'POST': #Post the students results dynamoDBInstance = getDatabaseClient() try: status = 200 problemIndex = int(request.json.get('problemIndex', '')) didSucceed = request.json.get('didSucceed', 'True') == 'True' res = problem_ctrl.updateUserTemplateHistory(classId, activity, userId, problemIndex, didSucceed, dynamoDBInstance) if res.hasErrors(): status = 462 except ValueError, TypeError: pass
def getClass(classCode): status = 200 dynamoDBInstance = getDatabaseClient() res = class_ctrl.getClass(classCode, dynamoDBInstance) if res.hasErrors(): status = 400 return ResponseCreation.createResponse(res, status)
def public_class_list(): status = 200 dynamoDBInstance = getDatabaseClient() res = class_ctrl.getPublicClassList(dynamoDBInstance) if res.hasErrors(): status = 400 return ResponseCreation.createResponse(res, status)
def getBook(bookId): status = 200 res = ResponseCreation.ControllerResponse() role = g.authenticatedUser['userRole'] if role != "teacher" and role != "admin" : res.addError('Role error', 'Only those with teacher privileges can get a book.') status = 403 else: dynamoDBInstance = getDatabaseClient() book = book_ctrl.getBook(bookId, dynamoDBInstance) if not book: res.addError('Data error', 'Unable to retrive book.') status = 400 else: res.addToPayload('book', book) return ResponseCreation.createResponse(res,status)
def test_removeStudentFromClasss(self): print('Running removeStudentFromClass test case') userRole = 'admin' email1 = '[email protected]' email2 = '[email protected]' classCode = 'f77668db-fb9d-4fd6-81f1-026784d6cc9b' res = ResponseCreation.ControllerResponse() #Put user and class data into DB classesTable = db.getTable('classes', dynamodb) classJsonData = {'code': classCode, 'students': [email1, email2]} db.putItem(classJsonData, classesTable) #Check test data was successfully placed into DB jsonData = {'Key': {'code': classCode}} response = db.getItem(jsonData, classesTable) self.assertIsNotNone(response.get('Item')) self.assertEqual(response.get('Item').get('code'), classCode) class_ctrl.removeStudentFromClass(dynamodb, res, email1, classCode) #check that the student was removed from the class jsonData = {'Key': {'code': classCode}} response = db.getItem(jsonData, classesTable) self.assertIsNotNone(response.get('Item')) stu = response.get('Item').get('students') self.assertFalse(email1 in stu) self.assertTrue(email2 in stu)
def test_removeStudentFromClass_len_zero(self): print( 'Running removeStudentFromClass students length is zero test case') userRole = 'admin' email = '*****@*****.**' classCode = 'f77998db-fb9d-4fd6-81f1-026784d6cc9b' res = ResponseCreation.ControllerResponse() #Put user and class data into DB classesTable = db.getTable('classes', dynamodb) classJsonData = {'code': classCode, 'students': [email]} db.putItem(classJsonData, classesTable) #Check test data was successfully placed into DB jsonData = {'Key': {'code': classCode}} response = db.getItem(jsonData, classesTable) self.assertIsNotNone(response.get('Item')) self.assertEqual(response.get('Item').get('code'), classCode) class_ctrl.removeStudentFromClass(dynamodb, res, email, classCode) #check that the attribute was removed from the class jsonData = {'Key': {'code': classCode}} response = db.getItem(jsonData, classesTable) self.assertIsNotNone(response.get('Item')) self.assertIsNone(response.get('Item').get('students'))
def test_removeClassFromStudent_len_zero(self): print( 'Running removeClassFromStudent students length is zero test case') userRole = 'admin' email = '*****@*****.**' classCode = 'f84138db-fb9d-4fd6-81f1-026784d6cc9b' res = ResponseCreation.ControllerResponse() #Put user and class data into DB usersTable = db.getTable('users', dynamodb) userJsonData = {'email': email, 'classCodes': [classCode]} db.putItem(userJsonData, usersTable) # Check test data was successfully placed into DB jsonData = {'Key': {'email': email}} response = db.getItem(jsonData, usersTable) self.assertIsNotNone(response.get('Item')) self.assertEqual(response.get('Item').get('email'), email) class_ctrl.removeClassFromStudent(dynamodb, res, email, classCode) # Check that the attribute was removed from the student jsonData = {'Key': {'email': email}} response = db.getItem(jsonData, usersTable) self.assertIsNotNone(response.get('Item')) self.assertIsNone(response.get('Item').get('classCodes'))
def test_removeClassFromStudent(self): print('Running removeClassFromStudent test case') userRole = 'admin' email = '*****@*****.**' classCode = 'f77668db-fb9d-4fd6-81f1-026784d6cc9b' classCode2 = 'e00045db-fb9d-4fd6-81f1-026784d6cc9b' res = ResponseCreation.ControllerResponse() # Put user and class data into DB usersTable = db.getTable('users', dynamodb) userJsonData = {'email': email, 'classCodes': [classCode, classCode2]} db.putItem(userJsonData, usersTable) # Check test data was successfully placed into DB jsonData = {'Key': {'email': email}} response = db.getItem(jsonData, usersTable) self.assertIsNotNone(response.get('Item')) self.assertEqual(response.get('Item').get('email'), email) class_ctrl.removeClassFromStudent(dynamodb, res, email, classCode2) # Check that the classCode was removed from the student jsonData = {'Key': {'email': email}} response = db.getItem(jsonData, usersTable) self.assertIsNotNone(response.get('Item')) cc = response.get('Item').get('classCodes') self.assertFalse(classCode2 in cc) self.assertTrue(classCode in cc)
def resetPassword(): status = 200 dynamoDBInstance = getDatabaseClient() res = user_ctrl.resetUserPassword(request.json, dynamoDBInstance) if res.hasErrors(): status = 400 return ResponseCreation.createResponse(res,status)
def activate(activationid): logger.info(str(request)) dynamoDBInstance = getDatabaseClient() res = user_ctrl.activate(activationid, dynamoDBInstance) status = 200 if res.hasErrors(): status = 400 flaskResponse = ResponseCreation.createResponse(res, status) logger.info(str(flaskResponse)) return flaskResponse
def getSampleProblems(bookId, chapterTitle, sectionTitle): status = 200 res = ResponseCreation.ControllerResponse() role = g.authenticatedUser['userRole'] if role != "teacher" and role != "admin" : res.addError('Role error', 'Only those with teacher privileges can get sample problems from a book.') status = 403 else: dynamoDBInstance = getDatabaseClient() problemCount = 4 sampleProblems = set() #prevent duplicates for _ in range(problemCount): template = problem_ctrl.getProblemFromBook( bookId, chapterTitle, sectionTitle, g.authenticatedUser['email'], dynamoDBInstance)[1] sampleProblems.add(algebra.getProblem(template)) sampleProblems = list(sampleProblems) if 'Bad Problem' in sampleProblems: status = 400 else: res.addToPayload('problems', sampleProblems) return ResponseCreation.createResponse(res, status)
def register(): logger.info(str(request)) status = 200 dynamoDBInstance = getDatabaseClient() httpOrigin = request.environ.get('HTTP_ORIGIN') jsonData = request.json res = user_ctrl.register(httpOrigin, jsonData, mail, dynamoDBInstance) status = 201 if res.hasErrors(): status = 400 flaskResponse = ResponseCreation.createResponse(res, status) logger.info(str(flaskResponse)) return flaskResponse
def signin(): logger.info(str(request)) status = 200 email = request.authorization.username password = request.authorization.password dynamoDBInstance = getDatabaseClient() userRole = user_ctrl.getRole(email, dynamoDBInstance) if not userRole: userRole = 'student' userCredentials = { 'email': email, 'userRole': userRole, 'password': password } response = ResponseCreation.ControllerResponse() token = MentiiAuth.generateAuthToken(userCredentials, appSecret) response.addToPayload('token', token) flaskResponse = ResponseCreation.createResponse(response, status) logger.info(str(flaskResponse)) return flaskResponse
else: res.addToPayload('problemIndex', index) elif request.method == 'POST': #Post the students results dynamoDBInstance = getDatabaseClient() try: status = 200 problemIndex = int(request.json.get('problemIndex', '')) didSucceed = request.json.get('didSucceed', 'True') == 'True' res = problem_ctrl.updateUserTemplateHistory(classId, activity, userId, problemIndex, didSucceed, dynamoDBInstance) if res.hasErrors(): status = 462 except ValueError, TypeError: pass return ResponseCreation.createResponse(res,status) @app.route('/classes/remove', methods=['POST', 'OPTIONS']) @auth.login_required @handleOptionsRequest def removeStudentFromClass(): status = 200 role = g.authenticatedUser['userRole'] if role != "teacher" and role != "admin" : res = ResponseCreation.ControllerResponse() res.addError('Role error', 'Only those with teacher privileges can remove students from classes') status = 403 else: dynamoDBInstance = getDatabaseClient() res = class_ctrl.removeStudent(dynamoDBInstance, request.json)
def index(): logger.info(str(request)) response = ResponseCreation.createEmptyResponse(200) logger.info(str(response)) return response
def forgotPassword(): dynamoDBInstance = getDatabaseClient() httpOrigin = request.environ.get('HTTP_ORIGIN') user_ctrl.sendForgotPasswordEmail(httpOrigin, request.json, mail, dynamoDBInstance) return ResponseCreation.createEmptyResponse(200)