def create_sample_course_framework(self, NC, NU, NL): """ create a user, courses, units, and lessons for the purpose of testing the api NC is the number of courses to create NU is the number of units, NL is the number of lessons """ user = self.create_and_return_local_user() course_id_bank = [] for course in range(NC): course_id = new_course({ 'title': 'Foo Course %s' % course, 'teacher': user.key.id() }) course_id_bank.append(course_id) unit_id_bank = [] for course_id in course_id_bank: for unit in range(NU): unit_id = new_unit({ 'title': 'Foo Unit %s' % unit, 'course': course_id }) unit_id_bank.append(unit_id) for unit_id in unit_id_bank: for lesson in range(NL): new_lesson({ 'title': 'Foo Lesson %s' % lesson, 'unit': unit_id })
def create_sample_course_framework(self,NC,NU,NL): """ create a user, courses, units, and lessons for the purpose of testing the api NC is the number of courses to create NU is the number of units, NL is the number of lessons """ user = self.create_and_return_local_user() course_id_bank = [] for course in range(NC): course_id = new_course({ 'title' : 'Foo Course %s' % course, 'teacher' : user.key.id() }) course_id_bank.append(course_id) unit_id_bank = [] for course_id in course_id_bank: for unit in range(NU): unit_id = new_unit({ 'title' : 'Foo Unit %s' % unit, 'course' : course_id }) unit_id_bank.append(unit_id) for unit_id in unit_id_bank: for lesson in range(NL): new_lesson({ 'title' : 'Foo Lesson %s' % lesson, 'unit' : unit_id })
def test_new_lesson_creation(self): """ assert that a new lesson can be created using the new lesson function """ local_user = self.create_and_return_local_user() course_id = new_course({ 'teacher': local_user.key.id(), 'title': 'foo course', 'body': 'hey look mom', }) unit_id = new_unit({ 'course': course_id, 'title': 'foo unit', 'body': 'bla bla unit body', }) lesson_id = new_lesson({ 'unit': unit_id, 'title': 'foo lesson', 'body': 'lesson about stuff' }) unit = ndb.Key('Curriculum', unit_id).get() course = ndb.Key('Curriculum', course_id).get() lesson = ndb.Key('Curriculum', lesson_id).get() # check that the correct content properties were set self.assertEqual(lesson.content['title'], 'foo lesson') self.assertEqual(lesson.content['body'], 'lesson about stuff') # check that the correct inferred properties were set self.assertEqual(lesson.content['course'], course_id) self.assertEqual(lesson.content['unit'], unit_id) self.assertEqual(lesson.content['teacher'], int(local_user.key.id())) self.assertEqual(lesson.content_type, 'lesson') # check that the parent unit correctly had this new lesson appended self.assertIn(lesson_id, unit.content['lessons'])
def test_new_lesson_creation(self): """ assert that a new lesson can be created using the new lesson function """ local_user = self.create_and_return_local_user() course_id = new_course({ 'teacher' : local_user.key.id(), 'title' : 'foo course', 'body' : 'hey look mom', }) unit_id = new_unit({ 'course' : course_id, 'title' : 'foo unit', 'body' : 'bla bla unit body', }) lesson_id = new_lesson({ 'unit' : unit_id, 'title' : 'foo lesson', 'body' : 'lesson about stuff' }) unit = ndb.Key('Curriculum', unit_id).get() course = ndb.Key('Curriculum', course_id).get() lesson = ndb.Key('Curriculum', lesson_id).get() # check that the correct content properties were set self.assertEqual(lesson.content['title'], 'foo lesson') self.assertEqual(lesson.content['body'], 'lesson about stuff') # check that the correct inferred properties were set self.assertEqual(lesson.content['course'], course_id) self.assertEqual(lesson.content['unit'], unit_id) self.assertEqual(lesson.content['teacher'], int(local_user.key.id())) self.assertEqual(lesson.content_type, 'lesson') # check that the parent unit correctly had this new lesson appended self.assertIn(lesson_id, unit.content['lessons'])
def post(self, content_id=None): """ handle the post request for the CurriculumAPI if no content_id then assumption is new entity, else the assumption is to edit an existing entity """ if not users.get_current_user(): return {'error': 'you must be logged in'}, 401 else: parser = reqparse.RequestParser() parser.add_argument('content_type', type=str) parser.add_argument('teacher', type=str) parser.add_argument('title', type=str) parser.add_argument('body', type=str) parser.add_argument('private', type=str) parser.add_argument('course', type=str) parser.add_argument('unit', type=str) args = parser.parse_args() try: content = {} content_type = args['content_type'] if content_type not in ['course', 'unit', 'lesson']: raise TypeError('invalid content type') googleID = users.get_current_user().user_id() content['teacher'] = get_user_by_google_id(googleID).key.id() if content_type == 'lesson': # the first line of the lesson body IS the title title = args['body'].strip().splitlines()[0] content['title'] = re.sub('^#+', '', title) else: content['title'] = args['title'] content['body'] = args['body'] content['private'] = args['private'] if not content_id: if content_type == 'course': content_id = new_course(content) if content_type == 'unit': content['course'] = args['course'] content_id = new_unit(content) if content_type == 'lesson': content['unit'] = args['unit'] content_id = new_lesson(content) else: modify_content(content, content_id) new_content = get_content_by_id(content_id) data = content_to_dict(new_content) except Exception as e: logging.info(e) return {'error' : str(e)}, 500 else: return data
def post(self, contentID=None): """ handle the post request for the CurriculumAPI if no contentID then assumption is new entity, else the assumption is to edit an existing entity acceptable params of the data sent in the post request include: 'content_type' : str 'title' : str 'body' : str 'private' : bool """ try: content_type = self.request.POST.get('content_type') if content_type not in ['course', 'unit', 'lesson']: raise TypeError('invalid content type') googleID = users.get_current_user().user_id() content = {} content['teacher'] = get_user_by_google_id(googleID).key.id() if content_type == 'lesson': # the first line of the lesson body IS the title body = self.request.POST.get('body') title = body.splitlines()[0] content['title'] = re.sub('^#+', '', title) else: content['title'] = self.request.POST.get('title') content['body'] = self.request.POST.get('body') content['private'] = self.request.POST.get('private') if contentID is None: if content_type == 'course': contentID = new_course(content) if content_type == 'unit': content['course'] = self.request.POST.get('course') contentID = new_unit(content) if content_type == 'lesson': content['unit'] = self.request.POST.get('unit') contentID = new_lesson(content) else: modify_content(content, contentID) new_content = get_content_by_id(contentID) data = content_to_dict(new_content) except Exception as e: data = {'error': str(e)} logging.info(data) self.write_json(data)
def post(self, contentID=None): """ handle the post request for the CurriculumAPI if no contentID then assumption is new entity, else the assumption is to edit an existing entity acceptable params of the data sent in the post request include: 'content_type' : str 'title' : str 'body' : str 'private' : bool """ try: content_type = self.request.POST.get('content_type') if content_type not in ['course', 'unit', 'lesson']: raise TypeError('invalid content type') googleID = users.get_current_user().user_id() content = {} content['teacher'] = get_user_by_google_id(googleID).key.id() if content_type == 'lesson': # the first line of the lesson body IS the title body = self.request.POST.get('body') title = body.splitlines()[0] content['title'] = re.sub('^#+', '', title) else: content['title'] = self.request.POST.get('title') content['body'] = self.request.POST.get('body') content['private'] = self.request.POST.get('private') if contentID is None: if content_type == 'course': contentID = new_course(content) if content_type == 'unit': content['course'] = self.request.POST.get('course') contentID = new_unit(content) if content_type == 'lesson': content['unit'] = self.request.POST.get('unit') contentID = new_lesson(content) else: modify_content(content, contentID) new_content = get_content_by_id(contentID) data = content_to_dict(new_content) except Exception as e: data = {'error' : str(e)} logging.info(data) self.write_json(data)
def get(self, migration_type=None): current_user = users.get_current_user() if current_user.email() != '*****@*****.**': abort(403) else: if migration_type == 'clear_content': ndb.delete_multi(Curriculum.query().fetch(keys_only=True)) elif migration_type == 'clear_teacher_courses': teachers = User.query() for teacher in teachers: logging.info('clearing courses for %s' % teacher.key.id()) teacher.courses = [] teacher.put() logging.info('Completed clearing courses for %s' % teacher.key.id()) elif migration_type == 'course': courses = Content.query(Content.contentType == 'course') for course in courses: if course.listed != 'done_migrating3': try: logging.info("Begin course migration for %s" % course.key.id()) app_user = course.key.parent().get() teacher = get_user_by_google_id(app_user.googleID) course_data = { 'teacher' : teacher.key.id(), 'title' : course.title, 'body' : course.body } new_course_id = new_course(course_data) logging.info("Saved data for Curriculum ID: %s" % new_course_id) units = Content.query(Content.contentType == 'unit', ancestor=course.key) for unit in units: logging.info("Begin unit migration for %s" % unit.key.id()) unit_data = { 'teacher' : teacher.key.id(), 'course' : new_course_id, 'title' : unit.title, 'body' : unit.body } new_unit_id = new_unit(unit_data) logging.info("Saved data for Unit ID: %s" % new_unit_id) lessons = Content.query(Content.contentType == 'lesson', ancestor=unit.key) for lesson in lessons: logging.info("Begin lesson migration for %s" % lesson.key.id()) lesson_data = { 'teacher' : teacher.key.id(), 'course' : new_course_id, 'unit' : new_unit_id, 'title' : lesson.title, 'body' : lesson.body } lesson_id = new_lesson(lesson_data) logging.info("Saved data for Lesson ID: %s" % lesson_id) course.listed = 'done_migrating3' course.put() logging.info("Finished course migration for %s" % course.key.id()) except Exception as e: logging.info("Error migrating course %s" % course.key.id()) logging.info(str(e)) return render_template( 'migrate.html', status_msg = 'migration complete' )