Beispiel #1
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
Beispiel #2
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 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)