diff --git a/Scripts/Migration_tool/Calls.py b/Scripts/Migration_tool/Calls.py index 5773dc26..ce6ee2df 100644 --- a/Scripts/Migration_tool/Calls.py +++ b/Scripts/Migration_tool/Calls.py @@ -25,7 +25,7 @@ def get(url): def post(url, payload): try: - post_response = requests.get(url, headers=HEADERS, json=payload) + post_response = requests.post(url, headers=HEADERS, json=payload) print(f"Executed Post Request. Status code is {get_response.status_code}") except HTTPError as h: print( @@ -34,3 +34,15 @@ def post(url, payload): finally: json_post = get_response.json() # PP.pprint(json_post) + + +def delete(url): + try: + get_response = requests.delete(url, headers=HEADERS) + # print(f"Executed Get Request. Status code is {get_response.status_code}") + except HTTPError as h: + print( + f"Error occurred. Here's the info: {h} and status code: {get_response.status_code}" + ) + finally: + return get_response diff --git a/Scripts/Migration_tool/Mark_Course_As_Complete/Apikeys.py b/Scripts/Migration_tool/Mark_Course_As_Complete/Apikeys.py new file mode 100644 index 00000000..4cadaafe --- /dev/null +++ b/Scripts/Migration_tool/Mark_Course_As_Complete/Apikeys.py @@ -0,0 +1 @@ +SANDBOX = "SlpQlju219WnWogn94dQUT6Yt" diff --git a/Scripts/Migration_tool/Mark_Course_As_Complete/Calls.py b/Scripts/Migration_tool/Mark_Course_As_Complete/Calls.py new file mode 100644 index 00000000..14cc863b --- /dev/null +++ b/Scripts/Migration_tool/Mark_Course_As_Complete/Calls.py @@ -0,0 +1,60 @@ +import requests +import Apikeys +import pprint +import json + + +PP = pprint.PrettyPrinter(indent=4) +APIKEY = Apikeys.SANDBOX +HEADERS = {"content-type": "application/json", "X-Api-Key": APIKEY} +BASEURL = "https://api.northpass.com/v2" + + +def get(url): + try: + get_response = requests.get(url, headers=HEADERS) + # print(f"Executed Get Request. Status code is {get_response.status_code}") + except HTTPError as h: + print( + f"Error occurred. Here's the info: {h} and status code: {get_response.status_code}" + ) + finally: + json_get = get_response.json() + # PP.pprint(json_get) + return json_get + + +def post(url, payload): + try: + post_response = requests.post(url, headers=HEADERS, json=payload) + print(f"Executed Post Request. Status code is {post_response.status_code}") + # if post_response.status_code == 404: + # print(f"Received 404 Response. Here's the returned text: {post_response.text}") + except HTTPError as h: + print( + f"Error occurred. Here's the info: {h} and status code: {post_response.status_code}" + ) + finally: + try: + json_post = post_response.json() + # PP.pprint(json_post) + return json_post + except JSONDecodeError as j: + print( + f"Error occurred. Here's the info: {h} and status code: {post_response.status_code}" + ) + return post_response + finally: + pass + + +def delete(url): + try: + get_response = requests.delete(url, headers=HEADERS) + # print(f"Executed Get Request. Status code is {get_response.status_code}") + except HTTPError as h: + print( + f"Error occurred. Here's the info: {h} and status code: {get_response.status_code}" + ) + finally: + return get_response diff --git a/Scripts/Migration_tool/Mark_Course_As_Complete/__pycache__/Apikeys.cpython-310.pyc b/Scripts/Migration_tool/Mark_Course_As_Complete/__pycache__/Apikeys.cpython-310.pyc new file mode 100644 index 00000000..f65ef317 Binary files /dev/null and b/Scripts/Migration_tool/Mark_Course_As_Complete/__pycache__/Apikeys.cpython-310.pyc differ diff --git a/Scripts/Migration_tool/Mark_Course_As_Complete/__pycache__/Calls.cpython-310.pyc b/Scripts/Migration_tool/Mark_Course_As_Complete/__pycache__/Calls.cpython-310.pyc new file mode 100644 index 00000000..089c22ca Binary files /dev/null and b/Scripts/Migration_tool/Mark_Course_As_Complete/__pycache__/Calls.cpython-310.pyc differ 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 1bf95c2f..366dc198 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 @@ -14,6 +14,37 @@ Order of operations: 8. Run migration. """ + +class Project: + proj_url = "projects" + + def __init__(self, proj_id: str): + self.proj_id = proj_id + + +class Item: + item_url = "items" + + def __init__(self, item_id: str): + self.item_id = item_id + + +class Courses: + course_url = "courses" + + def __init__(self, course_uuid, course_name): + self.course_uuid = course_uuid + self.course_name = course_name + + +class People: + ppl_url = "people" + + def __init__(self, ppl_uuid, ppl_email): + self.ppl_uuid = ppl_uuid + self.ppl_email = ppl_email + + def get_people(): email = "norm@rsmsn.co" if type(email) is str: @@ -44,10 +75,13 @@ def get_individual_course(name, learner_uuid): print(f"Cool. Course {single_uuid} exists. Checking enrollments.") enrollment = get_enrollment_status(single_uuid, learner_uuid) + def get_enrollment_status(uuid, learner_uuid): - enrollment_url = f"{baseurl}/courses/{uuid}/enrollments?filter[person_id][eq]={learner_uuid}" + enrollment_url = ( + f"{baseurl}/courses/{uuid}/enrollments?filter[person_id][eq]={learner_uuid}" + ) enrolled = Calls.get(enrollment_url) - if enrolled['data'] == "": + if enrolled["data"] == "": 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: @@ -66,6 +100,32 @@ def get_enrollment_status(uuid, learner_uuid): )""" +def create_project_item(): + project_name = "Mark Course as Complete" + item_name = "Courses to Mark as Complete" + proj_payload = { + "data": { + "type": "migration_projects", + "attributes": {"name": project_name}, + } + } + 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) + + item_url = f"{baseurl}/migration/{Project.proj_url}/{p.proj_id}/{Item.item_url}" + print(item_url) + item_payload = { + "data": { + "type": "migration_items", + "attributes": {"type": "courses"}, + } + } + i = Item(Calls.post(item_url, item_payload)) + + def get_group_course(list): multiple_uuids = [] for person in email: @@ -107,6 +167,7 @@ def get_group_person(list): return multiple_uuids -if name == "main": - get_people() +if __name__ == "__main__": + create_project_item() + # get_people() # get_courses() diff --git a/Scripts/Migration_tool/__pycache__/Calls.cpython-310.pyc b/Scripts/Migration_tool/__pycache__/Calls.cpython-310.pyc index d2861a44..bf1583bf 100644 Binary files a/Scripts/Migration_tool/__pycache__/Calls.cpython-310.pyc and b/Scripts/Migration_tool/__pycache__/Calls.cpython-310.pyc differ diff --git a/Scripts/Migration_tool/migration_playground.py b/Scripts/Migration_tool/migration_playground.py index 0a8be98e..8d491139 100644 --- a/Scripts/Migration_tool/migration_playground.py +++ b/Scripts/Migration_tool/migration_playground.py @@ -123,12 +123,24 @@ def get_all_projects(): """ Returns all projects. """ + project_ids = [] url = f"{BASEURL}/projects" ret = Calls.get(url) for items in ret["data"]: + project_ids.append(items['id']) print(f"{ items['attributes']['name'] } -- { items['id'] }") + 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(): """ @@ -140,6 +152,7 @@ def get_specific_project(): if __name__ == "__main__": + # delete_all_projects() # read_json_docs() # create_project() get_all_projects()