def create_learning_content_collections()

in microservices/course_ingestion/services/import_learning_content.py [0:0]


def create_learning_content_collections(title, gcs_path, content_type,
                                        start_page, end_page,
                                        parsed_output_json,last_modified_by="",
                                        created_by="", course_category=""):
  """creates new set of end
  to end documents (in collections)
  using parser alone for a new course"""
  json_filename = parsed_output_json

  try:
    last_competency = None
    last_subcompetency = None
    with open(json_filename) as json_file:
      competencies = json.load(json_file)
    if competencies:
      learning_content_item = LearningContentItem()
      learning_content_item.title = title
      learning_content_item.description = title
      learning_content_item.gcs_path = gcs_path
      learning_content_item.document_type = content_type
      learning_content_item.last_modified_by = last_modified_by
      learning_content_item.created_by = created_by
      learning_content_item.course_category = course_category
      learning_content_item.start_page = start_page
      learning_content_item.end_page = end_page

      competency_ids = []
      for competency_item in competencies:

        if last_competency != competency_item["competency"]:
          last_competency = competency_item["competency"]

          competency = Competency()
          competency.title = competency_item["competency"]
          competency.description = competency_item["competency"]
          competency.label = competency_item["competency"]
          competency.last_modified_by = last_modified_by
          competency.created_by = created_by
          competency.save()
          competency_ids.append(competency.id)
        sub_comp_item = competency_item["sub_competency"]

        if last_subcompetency != sub_comp_item["title"]:
          last_subcompetency = sub_comp_item["title"]
          subcompentency = competency.add_sub_competency()
          subcompentency.title = sub_comp_item["title"]
          subcompentency.description = sub_comp_item["title"]
          subcompentency.all_learning_resource = ""
          subcompentency.label = sub_comp_item["title"]
          subcompentency.total_lus = len(
              sub_comp_item["learning_objectives"]["learning_units"])
          subcompentency.last_modified_by = last_modified_by
          subcompentency.created_by = created_by
          subcompentency.save()

        learning_objective_items = sub_comp_item["learning_objectives"]

        learning_objective = subcompentency.add_learning_objective()
        learning_objective.title = learning_objective_items["title"]
        learning_objective.description = learning_objective_items["title"]
        learning_objective.text = " ".join(
          [lu["text"] for lu in learning_objective_items["learning_units"]])
        learning_objective.last_modified_by = last_modified_by
        learning_objective.created_by = created_by
        learning_objective.save()
        for learning_unit_item in learning_objective_items["learning_units"]:
          learning_unit = learning_objective.add_learning_unit()
          learning_unit.title = learning_unit_item["title"]
          learning_unit.text = learning_unit_item["text"]
          learning_unit.topics = "{}"
          learning_unit.pdf_title = ""
          learning_unit.last_modified_by = last_modified_by
          learning_unit.created_by = created_by
          learning_unit.save()
      learning_content_item.competency_ids = competency_ids
      learning_content_item.save()
      return learning_content_item.id
    else:
      Logger.debug("No competency found")
      raise Exception("Failed in parsing the given document")
  except FileNotFoundError:
    raise Exception(
        "No file found with the name - {}".format(parsed_output_json))
#pylint: disable=superfluous-parens
  except Exception as e: #pylint: disable=broad-except
    if (e.args):
      raise Exception(e)
    else:
      raise Exception("Internal server error. Failed to create Learning \
      content")