def new_unit(content): """ create a new unit entity; return the id of the new unit """ course_id = content["course"] course = get_content_by_id(course_id) if course is None: raise Exception("invalid course id: that course does not exist") if "title" not in content: content["title"] = "" if "body" not in content: content["body"] = "" content["private"] = course.content["private"] content["teacher"] = course.content["teacher"] content["course"] = course_id content["lessons"] = [] key = Curriculum(content_type="unit", content=content).put() # add this unit to the parent course's unit list course.content["units"].append(int(key.id())) course.put() return int(key.id())
def test_course_unit_lesson_creation_and_query(self): """ create a course, then unit, then lesson and make sure the entity relationships are all valid """ course_key = Curriculum( content_type = 'course', content={'title':'numero uno'}, ).put() unit_key = Curriculum( content_type = 'unit', content={'course':int(course_key.id())} ).put() lesson_key = Curriculum( content_type = 'lesson', content={'course':int(course_key.id()), 'unit':int(unit_key.id())} ).put() lesson = lesson_key.get() unit = ndb.Key(Curriculum, lesson.content['unit']).get() course = ndb.Key(Curriculum, unit.content['course']).get() self.assertIsNotNone(unit) self.assertIsNotNone(course) self.assertEqual(course.content['title'], 'numero uno')
def new_lesson(content): """ create a new lesson and return the lesson id """ unit_id = content['unit'] unit = get_content_by_id(unit_id) if unit is None: raise Exception("invalid unit id: that unit does not exist") if 'title' not in content: content['title'] = '' if 'body' not in content: content['body'] = '' content['private'] = unit.content['private'] content['teacher'] = unit.content['teacher'] content['course'] = unit.content['course'] key = Curriculum(content_type = 'lesson', content = content).put() # add this lesson to the parent unit's lesson list unit.content['lessons'].append(int(key.id())) unit.put() return int(key.id())
def test_course_unit_lesson_creation_and_query(self): """ create a course, then unit, then lesson and make sure the entity relationships are all valid """ course_key = Curriculum( content_type='course', content={ 'title': 'numero uno' }, ).put() unit_key = Curriculum(content_type='unit', content={ 'course': int(course_key.id()) }).put() lesson_key = Curriculum(content_type='lesson', content={ 'course': int(course_key.id()), 'unit': int(unit_key.id()) }).put() lesson = lesson_key.get() unit = ndb.Key(Curriculum, lesson.content['unit']).get() course = ndb.Key(Curriculum, unit.content['course']).get() self.assertIsNotNone(unit) self.assertIsNotNone(course) self.assertEqual(course.content['title'], 'numero uno')
def new_unit(content): """ create a new unit entity; return the id of the new unit """ course_id = content['course'] course = get_content_by_id(course_id) if course is None: raise Exception("invalid course id: that course does not exist") if 'title' not in content: content['title'] = '' if 'body' not in content: content['body'] = '' content['private'] = course.content['private'] content['teacher'] = course.content['teacher'] content['course'] = course_id content['lessons'] = [] key = Curriculum(content_type='unit', content=content).put() # add this unit to the parent course's unit list course.content['units'].append(int(key.id())) course.put() return int(key.id())
def new_course(content): """ create a new course entity; return the id of the new course content should conform to: content = { 'teacher' : (str or int, Required), 'title' : (string), 'body' : (string), 'private' : (boolean), } """ teacher_id = content['teacher'] teacher = get_user(teacher_id) if teacher is None: raise Exception("invalid user id: that user does not exist") if 'title' not in content: content['title'] = '' if 'body' not in content: content['body'] = '' if 'private' not in content or content['private'] != True: content['private'] = False content['approved_students'] = [] content['pending_approval'] = [] content['units'] = [] content['listed'] = False key = Curriculum(content_type = 'course', content = content).put() if teacher.courses: teacher.courses.append(int(key.id())) else: teacher.courses = [int(key.id())] teacher.put() return int(key.id())