コード例 #1
0
def editStoryPage(category_id, story_id, page_id):
    # start an sql session
    session = create_session()
    # get category
    category = session.query(Category).get(category_id)
    # get story
    story = session.query(Story).get(story_id)
    # get page
    page = session.query(Story_Page).get(page_id)
    # post
    if request.method == 'POST':
        # edit the page
        page.name = request.form['name']
        page.description = request.form['description']
        page.text = request.form['text']
        # add and commit
        session.add(page)
        session.commit()
        # close session
        session.close()
        return redirect(
            url_for("editPages", category_id=category_id, story_id=story_id))
    else:
        session.close()
        user = get_user()
        return render_template("editStoryPage.html",
                               category=category,
                               story=story,
                               page=page,
                               user=user)
コード例 #2
0
def newStory():
    user = get_user()
    # start sql session
    session = create_session()
    # get categories
    categories = session.query(Category).all()
    # Protect this page by login
    if user == None:
        return redirect(url_for("login"))
    # if submitted form
    if request.method == 'POST':
        # get category that the story is being posted to
        category_id = request.form['category']
        # create new story
        story = Story(name=request.form['name'],
                      description=request.form['description'],
                      category_id=category_id,
                      owner_id=user.id)
        session.add(story)
        # close sql session
        session.commit()
        session.close()
        return redirect(url_for('showCategory', category_id=category_id))
    else:
        # close sql session
        session.close()
        return render_template("newStory.html",
                               categories=categories,
                               user=user)
コード例 #3
0
def editPages(category_id, story_id):
    # start an sql session
    session = create_session()
    # get category
    category = session.query(Category).get(category_id)
    # get story
    story = session.query(Story).get(story_id)
    # get list of pages
    pages = session.query(Story_Page).filter_by(story_id=story.id).all()
    # close session
    session.close()
    if len(pages) == 0:
        # redirect
        return redirect(
            url_for("addStoryPage",
                    category_id=category_id,
                    story_id=story_id,
                    linking_page_id=0))
    else:
        # get root page
        session = create_session()
        root_page = session.query(Story_Page).filter_by(story_id=story.id,
                                                        is_root=True).one()
        session.close()
        # create page tree
        page_tree = [create_page_tree(root_page, None, 0)]
        user = get_user()
        return render_template("editPages.html",
                               story=story,
                               category=category,
                               page_tree=page_tree,
                               user=user)
コード例 #4
0
def deleteStory(category_id, story_id):
    # start sql session
    session = create_session()
    # get category
    category = session.query(Category).get(category_id)
    cat_id = category.id
    # get story
    story = session.query(Story).get(story_id)
    if request.method == 'POST':
        # delete all page links associated with story
        session.query(Page_Link).filter_by(story_id=story.id).delete()
        # delete all pages associated with story
        session.query(Story_Page).filter_by(story_id=story.id).delete()
        # delete story
        session.query(Story).filter_by(id=story.id).delete()

        # close session and redirect
        session.commit()
        session.close()
        return redirect(url_for('showCategory', category_id=category_id))
    else:
        # close session
        session.close()
        user = get_user()
        return render_template("deleteStory.html",
                               story=story,
                               category=category,
                               user=user)
コード例 #5
0
def showProfile():
    # get user
    user = get_user()
    # if not logged in, login
    if not user:
        return redirect(url_for("login"))
    return render_template("profile.html", user=user)
コード例 #6
0
def showCategories():
    # get categories from database
    session = create_session()
    categories = session.query(Category).all()
    # close database session
    session.close()
    # get user if logged in
    user = get_user()
    return render_template("categories.html", categories=categories, user=user)
コード例 #7
0
def addStoryPage(category_id, story_id, linking_page_id):
    user = get_user()
    # start an sql session
    session = create_session()
    # get category
    category = session.query(Category).get(category_id)
    # get story
    story = session.query(Story).get(story_id)
    # Protect this page by login
    if user == None:
        return redirect(url_for("login"))
    owner = story.owner
    if user.id != owner.id:
        return abort(401)
    # get linking_page
    linking_page = None
    if linking_page_id != 0:
        linking_page = session.query(Story_Page).get(linking_page_id)
    # post
    if request.method == 'POST':
        # check if root
        is_root = False
        if linking_page_id == 0:
            is_root = True
        # create the page
        page = Story_Page(name=request.form['name'],
                          description=request.form['description'],
                          text=request.form['text'],
                          is_root=is_root,
                          story_id=story.id)
        session.add(page)
        session.commit()
        # create a page link if not root
        if not is_root:
            page_link = Page_Link(base_page_id=linking_page_id,
                                  linked_page_id=page.id,
                                  story_id=story.id)
            session.add(page_link)
            session.commit()
        # close session
        session.close()
        return redirect(url_for("editPages",
                                category_id=category_id,
                                story_id=story_id))
    else:
        session.close()
        return render_template("newStoryPage.html",
                               category=category,
                               story=story,
                               linking_page=linking_page,
                               user=user)
コード例 #8
0
def showCategory(category_id):
    # start sql session
    session = create_session()
    # get category and check it's valid
    category = session.query(Category).get(category_id)
    if not category:
        return None
    # get stories
    stories = session.query(Story).filter_by(category_id=category.id)
    # close database session
    session.close()
    # get user if logged in
    user = get_user()
    return render_template("showCategory.html",
                           category=category,
                           stories=stories,
                           user=user)
コード例 #9
0
def showStory(category_id, story_id, page_id):
    # start sql session
    session = create_session()
    # get category
    category = session.query(Story).get(category_id)
    if not category:
        return None
    # get story
    story = session.query(Story).get(story_id)
    if not story:
        return None
    # get root page if a page is not specified
    page = None
    if page_id == 0:
        page_query = session.query(Story_Page).filter_by(is_root=True,
                                                         story_id=story.id)
        if page_query.count() > 0:
            page = page_query.one()
    # page was specified
    else:
        page = session.query(Story_Page).get(page_id)
    # get linked pages
    linked_pages = []
    if page:
        linked_pages = session.query(Story_Page).     \
            filter(Story_Page.id==Page_Link.linked_page_id). \
            filter(Page_Link.base_page_id==page.id).         \
            all()
    # close sql session
    session.close()
    # get user
    user = get_user()
    return render_template("showStory.html",
                           category=category,
                           story=story,
                           page=page,
                           linked_pages=linked_pages,
                           user=user)
コード例 #10
0
def deleteStoryPage(category_id, story_id, page_id):
    user = get_user()
    # start an sql session
    session = create_session()
    # get category
    category = session.query(Category).get(category_id)
    # get story
    story = session.query(Story).get(story_id)
    # get page
    page = session.query(Story_Page).get(page_id)
    # Protect this page by login
    if user == None:
        return redirect(url_for("login"))
    owner = story.owner
    if user.id != owner.id:
        return abort(401)
    # post
    if request.method == 'POST':
        # delete and commit
        session.query(Story_Page).filter_by(id=page_id).delete()
        session.query(Page_Link).filter_by(
                        linked_page_id=page_id).delete()
        session.query(Page_Link).filter_by(
                        base_page_id=page_id).delete()
        session.commit()
        # close session
        session.close()
        return redirect(url_for("editPages",
                                category_id=category_id,
                                story_id=story_id))
    else:
        session.close()
        return render_template("deleteStoryPage.html",
                               category=category,
                               story=story,
                               page=page,
                               user=user)
コード例 #11
0
def editStory(category_id, story_id):
    # start sql session
    session = create_session()
    # get category
    category = session.query(Category).get(category_id)
    cat_id = category.id
    # get story
    story = session.query(Story).get(story_id)
    if request.method == 'POST':
        story.name = request.form['name']
        story.description = request.form['description']
        session.add(story)
        session.commit()
        # close sql session and redirect
        session.close()
        return redirect(url_for('showCategory', category_id=cat_id))
    else:
        # close sql session
        session.close()
        user = get_user()
        return render_template("editStory.html",
                               category=category,
                               story=story,
                               user=user)
コード例 #12
0
def showHome():
    user = get_user()
    return render_template("home.html", user=user)