Beispiel #1
0
def edit_content(course, content_path, TOC: TableOfContent):
    try:
        content = TOC.get_page_from_path("%s.rst" % content_path)
    except ContentNotFoundError:
        content = TOC.get_content_from_path(content_path)
    if request.method == "POST":
        inpt = request.form
        if "new_content" not in inpt:
            return seeother(request.path)
        else:
            if type(content) is Chapter:
                with open(
                        safe_join(syllabus.get_pages_path(course),
                                  content.path, "chapter_introduction.rst"),
                        "w") as f:
                    f.write(inpt["new_content"])
            else:
                with open(
                        safe_join(syllabus.get_pages_path(course),
                                  content.path), "w") as f:
                    f.write(inpt["new_content"])
            return seeother(request.path)
    elif request.method == "GET":
        return render_template("edit_page.html",
                               content_data=get_content_data(course, content),
                               content=content,
                               TOC=TOC)
Beispiel #2
0
def toc_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 "new_content" in inpt:
            try:
                # check YAML validity
                toc_dict = yaml.load(inpt["new_content"],
                                     OrderedDictYAMLLoader)
                if not TableOfContent.is_toc_dict_valid(
                        syllabus.get_pages_path(course), toc_dict):
                    set_feedback(
                        session,
                        Feedback(feedback_type="error",
                                 message="The submitted table of contents "
                                 "is not consistent with the files "
                                 "located in the pages directory."))
                    return seeother(request.path)
            except yaml.YAMLError:
                set_feedback(
                    session,
                    Feedback(feedback_type="error",
                             message="The submitted table of contents is not "
                             "written in valid YAML."))
                return seeother(request.path)
            # the YAML is valid, write it in the ToC
            with open(
                    os.path.join(syllabus.get_pages_path(course), "toc.yaml"),
                    "w") as f:
                f.write(inpt["new_content"])
            syllabus.get_toc(course, force=True)
        set_feedback(
            session,
            Feedback(feedback_type="success",
                     message="The table of contents has been modified "
                     "successfully !"))
        return seeother(request.path)
    else:
        with open(os.path.join(syllabus.get_pages_path(course), "toc.yaml"),
                  "r") as f:
            try:
                return render_template(
                    'edit_table_of_content.html',
                    active_element=sidebar['active_element'],
                    sidebar_elements=sidebar['elements'],
                    content=f.read(),
                    feedback=pop_feeback(session))
            except TemplateNotFound:
                abort(404)
Beispiel #3
0
 def reload_toc():
     """ loads the TOC explicitely """
     # TODO: change this hack a bit ugly
     from syllabus.utils.toc import TableOfContent
     get_toc.TOC[course] = TableOfContent(course)
     return get_toc.TOC[course]