Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
    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)
Пример #4
0
    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')
Пример #6
0
    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)
Пример #7
0
    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)