in microservices/user_management/src/routes/learner_association_group.py [0:0]
def add_user_to_learner_association_group(uuid: str,
input_users: AddUsersModel):
"""Add user to the learner association group with
the uuid passed in the request body
### Args:
input_users: Required body to the add users in
the learner association group with the status field
NOTE: A user can be part of only
# one learner association group at a time
### Raises:
ResourceNotFoundException: If the group does not exist
Exception: 500 Internal Server Error if something went wrong
### Returns:
AddUserToAssociationGroupResponseModel: learner
association group Object
"""
try:
existing_association_group = AssociationGroup.find_by_uuid(uuid)
input_users_dict = {**input_users.dict()}
group_fields = existing_association_group.get_fields()
# Checking the given UUID if of learner type or not
if not is_learner_association_group(group_fields):
raise ValidationError(f"AssociationGroup for given uuid: {uuid} "
"is not learner type")
if not isinstance(group_fields.get("users"), list):
group_fields["users"] = []
learner_association_groups = AssociationGroup.collection.filter(
"association_type", "==", "learner").fetch()
learner_association_groups = [i.get_fields(reformat_datetime=True) for i \
in learner_association_groups]
for input_user in input_users_dict.get("users"):
# Checking wheather given user is of type learner or not
learner = User.find_by_user_id(input_user)
learner_fields = learner.get_fields(reformat_datetime=True)
if learner_fields["user_type"] != "learner":
raise ValidationError(
f"User for given user_id {input_user} is not of learner type")
# TODO: A user can be part of only
# one learner association group at a time
if any(input_user == user_dict.get("user","")
for learner_association_group in learner_association_groups
for user_dict in learner_association_group["users"]):
raise ValidationError("A user can be part of only one learner "\
"association group at a time")
# FIXME: To add validation wheather that user belongs
# to leaner user-group or not
# Only insert the User if its not exist in the learner association group
if not any(
users["user"] == input_user for users in group_fields["users"]):
group_fields["users"].append({
"user": input_user,
"status": input_users_dict.get("status")
})
for key, value in group_fields.items():
setattr(existing_association_group, key, value)
# Update the data once all the incoming users validate
existing_association_group.update()
group_fields = existing_association_group.get_fields(reformat_datetime=True)
return {
"success": True,
"message": "Successfully added the users "\
"to the learner association group",
"data": group_fields
}
except ResourceNotFoundException as e:
Logger.error(e)
Logger.error(traceback.print_exc())
raise ResourceNotFound(str(e)) from e
except ValidationError as e:
Logger.error(e)
Logger.error(traceback.print_exc())
raise BadRequest(str(e)) from e
except Exception as e:
Logger.error(e)
Logger.error(traceback.print_exc())
raise InternalServerError(str(e)) from e