in microservices/course_ingestion/services/course_inference.py [0:0]
def get_all_courses(self, skip: int, limit: int, sort_by: str, order_by: str,
competencies: bool, search_query: str):
"""returns all the courses"""
course_list = []
ord_by = order_by
if sort_by == "ascending" and order_by != "title":
ord_by = order_by
elif sort_by == "descending" and order_by != "title":
ord_by = "-{}".format(order_by)
courses = Course.collection.order(ord_by).fetch()
if courses:
try:
for course in courses:
course_dict = course.get_fields(reformat_datetime=True)
course_dict["id"] = course.id
competency_list = []
if competencies:
course.load_children()
for competency in course.competencies:
competency_item = competency.get_fields(reformat_datetime=True)
competency_item["id"] = competency.id
competency_list.append(competency_item)
course_dict["competencies"] = competency_list
course_dict.pop("competency_ids", None)
course_list.append(course_dict)
if search_query is not None:
course_list = [course for course in course_list if
search_query.lower() in course["title"].lower()]
if sort_by == "ascending" and order_by == "title":
course_list = sorted(course_list, key=lambda i: i["title"].lower())
elif sort_by == "descending" and order_by == "title":
course_list = sorted(course_list, key=lambda i: i["title"].lower(),
reverse=True)
if skip == 0 and limit == 0:
return course_list
elif skip < 0 or limit < 0:
raise Exception("The skip and limit value should be a positive "
"number")
else:
result = pagination(payload=course_list, skip=skip, limit=limit)
return {"data": result, "total_rec": len(course_list)}
except (TypeError, KeyError) as e:
raise Exception("Failed to fetch all courses") from e
else:
raise ResourceNotFoundException("No courses found")