def ensure_user_exists(self): """ Function that ensures the user exists. """ user_id = int(self.lti_instance.user_id) print(user_id) user = User.query.get(user_id) if user is None: user = User() user.create(id=user_id, name=self.lti_instance.user_full_name, email=self.lti_instance.user_primary_email, password="******") if not database.addItemSafelyToDB(user): user = None self.cache['user'] = user
def fill_new_course_with_canvas_data(headers, course_id): """ A function that fills a new course with data from canvas. This function is only called if a new course is created. It then fills the course with students, tas and teachers. When using the api canvas has different names for roles, so we use a different function for filling a course with canvas data. """ request_url = lti_base_route + lti_api_route request_url += '/courses/{}/users?'.format(course_id) request_url += 'include[]=enrollments&include[]=email' user_req = requests.get(request_url, headers=headers) users = user_req.json() if user_req.status_code != 200: return for user in users: user_id = user.get('sis_user_id') if user_id is None: return existing_user = User.query.get(user_id) if existing_user is None: name = user.get('name') email = user.get('email') existing_user = User() existing_user.create(id=user_id, name=name, email=email, password="******") if not database.addItemSafelyToDB( existing_user, fill_new_course_with_canvas_data): continue course = Course.query.filter_by(canvas_unique_id=course_id).first() if course is None: return ensure_user_couples_to_course(existing_user, course, user)