def unassign(cls, course_id, ignore=False):
     course = cls.query.filter_by(id=course_id).first()
     course.category_id = None
     course.ignore = ignore
     std_commit()
     DegreeProgressCourseUnitRequirement.delete(course_id)
     return course
예제 #2
0
def assign_course(course_id):
    params = request.get_json()
    course = DegreeProgressCourse.find_by_id(course_id)
    if course:
        # Get existing category assignment. If it's a placeholder then delete it at end of transaction.
        previous_category = DegreeProgressCategory.find_by_id(
            course.category_id) if course.category_id else None
        category_id = get_param(params, 'categoryId')
        category = DegreeProgressCategory.find_by_id(
            category_id) if category_id else None
        if category:
            if category.template_id != course.degree_check_id:
                raise BadRequestError(
                    'The category and course do not belong to the same degree_check instance.'
                )
            children = DegreeProgressCategory.find_by_parent_category_id(
                parent_category_id=category_id)
            if next((c for c in children if c.category_type == 'Subcategory'),
                    None):
                raise BadRequestError(
                    'A course cannot be assigned to a category with a subcategory.'
                )
            course = DegreeProgressCourse.assign(category_id=category_id,
                                                 course_id=course.id)

        else:
            # When user un-assigns a course we delete all copies of that course,.
            for copy_of_course in DegreeProgressCourse.get_courses(
                    degree_check_id=course.degree_check_id,
                    manually_created_at=course.manually_created_at,
                    manually_created_by=course.manually_created_by,
                    section_id=course.section_id,
                    sid=course.sid,
                    term_id=course.term_id,
            ):
                if copy_of_course.id != course.id:
                    DegreeProgressCourseUnitRequirement.delete(
                        copy_of_course.id)
                    category = DegreeProgressCategory.find_by_id(
                        copy_of_course.category_id)
                    if category and 'Placeholder' in category.category_type:
                        DegreeProgressCategory.delete(category.id)
                    DegreeProgressCourse.delete(copy_of_course)
            ignore = to_bool_or_none(get_param(params, 'ignore'))
            course = DegreeProgressCourse.unassign(course_id=course.id,
                                                   ignore=ignore)

        # If previous assignment was a "placeholder" category then delete it.
        if previous_category and 'Placeholder' in previous_category.category_type:
            DegreeProgressCategory.delete(previous_category.id)

        # Update updated_at date of top-level record
        DegreeProgressTemplate.refresh_updated_at(course.degree_check_id,
                                                  current_user.get_id())
        return tolerant_jsonify(course.to_api_json())
    else:
        raise ResourceNotFoundError('Course not found.')
 def assign(cls, category_id, course_id):
     course = cls.query.filter_by(id=course_id).first()
     course.category_id = category_id
     course.ignore = False
     std_commit()
     DegreeProgressCourseUnitRequirement.delete(course_id)
     for u in DegreeProgressCategoryUnitRequirement.find_by_category_id(
             category_id):
         DegreeProgressCourseUnitRequirement.create(course.id,
                                                    u.unit_requirement_id)
     return course
예제 #4
0
 def _delete_course(c):
     category = DegreeProgressCategory.find_by_id(c.category_id)
     DegreeProgressCourseUnitRequirement.delete(c.id)
     DegreeProgressCourse.delete(c)
     if category and 'Placeholder' in category.category_type:
         DegreeProgressCategory.delete(category_id=category.id)