in microservices/user_management/src/routes/discipline_association_group.py [0:0]
def remove_user_from_discipline_association_group(uuid: str,
input_user: RemoveUserModel):
"""Remove instructor or assessor from the discipline
association group with the uuid passed in the
request body
### Args:
input_user: UUID of the user that need to
remove from the discipline association group
### Raises:
ResourceNotFoundException: If the group does not exist
Exception: 500 Internal Server Error if something went wrong
### Returns:
RemoveUserFromAssociationGroupResponseModel: discipline
association group Object
"""
try:
existing_association_group = AssociationGroup.find_by_uuid(uuid)
input_users_dict = {**input_user.dict()}
group_fields = existing_association_group.get_fields()
# Checking the given UUID if of discipline type or not
if not is_discipline_association_group(group_fields):
raise ValidationError(f"AssociationGroup for given uuid: {uuid} "
"is not discipline type")
group_users = map(lambda x: x["user"], group_fields["users"])
# Check if user exists in association group
if input_users_dict["user"] not in group_users:
raise ValidationError(f"User with uuid {input_users_dict['user']} "\
f"is not a part of Association Group with uuid {uuid}")
if isinstance(group_fields.get("users"), list):
# Identify if user being deleted is instructor type
is_instructor = False
is_assessor = False
for user_dict in group_fields.get("users"):
if input_users_dict["user"] == user_dict["user"]:
if user_dict["user_type"] == "instructor":
is_instructor = True
break
elif user_dict["user_type"] == "assessor":
is_assessor = True
break
# The filter returns an iterator containing all the users
group_fields["users"][:] = filter(
lambda x: x["user"] != input_users_dict["user"],
group_fields.get("users"))
if group_fields.get("associations"):
Logger.info("""found associations""")
Logger.info(group_fields.get("associations"))
# Get active curriculum_pathway_id for discipline from Discipline Group
for pathway_dict in group_fields.get("associations").get(
"curriculum_pathways"):
if pathway_dict["status"] == "active":
discipline_id = pathway_dict["curriculum_pathway_id"]
break
if is_instructor:
# Logic to remove instructor associated from learner association group
# Fetch all learner association groups
learner_association_groups = AssociationGroup.collection.filter(
"association_type", "==", "learner").fetch()
learner_group_fields = [i.get_fields() for i in \
learner_association_groups]
# Find Learner Association Group in which given instructor exists
learner_group = None
for group in learner_group_fields:
for instructor_dict in group.get("associations").get("instructors"):
if input_users_dict["user"] == instructor_dict["instructor"] and \
instructor_dict["curriculum_pathway_id"] == discipline_id:
learner_group = group
break
# pylint: disable=line-too-long
# Remove instructor from Learner Association Group
if learner_group:
learner_group.get("associations").get("instructors")[:] = [
instructor for instructor in learner_group.get("associations").get(
"instructors") if instructor.get("instructor") != input_users_dict[
"user"] and instructor.get("curriculum_pathway_id") != discipline_id
]
learner_association_group_object = AssociationGroup.find_by_uuid(
learner_group["uuid"])
for key, value in learner_group.items():
setattr(learner_association_group_object, key, value)
learner_association_group_object.update()
elif is_assessor:
# Logic to remove assessor from submitted assessments
Logger.info("About to trigger update assessor reference function")
update_assessor_of_submitted_assessments_of_a_discipline(
uuid,
discipline_id,
input_users_dict
)
else:
# No users exist
raise ValidationError(
f"Discipline association group with uuid {uuid} "\
"doesn't contain any Instructor or Assessors"
)
for key, value in group_fields.items():
setattr(existing_association_group, key, value)
existing_association_group.update()
group_fields = existing_association_group.get_fields(reformat_datetime=True)
return {
"success": True,
"message": "Successfully removed the user "\
"from the discipline association group",
"data": group_fields
}
except ValidationError as e:
Logger.info(traceback.print_exc())
raise BadRequest(str(e)) from e
except ResourceNotFoundException as e:
Logger.info(traceback.print_exc())
raise ResourceNotFound(str(e)) from e
except Exception as e:
Logger.info(traceback.print_exc())
raise InternalServerError(str(e)) from e