def post(self, course_id): "Add or update students. (instructors only)" course = self.find_course(course_id) self.check_course_instructor(course) students_str = self.get_argument("students", None) # Check request format if not students_str: self.json_error(400, "Please supply students") try: students = json.loads(students_str) except json.decoder.JSONDecodeError: self.json_error(400, "Students cannot be JSON decoded") if not isinstance(students, list): self.json_error(400, "Incorrect request format") if not students: self.json_error(400, "Please supply students") for i in students: if ( not isinstance(i, dict) or not isinstance(i.get("username"), str) or not isinstance(i.get("first_name"), str) or not isinstance(i.get("last_name"), str) or not isinstance(i.get("email"), str) ): self.json_error(400, "Incorrect request format") # Commit ans = [] for i in students: student = self.find_or_create_user(i["username"]) if student in course.instructors: ans.append( { "username": i["username"], "success": False, "message": "Cannot add instructor as student", } ) continue if student not in course.students: course.students.append(student) association = StudentAssociation.find(self.db, student, course) association.first_name = i["first_name"] association.last_name = i["last_name"] association.email = i["email"] ans.append( { "username": i["username"], "success": True, } ) self.db.commit() self.json_success(status=ans)
def post(self, course_id): 'Add or update students. (instructors only)' course = self.find_course(course_id) self.check_course_instructor(course) students_str = self.get_argument('students', None) # Check request format if not students_str: self.json_error(400, 'Please supply students') try: students = json.loads(students_str) except json.decoder.JSONDecodeError: self.json_error(400, 'Students cannot be JSON decoded') if not isinstance(students, list): self.json_error(400, 'Incorrect request format') if not students: self.json_error(400, 'Please supply students') for i in students: if ( not isinstance(i, dict) or not isinstance(i.get('username'), str) or not isinstance(i.get('first_name'), str) or not isinstance(i.get('last_name'), str) or not isinstance(i.get('email'), str) ): self.json_error(400, 'Incorrect request format') # Commit ans = [] for i in students: student = self.find_or_create_user(i['username']) if student in course.instructors: ans.append( { 'username': i['username'], 'success': False, 'message': 'Cannot add instructor as student', } ) continue if student not in course.students: course.students.append(student) association = StudentAssociation.find(self.db, student, course) association.first_name = i['first_name'] association.last_name = i['last_name'] association.email = i['email'] ans.append( {'username': i['username'], 'success': True,} ) self.db.commit() self.json_success(status=ans)
def wrap_student_info(self, user, course): "Return dict of student info (full name, email, etc)" association = StudentAssociation.find(self.db, user, course) if association is None: # Error in data integrity return { "username": user.id, "first_name": None, "last_name": None, "email": None, } return { "username": user.id, "first_name": association.first_name, "last_name": association.last_name, "email": association.email, }
def wrap_student_info(self, user, course): 'Return dict of student info (full name, email, etc)' association = StudentAssociation.find(self.db, user, course) if association is None: # Error in data integrity return { 'username': user.id, 'first_name': None, 'last_name': None, 'email': None, } return { 'username': user.id, 'first_name': association.first_name, 'last_name': association.last_name, 'email': association.email, }
def post(self, course_id, student_id): "Add or update a student. (instructors only)" course = self.find_course(course_id) self.check_course_instructor(course) student = self.find_or_create_user(student_id) first_name = self.get_argument("first_name", None) if first_name is None: self.json_error(400, "Please supply first name") last_name = self.get_argument("last_name", None) if last_name is None: self.json_error(400, "Please supply last name") email = self.get_argument("email", None) if email is None: self.json_error(400, "Please supply email") if student in course.instructors: self.json_error(409, "Cannot add instructor as student") if student not in course.students: course.students.append(student) association = StudentAssociation.find(self.db, student, course) association.first_name = first_name association.last_name = last_name association.email = email self.db.commit() self.json_success()