-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9937721
commit 63dfc3e
Showing
38 changed files
with
4,696 additions
and
796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,107 @@ | ||
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_canvas_group_creation.ipynb. | ||
|
||
# %% auto 0 | ||
__all__ = [] | ||
__all__ = ['CanvasGroup'] | ||
|
||
# %% ../nbs/03_canvas_group_creation.ipynb 3 | ||
from .assign import * | ||
from .gh_group import * | ||
from canvasapi import Canvas | ||
import canvasapi | ||
|
||
# %% ../nbs/03_canvas_group_creation.ipynb 4 | ||
#| export | ||
# %% ../nbs/03_canvas_group_creation.ipynb 5 | ||
class CanvasGroup(): | ||
def __init__(self, | ||
API_URL="https://canvas.ucsd.edu", # the domain name of canvas | ||
API_KEY="", # Integrations' API generated by canvas | ||
course_id="" # Course ID, can be found in the course url | ||
): | ||
"Initialize Canvas Group within a Group Set and its appropriate memberships" | ||
self.API_URL = API_URL | ||
self.canvas = None | ||
self.course = None | ||
self.group_category = None | ||
self.group_categories = None | ||
self.groups = None | ||
self.users = None | ||
self.email_to_canvas_id = None | ||
|
||
# initialize by the input parameter | ||
if API_KEY != "": | ||
self.auth_canvas(API_KEY) | ||
if course_id != "": | ||
self.set_course(course_id) | ||
self.get_group_categories() | ||
|
||
def auth_canvas(self, | ||
API_key: str # the Authenticator key generated from canvas | ||
): | ||
"Authorize the canvas module with API_KEY" | ||
self.canvas = Canvas(self.API_URL, API_KEY) | ||
|
||
def set_course(self, | ||
course_id: int # the course id of the target course | ||
): | ||
"Set the target course by the course ID" | ||
self.course = self.canvas.get_course(course_id) | ||
self.users = list(self.course.get_users()) | ||
self.email_to_canvas_id = {u.email.split("@")[0]: u.id for u in self.users} | ||
|
||
def set_group_category(self, | ||
category_name: str # the target group category | ||
) -> canvasapi.group.GroupCategory: # target group category object | ||
try: | ||
self.group_category = self.group_categories[category_name] | ||
except KeyError: | ||
raise KeyError(f"{category_name} did not found in the group categories." | ||
f"\n Try to create one with CanvasGroup.create_group_category") | ||
return self.group_category | ||
|
||
def get_course(self): | ||
return self.course | ||
|
||
def get_group_categories(self) -> dict: # return a name / group category object | ||
"Grab all existing group category (group set) in this course" | ||
categories = list(self.course.get_group_categories()) | ||
self.group_categories = {cat.name: cat for cat in categories} | ||
return {cat.name: cat for cat in categories} | ||
|
||
def create_group_category(self, | ||
params: dict # the parameter of canvas group category API @ [this link](https://canvas.instructure.com/doc/api/group_categories.html#method.group_categories.create) | ||
) -> canvasapi.group.GroupCategory: # the generated group category object | ||
"Create group category (group set) in this course" | ||
self.group_category = self.course.create_group_category(**params) | ||
return self.group_category | ||
|
||
def create_group(self, | ||
params: dict, #the parameter of canvas group create API at [this link](https://canvas.instructure.com/doc/api/groups.html#method.groups.create) | ||
) -> canvasapi.group.Group: # the generated target group object | ||
"Create canvas group under the target group category" | ||
if self.group_category is None: | ||
raise ValueError("Have you specified or create a group category (group set)?") | ||
return self.group_category.create_group(**params) | ||
|
||
def join_canvas_group(self, | ||
group: canvasapi.group.Group, # the group that students will join | ||
group_members:[str], # list of group member's SIS Login (email prefix, before the @.) | ||
) -> [str]: # list of unsuccessful join | ||
"Add membership access of each group member into the group" | ||
unsuccessful_join = [] | ||
for group_member in group_members: | ||
try: | ||
canvas_id = self.email_to_canvas_id[group_member] | ||
except KeyError: | ||
unsuccessful_join.append(group_member) | ||
print(f"Error adding student {group_member} \n into group {group.name}") | ||
group.create_membership(canvas_id) | ||
return unsuccessful_join | ||
|
||
def assign_canvas_group(self, | ||
group_name: str, # group name, display on canvas | ||
group_members:[str], # list of group member's SIS Login | ||
in_group_category: str, # specify which group category the group belongs to | ||
) -> [str]: # list of unsuccessful join | ||
"Create new groups and assign group member into the class in the `self.group_category`" | ||
self.set_group_category(in_group_category) | ||
group = self.create_group({"name": group_name}) | ||
unsuccessful_join = self.join_canvas_group(group, group_members) | ||
return unsuccessful_join | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,3 @@ | |
# %% ../nbs/00_core.ipynb 3 | ||
from .assign import * | ||
from .gh_group import * | ||
|
||
# %% ../nbs/00_core.ipynb 4 | ||
#| export | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
CanvasGroupy | ||
================ | ||
# CanvasGroupy | ||
|
||
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! --> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.