def get(self, content_id): """ handle the get request for the CurriculumAPI, exceptions are caught and passed in the 'error' param """ try: content = get_content_by_id(content_id) data = content_to_dict(content) logging.info('made it here 1') if content.content_type == 'course': logging.info('made it here 2') unit_list = [] for unit_id in content.content['units']: logging.info('made it here in unit: %s' % unit_id) unit = get_content_by_id(unit_id) unit_list.append(content_to_dict(unit)) data['units'] = unit_list if content.content_type == 'unit': lesson_list = [] for lesson_id in content.content['lessons']: lesson = get_content_by_id(lesson_id) lesson_list.append(content_to_dict(lesson)) data['lessons'] = lesson_list except Exception as e: return {'error' : str(e)}, 500 else: return data
def delete_content(content_id): """ delete the content if possible """ active_user = get_active_user() if not active_user: raise NameError("You are unauthorized to perform this action") else: content_object = get_content_by_id(content_id) content_type = content_object.content_type if int(content_object.content['teacher']) == int(active_user.key.id()): if content_type == 'course': active_user.courses.remove(int(content_object.key.id())) active_user.put() elif content_type == 'unit': course_id = content_object.content['course'] parent_course = get_content_by_id(course_id) parent_course.content['units'].remove(int(content_object.key.id())) parent_course.put() elif content_type == 'lesson': unit_id = content_object.content['unit'] parent_unit = get_content_by_id(course_id) parent_unit.content['lessons'].remove(int(content_object.key.id())) parent_unit.put() content_object.key.delete()
def delete_content(content_id): """ delete the content if possible """ active_user = get_active_user() if not active_user: raise NameError("You are unauthorized to perform this action") else: content_object = get_content_by_id(content_id) content_type = content_object.content_type if int(content_object.content['teacher']) == int(active_user.key.id()): if content_type == 'course': active_user.courses.remove(int(content_object.key.id())) active_user.put() elif content_type == 'unit': course_id = content_object.content['course'] parent_course = get_content_by_id(course_id) parent_course.content['units'].remove( int(content_object.key.id())) parent_course.put() elif content_type == 'lesson': unit_id = content_object.content['unit'] parent_unit = get_content_by_id(course_id) parent_unit.content['lessons'].remove( int(content_object.key.id())) parent_unit.put() content_object.key.delete()
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 modify_content(modified_content, content_id): """ edit existing content, return the id of that content object """ content_object = get_content_by_id(content_id) if 'title' in modified_content: content_object.content['title'] = modified_content['title'] if 'body' in modified_content: content_object.content['body'] = modified_content['body'] content_object.put() return content_id
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 test_simple_content_model_conversion(self): """ create a simple model and pass it to the content_to_dict controller """ local_user = self.create_and_return_local_user() course_id = new_course({ 'teacher' : local_user.key.id(), 'title' : 'foo course', 'body' : 'hey look mom', }) course = get_content_by_id(course_id) dict_content = content_to_dict(course) self.assertEqual(dict_content['content_type'], 'course') self.assertEqual(dict_content['content']['title'], 'foo course')
def get(self, contentID): """ handle the get request for the CurriculumAPI, exceptions are caught and passed in the 'error' param """ try: content = get_content_by_id(contentID) data = content_to_dict(content) if content.content_type == 'course': unit_list = [] for unit_id in content.content['units']: unit = get_content_by_id(unit_id) unit_list.append(content_to_dict(unit)) data['units'] = unit_list if content.content_type == 'unit': lesson_list = [] for lesson_id in content.content['lessons']: lesson = get_content_by_id(lesson_id) lesson_list.append(content_to_dict(lesson)) data['lessons'] = lesson_list except Exception as e: data = {'error': str(e)} self.write_json(data)
def get(self, contentID): """ handle the get request for the CurriculumAPI, exceptions are caught and passed in the 'error' param """ try: content = get_content_by_id(contentID) data = content_to_dict(content) if content.content_type == 'course': unit_list = [] for unit_id in content.content['units']: unit = get_content_by_id(unit_id) unit_list.append(content_to_dict(unit)) data['units'] = unit_list if content.content_type == 'unit': lesson_list = [] for lesson_id in content.content['lessons']: lesson = get_content_by_id(lesson_id) lesson_list.append(content_to_dict(lesson)) data['lessons'] = lesson_list except Exception as e: data = {'error' : str(e)} self.write_json(data)