def post(self):

        # upload new content on google drive
        user = UserData.get_current_user()
        if not user:  # make sure user has an account to create resource
            self.redirect("find-resource")

        content_index_list = self.request.get("content-index-list").split(",")
        logging.info("content_index_list: %s" % content_index_list)

        content_list = []
        for i in content_index_list:
            content_name = self.request.get("content-name-%s" % i)
            content_content = io.BytesIO(self.request.get("content-%s" % i))
            content_file = google_drive_api.insert_file(
                service, content_name, content_content, google_drive_api.DOCX_MIME_TYPE
            )

            new_content = Content(content_name=content_name, related_file_id=content_file["id"])
            new_content.put()
            content_list.append(new_content.key)
        # 我們不在這個時候寄邀請信給使用者,請他同意獲得講義的writer權限
        # 因為我認為使用者很可能不會去收信,我們在使用者第一次真的要使用線上編輯功能的時候
        # 再告知他們我們會寄信邀請他們,請他們去收信  -- By EN

        resource_name = self.request.get("resource-name")
        grade_chunk = self.request.get("grade-chunk")
        difficulty = self.request.get("difficulty")
        subject = self.request.get("subject")
        is_public = bool(int(self.request.get("is_public")))
        keywords = self.request.get("keywords")
        keyword_list = []
        if keywords:
            keyword_list = keywords.strip().split(",")
        keyword_list.append(subject)
        keyword_list.append(grade_chunk)
        keyword_list = set(keyword_list)
        new_resource = Resource(
            resource_name=resource_name,
            grade_chunk=grade_chunk,
            difficulty=difficulty,
            subject=subject,
            keywords=keyword_list,
            contents=content_list,
            creator=user.key,
            admins=[user.key],
            is_public=is_public,
        )
        new_resource.put()

        for keyword in keyword_list:
            keywordindex = KeyWordIndex.get_by_keyword_and_subject(keyword, subject)
            if keywordindex:
                keywordindex.resource_count = keywordindex.resource_count + 1
            else:
                keywordindex = KeyWordIndex(key_word=keyword, related_subject=subject)
            keywordindex.put()

        self.redirect("/show-resource/%s" % new_resource.key.id())
示例#2
0
    def post(self):

        # upload new material on google drive
        user = UserData.get_current_user()
        if not user:  # make sure user has an account to create course
            self.redirect('find-course')

        material_index_list = self.request.get('material-index-list').split(',')
        logging.info("material_index_list: %s" % material_index_list)

        material_list = []
        for i in material_index_list:
            material_name = self.request.get('material-name-%s' % i)
            material_content = io.BytesIO(self.request.get('material-content-%s' % i))
            material_file = google_drive_api.insert_file(service, material_name, material_content, google_drive_api.DOCX_MIME_TYPE)
            new_content = Content(material_name=material_name,
                                    related_file_id=material_file['id']
                                    )
            new_content.put()
            material_list.append(new_content.key)
        # 我們不在這個時候寄邀請信給使用者,請他同意獲得講義的writer權限
        # 因為我認為使用者很可能不會去收信,我們在使用者第一次真的要使用線上編輯功能的時候
        # 再告知他們我們會寄信邀請他們,請他們去收信  -- By EN

        course_name = self.request.get('resource-name')
        grade_chunk = self.request.get('grade-chunk')
        difficulty = self.request.get('difficulty')
        subject = self.request.get('subject')
        is_public = bool(int(self.request.get('is_public')))
        keywords = self.request.get('keywords')
        keyword_list = []
        if keywords:
            keyword_list = keywords.strip().split(',')
        keyword_list.append(subject)
        keyword_list.append(grade_chunk)
        keyword_list = set(keyword_list)
        new_course = Resource(course_name=course_name,
                            grade_chunk=grade_chunk,
                            difficulty=difficulty,
                            subject=subject,
                            keywords=keyword_list,
                            materials=material_list,
                            creator=user.key,
                            admins=[user.key],
                            is_public=is_public
                            )
        new_course.put()

        for keyword in keyword_list:
            keywordindex = KeyWordIndex.get_by_keyword_and_subject(keyword, subject)
            if keywordindex:
                keywordindex.course_count = keywordindex.course_count + 1
            else:
                keywordindex = KeyWordIndex(key_word=keyword, related_subject=subject)
            keywordindex.put()

        self.redirect('/show-course/%s' % new_course.key.id())