def delete_content(course, inpt, TOC): pages_path = syllabus.get_pages_path(course) content_path = inpt["content-path"] path = os.path.join(pages_path, content_path) content_to_delete = TOC.get_content_from_path(content_path) TOC.remove_content_from_toc(content_to_delete) # dump the TOC and reload it syllabus.save_toc(course, TOC) syllabus.get_toc(course, force=True) # remove the files if asked if inpt.get("delete-files", None) == "on": if type(content_to_delete) is Chapter: # delete a chapter shutil.rmtree(os.path.join(path)) else: # delete a page os.remove(path) set_feedback( session, Feedback(feedback_type="success", message="The content has been successfully deleted")) return seeother(request.path)
def content_edition(course): if not course in syllabus.get_config()["courses"].keys(): abort(404) TOC = syllabus.get_toc(course) if TOC.ignored and not has_feedback(session): set_feedback( session, Feedback(feedback_type="warning", message="The following contents have not been found :\n" + "<pre>" + "\n".join(TOC.ignored) + "</pre>")) if request.method == "POST": inpt = request.form if inpt["action"] == "delete_content": return delete_content(course, inpt, TOC) containing_chapter = None try: if inpt["containing-chapter"] != "": containing_chapter = TOC.get_chapter_from_path( inpt["containing-chapter"]) except ContentNotFoundError: set_feedback( session, Feedback(feedback_type="error", message="The containing chapter for this page " "does not exist")) return seeother(request.path) # sanity checks on the filename: refuse empty filenames of filenames with spaces if len(inpt["name"]) == 0 or len(re.sub( r'\s+', '', inpt["name"])) != len(inpt["name"]): set_feedback( session, Feedback( feedback_type="error", message='The file name cannot be empty or contain spaces.') ) return seeother(request.path) if os.sep in inpt["name"]: set_feedback( session, Feedback( feedback_type="error", message= 'The file name cannot contain the "%s" forbidden character.' % os.sep)) return seeother(request.path) pages_path = syllabus.get_pages_path() content_path = os.path.join( containing_chapter.path, inpt["name"]) if containing_chapter is not None else inpt["name"] path = os.path.join(pages_path, content_path) # ensure that we do not create a page at the top level if containing_chapter is None and inpt["action"] == "create_page": set_feedback( session, Feedback(feedback_type="error", message="Pages cannot be at the top level of the " "syllabus.")) return seeother(request.path) # check that there is no chapter with the same title in the containing chapter if containing_chapter is None: for content in TOC.get_top_level_content(): if content.title == inpt["title"]: set_feedback( session, Feedback(feedback_type="error", message="There is already a top level " "chapter with this title.")) return seeother(request.path) else: for content in TOC.get_direct_content_of(containing_chapter): if content.title == inpt["title"]: set_feedback( session, Feedback(feedback_type="error", message="There is already a chapter/page " "with this title in this chapter.")) return seeother(request.path) if inpt["action"] == "create_page": # add the extension to the page filename content_path += ".rst" path += ".rst" # when file/directory already exists if os.path.isdir(path) or os.path.isfile(path): set_feedback( session, Feedback(feedback_type="error", message="A file or directory with this name " "already exists.")) return seeother(request.path) # create a new page open(path, "w").close() page = Page(path=content_path, title=inpt["title"], pages_path=syllabus.get_pages_path(course)) TOC.add_content_in_toc(page) elif inpt["action"] == "create_chapter": # creating a new chapter try: os.mkdir(path) except FileExistsError: set_feedback( session, Feedback(feedback_type="error", message="A file or directory with this name " "already exists.")) return seeother(request.path) chapter = Chapter(path=content_path, title=inpt["title"], pages_path=syllabus.get_pages_path(course)) TOC.add_content_in_toc(chapter) # dump the TOC and reload it syllabus.save_toc(course, TOC) syllabus.get_toc(course, force=True) return seeother(request.path) try: return render_template('content_edition.html', active_element=sidebar['active_element'], course_str=course, sidebar_elements=sidebar['elements'], TOC=TOC, feedback=pop_feeback(session)) except TemplateNotFound: abort(404)