Exemple #1
0
def test_course_basic_requirements(db, user_kaylee):
    orm_course = Course(
        org_id=user_kaylee.org_id,
        # course_code = "Strange",
    )
    db.add(orm_course)
    with pytest.raises(IntegrityError):
        db.commit()
    db.rollback()
    orm_course = Course(
        # org_id=user_kaylee.org_id,
        course_code="Strange",
    )
    db.add(orm_course)
    with pytest.raises(IntegrityError):
        db.commit()
    db.rollback()
    orm_course = Course(
        org_id=user_kaylee.org_id,
        course_code="Strange",
    )
    db.add(orm_course)
    db.commit()
    assert orm_course.course_code == "Strange"
    orm_course.course_title = "Damnation Alley"
    db.commit()
Exemple #2
0
    def nbex_user(self):

        hub_user = self.get_current_user()
        hub_username = hub_user.get("name")

        full_name = hub_user.get("full_name")
        current_course = hub_user.get("course_id")
        current_role = hub_user.get("course_role")
        course_title = hub_user.get("course_title", "no_title")
        org_id = hub_user.get("org_id", 1)

        # Raising an error appears to have no detrimental affect when running.
        if not (current_course and current_role):
            note = f"Both current_course ('{current_course}') and current_role ('{current_role}') must have values. User was '{hub_username}'"  # noqa: E501
            self.log.info(note)
            raise ValueError(note)

        self.org_id = org_id

        with scoped_session() as session:
            user = User.find_by_name(db=session,
                                     name=hub_username,
                                     log=self.log)
            if user is None:
                self.log.debug(
                    f"New user details: name:{hub_username}, org_id:{org_id}")
                user = User(name=hub_username, org_id=org_id)
                session.add(user)
            if user.full_name != full_name:
                user.full_name = full_name

            course = Course.find_by_code(db=session,
                                         code=current_course,
                                         org_id=org_id,
                                         log=self.log)
            if course is None:
                self.log.debug(
                    f"New course details: code:{current_course}, org_id:{org_id}"
                )
                course = Course(org_id=org_id, course_code=current_course)
                if course_title:
                    self.log.debug(f"Adding title {course_title}")
                    course.course_title = course_title
                session.add(course)

            # Check to see if we have a subscription (for this course)
            self.log.debug(
                f"Looking for subscription for: user:{user.id}, course:{course.id}, role:{current_role}"
            )

            subscription = Subscription.find_by_set(db=session,
                                                    user_id=user.id,
                                                    course_id=course.id,
                                                    role=current_role)
            if subscription is None:
                self.log.debug(
                    f"New subscription details: user:{user.id}, course:{course.id}, role:{current_role}"
                )
                subscription = Subscription(user_id=user.id,
                                            course_id=course.id,
                                            role=current_role)
                session.add(subscription)

            courses = {}

            for subscription in user.courses:
                if subscription.course.course_code not in courses:
                    courses[subscription.course.course_code] = {}
                courses[subscription.course.course_code][subscription.role] = 1

            model = {
                "kind": "user",
                "id": user.id,
                "name": user.name,
                "org_id": user.org_id,
                "current_course": current_course,
                "current_role": current_role,
                "courses": courses,
            }
        return model
Exemple #3
0
    def nbex_user(self):

        hub_user = self.get_current_user()
        hub_username = hub_user.get("name")

        full_name = hub_user.get("full_name")
        current_course = hub_user.get("course_id")
        current_role = hub_user.get("course_role")
        course_title = hub_user.get("course_title", "no_title")
        org_id = hub_user.get("org_id", 1)

        if not (current_course and current_role):
            return

        self.org_id = org_id

        with scoped_session() as session:
            user = User.find_by_name(db=session,
                                     name=hub_username,
                                     log=self.log)
            if user is None:
                self.log.debug(
                    f"New user details: name:{hub_username}, org_id:{org_id}")
                user = User(name=hub_username, org_id=org_id)
                session.add(user)
            if user.full_name != full_name:
                user.full_name = full_name

            course = Course.find_by_code(db=session,
                                         code=current_course,
                                         org_id=org_id,
                                         log=self.log)
            if course is None:
                self.log.debug(
                    f"New course details: code:{current_course}, org_id:{org_id}"
                )
                course = Course(org_id=org_id, course_code=current_course)
                if course_title:
                    self.log.debug(f"Adding title {course_title}")
                    course.course_title = course_title
                session.add(course)

            # Check to see if we have a subscription (for this course)
            self.log.debug(
                f"Looking for subscription for: user:{user.id}, course:{course.id}, role:{current_role}"
            )

            subscription = Subscription.find_by_set(db=session,
                                                    user_id=user.id,
                                                    course_id=course.id,
                                                    role=current_role)
            if subscription is None:
                self.log.debug(
                    f"New subscription details: user:{user.id}, course:{course.id}, role:{current_role}"
                )
                subscription = Subscription(user_id=user.id,
                                            course_id=course.id,
                                            role=current_role)
                session.add(subscription)

            courses = {}

            for subscription in user.courses:
                if not subscription.course.course_code in courses:
                    courses[subscription.course.course_code] = {}
                courses[subscription.course.course_code][subscription.role] = 1

            model = {
                "kind": "user",
                "id": user.id,
                "name": user.name,
                "org_id": user.org_id,
                "current_course": current_course,
                "current_role": current_role,
                "courses": courses,
            }
        return model