251 lines
8.1 KiB
Python
251 lines
8.1 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"),
|
|
("/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()
|
|
for proj in project_ids:
|
|
url = f"{BASEURL}/projects/{proj}"
|
|
calls.delete(url)
|
|
|
|
|
|
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_enrollment():
|
|
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": {
|
|
"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} response is")
|
|
if enroll_url != 404:
|
|
print("Cool, enrollment resource has been created. Let's create an attempt.")
|
|
create_attempt(project_id, item_id, learner_uuid, course_uuid)
|
|
else:
|
|
print("A 404 error occurred.")
|
|
|
|
|
|
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 check_resources():
|
|
print("Checking resources")
|
|
get_resources_url = (
|
|
f"{baseurl}/migration/projects/{PROJ_ID}/items/{ITEM_ID}/resources"
|
|
)
|
|
get_resources = Calls.get(get_resources_url)
|
|
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()
|
|
# create_attempt()
|