Files
Gainsight/Scripts/Migration_tool/migration_playground.py

316 lines
10 KiB
Python

"""
This file serves as the basecamp for all the calls for the projects.
The Creation and Deletion functions will likely be put into different files after some time.
For now, each function needs to be selected below.
*********
Questions:
- Does creating an attempt also create an enrollment?
- Does an attempt/enrollment create a course?
- No to either of these.
- What is the point of multiple item_ids? They seem as variable as projects.
- What is a display name in enrollment or attempt resources?
Notes:
- If client delivers just course progress, do we call completed activities in the array 1, 2, and 3.
- 427 within completed activities shows id, 281 within completed activities shows UUID.
"""
import json
from utils import calls, apikeys
from datetime import datetime, timedelta
JSONDOC = "./api_docs.json"
BASEURL = "https://api.northpass.com/v2/migration"
# All possible URLs and their functions:
tupee = [
# *************************
# The most fundamental of calls.
# *************************
("/v2/migration/projects", "get", "post"),
("/v2/migration/projects/{project_id}", "get", "delete", "patch"),
("/v2/migration/projects/{project_id}/items", "get", "post"),
# IMPORTANT: Each item is of a type that must be one of the following along with their follow up endpoints:
# 'courses' --> course_resources
# 'sections', --> section_resources
# 'activities', --> activity_recourds
# 'people', --> person_resources
# 'enrollments', --> enrollment_resources
# 'course_attempts', --> course_attempt_resources
# 'quiz_attempts', --> quiz_attempt_resources
# 'certificates',
# 'learning_path_attempts --> learning_path_attempt_resources
("/v2/migration/projects/{project_id}/items/{item_id}", "get", "delete"),
# *************************
# Get Resources (Done after you've added them from below)
# *************************
("/v2/migration/projects/{project_id}/items/{item_id}/resources", "get"),
# *************************
# Post calls for creating new resources.
# *************************
("/v2/migration/projects/{project_id}/items/{item_id}/section_resources", "post"),
("/v2/migration/projects/{project_id}/items/{item_id}/person_resources", "post"),
("/v2/migration/projects/{project_id}/items/{item_id}/process_resources", "post"),
("/v2/migration/projects/{project_id}/items/{item_id}/course_resources", "post"),
(
"/v2/migration/projects/{project_id}/items/{item_id}/quiz_attempt_resources",
"post",
),
(
"/v2/migration/projects/{project_id}/items/{item_id}/activity_resources",
"post",
),
(
"/v2/migration/projects/{project_id}/items/{item_id}/course_attempt_resources",
"post",
),
(
"/v2/migration/projects/{project_id}/items/{item_id}/enrollment_resources",
"post",
),
(
"/v2/migration/projects/{project_id}/items/{item_id}/learning_path_attempt_resources",
"post",
),
# *************************
# This is just for setting up the Skilljar Config. Not needed yeet.
# *************************
(
"/v2/migration/projects/{project_id}/skilljar_configuration",
"get",
"post",
"delete",
"patch",
),
# *************************
# Extract is just for getting things out of Skilljar
# *************************
("/v2/migration/projects/{project_id}/extraction_processes", "get"),
("/v2/migration/projects/{project_id}/extract_course_attempts", "post"),
("/v2/migration/projects/{project_id}/extract_courses", "post"),
("/v2/migration/projects/{project_id}/extract_enrollments", "post"),
("/v2/migration/projects/{project_id}/extract_people", "post"),
(
"/v2/migration/projects/{project_id}/extraction_processes/{extraction_process_id}/processing_logs",
"get",
),
# *************************
# Migrate is putting things into CE.
# *************************
("/v2/migration/projects/{project_id}/start_migration", "post"),
]
def read_json_docs():
file = open(JSONDOC)
data = json.load(file)
for items, vals in data["paths"].items():
print(items)
for cmds in vals:
print(cmds)
file.close()
def create_project():
"""
Function to create a project with just a name. While ID is in the example payload, it isn't needed.
Will generate a project ID for you in the return that you can use to transfer everything.
Note that it will not give you an error if a project already exists with that name!
"""
project_name = ""
payload = {
"data":[
{
"type": "migration_projects",
"attributes": {
"name": project_name
},
}
]
}
url = f"{BASEURL}/projects"
calls.post(url, payload)
def get_all_projects():
"""
Returns all projects.
"""
project_ids = []
url = f"{BASEURL}/projects"
ret = calls.get(url)
if ret["data"] != []:
for items in ret["data"]:
project_ids.append(items["id"])
print(f"{ items['attributes']['name'] } -- { items['id'] }")
else:
print("Looks like there are no projects created!")
return project_ids
def delete_all_projects():
"""
Deletes all projects after returning the uuids of the projects currently available.
"""
project_ids = get_all_projects()
print("Within delete function")
for proj in project_ids:
print(proj)
url = f"{BASEURL}/projects/{proj}"
resp = calls.delete(url)
print(resp)
def get_specific_project():
"""
Returns results from a specific project.
"""
proj_id = "13aa7aed-3fb5-4488-9185-3befd0c1ae86"
url = f"{BASEURL}/projects/{proj_id}"
calls.get(url)
def create_course():
project_id = "6c7a21c2-de35-4b9d-9b80-a235401783af"
item_id = "80b95e38-78d1-44b9-8d9f-be96d9c7bf6e"
learner_uuid = "101d891d-f145-4cb2-8f7f-f0d8a90a743e"
course_uuid = "0d41bb57-bc65-4e05-adfd-58436ed0bd50"
now = datetime.now()
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
course_url = f"{BASEURL}/projects/{project_id}/items/{item_id}/course_resources"
course_payload = {
"data": [
{
"attributes": {
"type": "course",
"uuid": "",
"display_name": "",
"name": "",
"short_description": "",
"full_description": "",
"navigation_mode": "",
}
}
]
}
def create_enrollment():
# important!! Received this error:
# {"errors":[{"data":{"0":["size cannot be less than 1"]}},{"resource_type":["resource type and item type must be same"]}]}
project_id = "6c7a21c2-de35-4b9d-9b80-a235401783af"
item_id = "80b95e38-78d1-44b9-8d9f-be96d9c7bf6e"
learner_uuid = "101d891d-f145-4cb2-8f7f-f0d8a90a743e"
course_uuid = "0d41bb57-bc65-4e05-adfd-58436ed0bd50"
now = datetime.now()
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
print("Creating Enrollment")
enroll_payload = {
"data": [
{
"attributes": {
"uuid": "1234567890",
"display_name": "Enrollment Resource Test",
"type": "course",
"data": {
"enrolled_at": formatted_now,
"course_id": course_uuid,
"person_id": learner_uuid
}
}
}
]
}
enroll_url = calls.post(
f"{BASEURL}/projects/{project_id}/items/{item_id}/enrollment_resources",
enroll_payload,
)
print(f"{enroll_url.status_code} response is")
if enroll_url.status_code == 404 or enroll_url.status_code == 400:
print(f"A {enroll_url.status_code} error occurred.")
else:
print("Cool, enrollment resource has been created. Let's create an attempt.")
create_attempt(project_id, item_id, learner_uuid, course_uuid)
def create_attempt():
course_uuid = "f1b92092-60bd-4d52-922a-e462f132b69c"
project_id = "6c7a21c2-de35-4b9d-9b80-a235401783af"
item_id = "80b95e38-78d1-44b9-8d9f-be96d9c7bf6e"
learner_uuid = "101d891d-f145-4cb2-8f7f-f0d8a90a743e"
now = datetime.now()
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
start_time = datetime.now() - timedelta(hours=2)
print(f"{formatted_now} ---- {start_time}")
new_attempt_payload = {
"data": {
"attributes": {
"uuid": course_uuid,
"display_name": f"norm@rsmsn.co's Attempt for course {course_uuid}",
"learner_id": learner_uuid,
"course_id": course_uuid,
"progress": "100",
"started_at": "2024-09-01 14:35:55",
"completed_at": formatted_now,
"completed_activities": [
{"uuid": "1111", "completed_at": formatted_now}
]
}
}
}
print(new_attempt_payload)
attempt_call = calls.post(
f"{BASEURL}/projects/{project_id}/items/{item_id}/course_attempt_resources",
new_attempt_payload,
)
print(attempt_call)
"""
If the learner is enrolled, we can go straight to creating an attempt.
(
"/v2/migration/projects/{project_id}/items/{item_id}/course_attempt_resources",
"post",
)
"""
def get_item():
project_id = "6c7a21c2-de35-4b9d-9b80-a235401783af"
item_id = "80b95e38-78d1-44b9-8d9f-be96d9c7bf6e"
print("Checking item")
get_item_url = f"{BASEURL}/projects/{project_id}/items/{item_id}"
get_item = calls.get(get_item_url)
print(get_item)
def check_resources():
PROJ_ID = "6c7a21c2-de35-4b9d-9b80-a235401783af"
ITEM_ID = "80b95e38-78d1-44b9-8d9f-be96d9c7bf6e"
print("Checking resources")
get_resources_url = (
f"{BASEURL}/projects/{PROJ_ID}/items/{ITEM_ID}/resources"
)
get_resources = calls.get(get_resources_url)
print(get_resources)
# if get_resources["data"] == []:
# print("Something went wrong. No resources were created.")
# else:
# print("We're in! Here's the data:")
# print(get_resources)
if __name__ == "__main__":
# delete_all_projects()
# read_json_docs()
# create_project()
get_all_projects()
# get_specific_project()
# create_enrollment()
# check_resources()
# get_item()
# create_attempt()