OK much
This commit is contained in:
42
Html/db/course.py
Normal file
42
Html/db/course.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import os
|
||||
# Example usage:
|
||||
import json
|
||||
LOCAL_COURSE_DATA_DIR = "data/course/" # only use locally, not for other .py files
|
||||
|
||||
class Course:
|
||||
def __init__(self, course_name, course_id, course_auther, course_img_path, lessons, description):
|
||||
self.course_name = course_name
|
||||
self.course_id = course_id
|
||||
self.course_auther = course_auther
|
||||
self.course_img_path = course_img_path
|
||||
self.lessons = lessons
|
||||
self.description = description
|
||||
|
||||
|
||||
def __json__(self):
|
||||
return {
|
||||
'course_id': self.course_id,
|
||||
'course_name': self.course_name,
|
||||
'course_auther': self.course_auther,
|
||||
'course_img_path': self.course_img_path,
|
||||
'lessons': self.lessons,
|
||||
'description': self.description
|
||||
}
|
||||
def to_json(self):
|
||||
return json.dumps(self, default=lambda o: o.__json__(), indent=4)
|
||||
|
||||
def load_course_from_json(course_id, course_data_dir = None)-> Course:
|
||||
try:
|
||||
COURSE_DATA_DIR = course_data_dir if course_data_dir else LOCAL_COURSE_DATA_DIR
|
||||
with open(os.path.join(COURSE_DATA_DIR, f'{course_id}.json'), "r") as f:
|
||||
raw_json = f.read()
|
||||
user_data = json.loads(raw_json)
|
||||
return Course(**user_data)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return None
|
||||
|
||||
if(__name__ == "__main__"):
|
||||
|
||||
user = load_course_from_json('CS101')
|
||||
|
||||
Reference in New Issue
Block a user