Skip to content

Commit

Permalink
Update rows #2
Browse files Browse the repository at this point in the history
  • Loading branch information
celinanperalta committed Sep 9, 2022
1 parent 527eb5d commit a1361db
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
18 changes: 17 additions & 1 deletion Assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __get_assignment_type(self, assignment):



def create_notion_page(self):
def get_notion_properties(self):
new_page = {
"Assignment ID" : {"type": "number", "number" : self.id},
"Name" : {"type": "title", "title": [{"type": "text", "text": {"content": self.name}}]},
Expand All @@ -45,3 +45,19 @@ def create_notion_page(self):
}
]
return new_page, children

def get_updated_notion_properties(self):
updated_page = {
"Assignment ID" : {"type": "number", "number" : self.id},
"Name" : {"type": "title", "title": [{"type": "text", "text": {"content": self.name}}]},
"Class" : {"type": "select", "select": {"name": self.course_name}},
"Assignment Type" : {"type": "select", "select": {"name": self.assignment_type}},
"Due Date" : {"type" : "date", "date" : {"start": self.due_at_date.astimezone(pytz.timezone('US/Eastern')).strftime('%Y-%m-%dT%H:%M:%S'), "time_zone": "America/New_York"}},
"Link" : {"type" : "url", "url" : self.html_url}
}

return updated_page




41 changes: 40 additions & 1 deletion AssignmentDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,48 @@ def __print_assignment_attrs(self):
def create_assignment(self, assignment):
course = self.current_courses[assignment['course_id']]
row = Assignment(assignment, course.name)
props, children = row.create_notion_page()
props, children = row.get_notion_properties()
self.client.pages.create(parent={"database_id":config.NOTION_LINK}, properties = props, children = children)

def update_assignment(self, assignment_id, new_assignment):
query = self.client.databases.query(
**{
"database_id":config.NOTION_LINK,
"filter": {
"and": [
{
"property": "Assignment ID",
"number": {
"equals": assignment_id
},
},
{
"property": "Status",
"status": {
"does_not_equal": "Done"
}
}
]
}
}).get("results")

if len(query) == 0:
return 1

page_id = query[0]["id"]
print(page_id)

if page_id == "":
return 1

course = self.current_courses[new_assignment['course_id']]
row = Assignment(new_assignment, course.name)
props = row.get_updated_notion_properties()

self.client.pages.update(page_id=page_id, properties = props)

return 0

def get_active_courses(self):
return self.current_courses

Expand Down
11 changes: 10 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ def run_canvas_to_notion():
assignment_ids = list(filter(lambda x: x not in existing_assignments, assignments))

for i in range(0, len(assignment_ids)):
print("Importing assignment %s/%s\n", i+1, len(assignment_ids))
print(f"Importing assignment {i+1}/{len(assignment_ids)}")
adb.create_assignment(assignments.get(assignment_ids[i]))

if (len(assignment_ids) == 0):
print("Nothing new.")

for i in range(0, len(existing_assignments)):
try:
if adb.update_assignment(existing_assignments[i], assignments.get(existing_assignments[i])) == 0:
print(f"Updated: assignment {existing_assignments[i]}")
else:
print(f"Failed to update: assignment {existing_assignments[i]}")
except:
print(f"Failed to update: assignment {existing_assignments[i]}")

def main():

run_canvas_to_notion()
Expand Down

0 comments on commit a1361db

Please sign in to comment.