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 datetime
baseurl = Calls.BASEURL
@ -18,21 +19,21 @@ Order of operations:
class Project:
proj_url = "projects"
def __init__(self, proj_id: str):
def __init__(self):
self.proj_id = proj_id
class Item:
item_url = "items"
def __init__(self, item_id: str):
def __init__(self):
self.item_id = item_id
class Courses:
course_url = "courses"
def __init__(self, course_uuid, course_name):
def __init__(self):
self.course_uuid = course_uuid
self.course_name = course_name
@ -40,16 +41,24 @@ class Courses:
class People:
ppl_url = "people"
def __init__(self, ppl_uuid, ppl_email):
def __init__(self):
self.ppl_uuid = ppl_uuid
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"
if type(email) is str:
learner_uuid = get_individual_person(email)
get_courses(learner_uuid)
learner = get_individual_person(email)
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:
person_uuids = get_group_person(email)
else:
@ -81,23 +90,66 @@ def get_enrollment_status(uuid, learner_uuid):
f"{baseurl}/courses/{uuid}/enrollments?filter[person_id][eq]={learner_uuid}"
)
enrolled = Calls.get(enrollment_url)
print(f"Is there a project ID? {Project.proj_id}")
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.")
"""
If the learner isn't enrolled yet, we need to create one first. Here's the URL:
(
"/v2/migration/projects/{project_id}/items/{item_id}/enrollment_resources",
"post",
)
"""
mig_enroll_payload = {
"data": {
"attributes": {
"enrolled_at": {formatted_now},
"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:
print("Nice! We have an enrollment. So now we just need to update progress.")
"""
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",
)"""
create_attempt()
def create_attempt():
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():
@ -112,18 +164,22 @@ def create_project_item():
proj_url = f"{baseurl}/migration/{Project.proj_url}"
print(proj_url)
tmp_p = Calls.post(proj_url, proj_payload)
p = Project(tmp_p['data']['id'])
print(p.proj_id)
Project.proj_id = tmp_p["data"]["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}"
print(item_url)
item_url = f"{baseurl}/migration/{Project.proj_url}/{proj_id}/{Item.item_url}"
item_payload = {
"data": {
"type": "migration_items",
"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):
@ -143,10 +199,11 @@ def get_individual_person(email):
for items in returned["data"]:
if items["attributes"]["registration_status"] == "activated":
single_uuid = items["id"]
single_email = items["attributes"]["email"]
print(
f"Awesome. This dude is activated. Proceeding with learner {single_uuid}"
)
return single_uuid
return (single_uuid, single_email)
else:
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":
print("Awesome. This dude is activated. Proceeding.")
single_uuid = items["id"]
multiple_uuids.append(learner_uuid)
single_email = items["attributes"]["email"]
multiple_uuids.append((single_uuid, single_email))
else:
print("Sorry bruv, but ya mate ain't activated yet. Can't do nuffin.")
return multiple_uuids
@ -170,4 +228,3 @@ def get_group_person(list):
if __name__ == "__main__":
create_project_item()
# get_people()
# get_courses()