def get_user(self, user): """ Return a User object. """ try: user_object = user_from_str(user) except User.DoesNotExist: raise CommandError(f"No user {user} found.") # lint-amnesty, pylint: disable=raise-missing-from return user_object
def get_user(self, user): """ Return a User object. """ try: user_object = user_from_str(user) except User.DoesNotExist: raise CommandError(u"No user {user} found.".format(user=user)) return user_object
def parse_args(self, **options): """ Return a 5-tuple of passed in values for (course_key, user, org, course, run). """ try: course_key = CourseKey.from_string(options['course_key']) except InvalidKeyError: raise CommandError("Invalid location string") # lint-amnesty, pylint: disable=raise-missing-from try: user = user_from_str(options['email']) except User.DoesNotExist: raise CommandError(u"No user found identified by {}".format(options['email'])) # lint-amnesty, pylint: disable=raise-missing-from return course_key, user.id, options['org'], options['course'], options['run']
def handle(self, *args, **options): try: courses = json.loads(options["courses_json"])["courses"] except ValueError: raise CommandError("Invalid JSON object") # lint-amnesty, pylint: disable=raise-missing-from except KeyError: raise CommandError("JSON object is missing courses list") # lint-amnesty, pylint: disable=raise-missing-from for course_settings in courses: # Validate course if not self._course_is_valid(course_settings): logger.warning( "Can't create course, proceeding to next course") continue # Retrieve settings org = course_settings["organization"] num = course_settings["number"] run = course_settings["run"] user_email = course_settings["user"] try: user = user_from_str(user_email) except User.DoesNotExist: logger.warning(user_email + " user does not exist") # lint-amnesty, pylint: disable=logging-not-lazy logger.warning( "Can't create course, proceeding to next course") continue fields = self._process_course_fields(course_settings["fields"]) # Create the course try: new_course = create_new_course_in_store( "split", user, org, num, run, fields) logger.info(f"Created {str(new_course.id)}") except DuplicateCourseError: logger.warning("Course already exists for %s, %s, %s", org, num, run) # Configure credit provider if ("enrollment" in course_settings) and ("credit_provider" in course_settings["enrollment"]): credit_provider = course_settings["enrollment"][ "credit_provider"] if credit_provider is not None: CreditProvider.objects.get_or_create( provider_id=credit_provider, display_name=credit_provider)