Divided util into it's own module. Struggling with class variables and using them across many functions...

This commit is contained in:
Norm Rasmussen
2024-09-01 08:16:39 -04:00
parent e49d3d0a01
commit 10cb3e54d0
7 changed files with 95 additions and 35 deletions

View File

@ -1,4 +1,5 @@
import Calls import Calls
import datetime
baseurl = Calls.BASEURL baseurl = Calls.BASEURL
@ -18,21 +19,21 @@ Order of operations:
class Project: class Project:
proj_url = "projects" proj_url = "projects"
def __init__(self, proj_id: str): def __init__(self):
self.proj_id = proj_id self.proj_id = proj_id
class Item: class Item:
item_url = "items" item_url = "items"
def __init__(self, item_id: str): def __init__(self):
self.item_id = item_id self.item_id = item_id
class Courses: class Courses:
course_url = "courses" course_url = "courses"
def __init__(self, course_uuid, course_name): def __init__(self):
self.course_uuid = course_uuid self.course_uuid = course_uuid
self.course_name = course_name self.course_name = course_name
@ -40,16 +41,24 @@ class Courses:
class People: class People:
ppl_url = "people" ppl_url = "people"
def __init__(self, ppl_uuid, ppl_email): def __init__(self):
self.ppl_uuid = ppl_uuid self.ppl_uuid = ppl_uuid
self.ppl_email = ppl_email self.ppl_email = ppl_email
# per = People()
# i = Item()
# p = Product()
# c = Courses()
def get_people(): def get_people(proj_id, proj_url, item_id, item_url):
email = "norm@rsmsn.co" email = "norm@rsmsn.co"
if type(email) is str: if type(email) is str:
learner_uuid = get_individual_person(email) learner = get_individual_person(email)
get_courses(learner_uuid) People.ppl_uuid = learner[0]
People.ppl_email = learner[1]
ppl_uuid = People.ppl_uuid
ppl_email = People.ppl_email
get_courses(ppl_uuid)
elif type(email) is list: elif type(email) is list:
person_uuids = get_group_person(email) person_uuids = get_group_person(email)
else: else:
@ -81,23 +90,66 @@ def get_enrollment_status(uuid, learner_uuid):
f"{baseurl}/courses/{uuid}/enrollments?filter[person_id][eq]={learner_uuid}" f"{baseurl}/courses/{uuid}/enrollments?filter[person_id][eq]={learner_uuid}"
) )
enrolled = Calls.get(enrollment_url) enrolled = Calls.get(enrollment_url)
print(f"Is there a project ID? {Project.proj_id}")
if enrolled["data"] == "": if enrolled["data"] == "":
now = datetime.datetime.now()
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
print("Oof, no enrollments. Not to worry. We'll get one created for you.") print("Oof, no enrollments. Not to worry. We'll get one created for you.")
""" mig_enroll_payload = {
If the learner isn't enrolled yet, we need to create one first. Here's the URL: "data": {
( "attributes": {
"/v2/migration/projects/{project_id}/items/{item_id}/enrollment_resources", "enrolled_at": {formatted_now},
"post", "course_id": {c.course_uuid},
) "person_id": {per.ppl_uuid},
""" }
}
}
mig_enroll_url = Call.post(
f"{baseurl}migration/projects/{p.proj_id}/items/{i.item_id}/enrollment_resources"
)
print("Cool, enrollment resource has been created. Let's check that it exists.")
check_resources()
else: else:
print("Nice! We have an enrollment. So now we just need to update progress.") print("Nice! We have an enrollment. So now we just need to update progress.")
""" create_attempt()
If the learner is enrolled, we can go straight to creating an attempt.
(
"/v2/migration/projects/{project_id}/items/{item_id}/course_attempt_resources", def create_attempt():
"post", print(f"Is there a project ID? {proj_id}")
)""" attempt_url = f"{baseurl}/migration/projects/{proj_id}/items/{item_id}/course_attempt_resources"
new_attempt_payload = {
"data": {
"attributes": {
"uuid": {"which uuid?"},
"display_name": {f"{per.ppl_email}'s Attempt for course {c.course_name}"},
"learner_id": {per.ppl_uuid},
"course_id": {c.course_uuid},
"progress": {"100"},
"started_at": {datetime.now() - timedelta(hours = 2)},
"completed_at": {formatted_now},
"completed_activities": [{"uuid": {"1111"}, "completed_at": {formatted_now}}],
}
}
}
"""
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():
get_resources_url = (
f"{baseurl}/migration/projects/{p.proj_id}/items/{i.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! An enrollment exists, let's create an attempt.")
def create_project_item(): def create_project_item():
@ -112,18 +164,22 @@ def create_project_item():
proj_url = f"{baseurl}/migration/{Project.proj_url}" proj_url = f"{baseurl}/migration/{Project.proj_url}"
print(proj_url) print(proj_url)
tmp_p = Calls.post(proj_url, proj_payload) tmp_p = Calls.post(proj_url, proj_payload)
p = Project(tmp_p['data']['id']) Project.proj_id = tmp_p["data"]["id"]
print(p.proj_id) proj_id = Project.proj_id
print(f"Created Project Id: { proj_id }")
item_url = f"{baseurl}/migration/{Project.proj_url}/{p.proj_id}/{Item.item_url}" item_url = f"{baseurl}/migration/{Project.proj_url}/{proj_id}/{Item.item_url}"
print(item_url)
item_payload = { item_payload = {
"data": { "data": {
"type": "migration_items", "type": "migration_items",
"attributes": {"type": "courses"}, "attributes": {"type": "courses"},
} }
} }
i = Item(Calls.post(item_url, item_payload)) item_return = Calls.post(item_url, item_payload)
Item.item_id = item_return['data']['id']
item_id = Item.item_id
print(f"Created Item ID: { item_id }")
get_people(proj_id, proj_url, item_id, item_url)
def get_group_course(list): def get_group_course(list):
@ -143,10 +199,11 @@ def get_individual_person(email):
for items in returned["data"]: for items in returned["data"]:
if items["attributes"]["registration_status"] == "activated": if items["attributes"]["registration_status"] == "activated":
single_uuid = items["id"] single_uuid = items["id"]
single_email = items["attributes"]["email"]
print( print(
f"Awesome. This dude is activated. Proceeding with learner {single_uuid}" f"Awesome. This dude is activated. Proceeding with learner {single_uuid}"
) )
return single_uuid return (single_uuid, single_email)
else: else:
print("Sorry bruv, but ya mate ain't activated yet. Can't do nuffin.") print("Sorry bruv, but ya mate ain't activated yet. Can't do nuffin.")
@ -161,7 +218,8 @@ def get_group_person(list):
if items["attributes"]["registration_status"] == "activated": if items["attributes"]["registration_status"] == "activated":
print("Awesome. This dude is activated. Proceeding.") print("Awesome. This dude is activated. Proceeding.")
single_uuid = items["id"] single_uuid = items["id"]
multiple_uuids.append(learner_uuid) single_email = items["attributes"]["email"]
multiple_uuids.append((single_uuid, single_email))
else: else:
print("Sorry bruv, but ya mate ain't activated yet. Can't do nuffin.") print("Sorry bruv, but ya mate ain't activated yet. Can't do nuffin.")
return multiple_uuids return multiple_uuids
@ -170,4 +228,3 @@ def get_group_person(list):
if __name__ == "__main__": if __name__ == "__main__":
create_project_item() create_project_item()
# get_people() # get_people()
# get_courses()

View File

@ -17,7 +17,7 @@ Notes:
""" """
import json import json
import Calls from utils import calls, apikeys
JSONDOC = "./api_docs.json" JSONDOC = "./api_docs.json"
BASEURL = "https://api.northpass.com/v2/migration" BASEURL = "https://api.northpass.com/v2/migration"
@ -125,11 +125,14 @@ def get_all_projects():
""" """
project_ids = [] project_ids = []
url = f"{BASEURL}/projects" url = f"{BASEURL}/projects"
ret = Calls.get(url) ret = calls.get(url)
for items in ret["data"]: if ret["data"] == "":
project_ids.append(items['id']) for items in ret["data"]:
print(f"{ items['attributes']['name'] } -- { items['id'] }") project_ids.append(items['id'])
print(f"{ items['attributes']['name'] } -- { items['id'] }")
else:
print("Looks like there are no projects created!")
return project_ids return project_ids
@ -140,7 +143,7 @@ def delete_all_projects():
project_ids = get_all_projects() project_ids = get_all_projects()
for proj in project_ids: for proj in project_ids:
url = f"{BASEURL}/projects/{proj}" url = f"{BASEURL}/projects/{proj}"
Calls.delete(url) calls.delete(url)
def get_specific_project(): def get_specific_project():
""" """
@ -148,7 +151,7 @@ def get_specific_project():
""" """
proj_id = "13aa7aed-3fb5-4488-9185-3befd0c1ae86" proj_id = "13aa7aed-3fb5-4488-9185-3befd0c1ae86"
url = f"{BASEURL}/projects/{proj_id}" url = f"{BASEURL}/projects/{proj_id}"
Calls.get(url) calls.get(url)
if __name__ == "__main__": if __name__ == "__main__":

View File