From 10cb3e54d04e69a6f31c7f635a7952a9d92e9551 Mon Sep 17 00:00:00 2001 From: Norm Rasmussen Date: Sun, 1 Sep 2024 08:16:39 -0400 Subject: [PATCH] Divided util into it's own module. Struggling with class variables and using them across many functions... --- .../mark_course_as_complete.py | 113 +++++++++++++----- .../Migration_tool/migration_playground.py | 17 +-- Scripts/Migration_tool/utils/__init__.py | 0 .../utils/__pycache__/apikeys.cpython-310.pyc | Bin 0 -> 205 bytes .../utils/__pycache__/calls.cpython-310.pyc | Bin 0 -> 1465 bytes .../{Apikeys.py => utils/apikeys.py} | 0 .../{Calls.py => utils/calls.py} | 0 7 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 Scripts/Migration_tool/utils/__init__.py create mode 100644 Scripts/Migration_tool/utils/__pycache__/apikeys.cpython-310.pyc create mode 100644 Scripts/Migration_tool/utils/__pycache__/calls.cpython-310.pyc rename Scripts/Migration_tool/{Apikeys.py => utils/apikeys.py} (100%) rename Scripts/Migration_tool/{Calls.py => utils/calls.py} (100%) diff --git a/Scripts/Migration_tool/Mark_Course_As_Complete/mark_course_as_complete.py b/Scripts/Migration_tool/Mark_Course_As_Complete/mark_course_as_complete.py index 366dc198..e66e450e 100644 --- a/Scripts/Migration_tool/Mark_Course_As_Complete/mark_course_as_complete.py +++ b/Scripts/Migration_tool/Mark_Course_As_Complete/mark_course_as_complete.py @@ -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() diff --git a/Scripts/Migration_tool/migration_playground.py b/Scripts/Migration_tool/migration_playground.py index 8d491139..ec0a2520 100644 --- a/Scripts/Migration_tool/migration_playground.py +++ b/Scripts/Migration_tool/migration_playground.py @@ -17,7 +17,7 @@ Notes: """ import json -import Calls +from utils import calls, apikeys JSONDOC = "./api_docs.json" BASEURL = "https://api.northpass.com/v2/migration" @@ -125,11 +125,14 @@ def get_all_projects(): """ project_ids = [] url = f"{BASEURL}/projects" - ret = Calls.get(url) + ret = calls.get(url) - for items in ret["data"]: - project_ids.append(items['id']) - print(f"{ items['attributes']['name'] } -- { items['id'] }") + 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 @@ -140,7 +143,7 @@ def delete_all_projects(): project_ids = get_all_projects() for proj in project_ids: url = f"{BASEURL}/projects/{proj}" - Calls.delete(url) + calls.delete(url) def get_specific_project(): """ @@ -148,7 +151,7 @@ def get_specific_project(): """ proj_id = "13aa7aed-3fb5-4488-9185-3befd0c1ae86" url = f"{BASEURL}/projects/{proj_id}" - Calls.get(url) + calls.get(url) if __name__ == "__main__": diff --git a/Scripts/Migration_tool/utils/__init__.py b/Scripts/Migration_tool/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Scripts/Migration_tool/utils/__pycache__/apikeys.cpython-310.pyc b/Scripts/Migration_tool/utils/__pycache__/apikeys.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..abdae51737325f64dd2944737d47a111607eb41a GIT binary patch literal 205 zcmd1j<>g`kf-C1Pr>Ozy#~=5?C9s>)(4^A%1EC6zRGt-L_OEUBG;!E=LbM#9~GINUc6ALo4 cQ!9)03My}L*yQG?l;)(`fgD)O1SB{Z0Il#iW&i*H literal 0 HcmV?d00001 diff --git a/Scripts/Migration_tool/utils/__pycache__/calls.cpython-310.pyc b/Scripts/Migration_tool/utils/__pycache__/calls.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6506fc9c0b2d9096748dbb8b6ece69f3eb930d01 GIT binary patch literal 1465 zcmah}&2QT_6elTKlH-rMY0<;50orAOP{bMr>=F#c+{HkOZkZ9MNUKXAh-|G?mNb%H zX2C87tcP6+?A{INj{Re{%Wk^v)PErznmtl+;_fmEd{2)bk9@!1d(1nX4Fv6vKmU6C zO9P?5wXxnTFusH?4?!@*aEiiO;}9F0goKk5ixFnMz#--Mq!HSTa+@`NC85LY&}9yD zpQEtJnrs8~25Yf4=oZ6cMFKw?Rr zk*rZzoibzgpQt~X=&Ewe=xRJq8E}jPCSR@4)-y~0j;qX}Hc^b@AlFA^C(p2_~xoCpF zwr0WuLGdQ;5`ufUckcd3&dEF1p@f{dloIz{kC@dlft9NU342B6&tQ~y%FPv-3k-Bx zOQIk(6*aVxnl?kPJ^vKwwG6!$5NiRw^kUsQz1B0!&U-lW0=-0f!qz(+5WXv1P!%m;z0e6OG)|(Y zMMsO47TUSr5Z6FeP7%%1JYr!-@42K3*raYO&z1n~iMsRae_Z{jXq zBjLPllYeeev+li0(l5hhB~8F6ZfI(~bYDrh3m+MJ@3A)gsJG zVb=iJjHg`j8X|* z+vcp$f#>EapUcoLOcjNlk>E