Exemple #1
0
def wiki(name='MainPage'):
    if request.method == 'POST':
    #save
        if 'save' in request.form:
            wiki = Wiki.query.filter_by(title=name).first()
            form = WikiEditForm()
            if wiki:
                wiki.text = form.wikitext.data
                wiki.store_to_db()
            else: 
                wiki = Wiki(title=name, text=form.wikitext.data)
                try:
                    wiki.store_to_db()
                except:
                    app.logger.debug('failed')
            return redirect(url_for('wiki', name=name))
    #edit
        if 'edit' in request.form:
            wiki = Wiki.query.filter_by(title=name).first()
            if not wiki:
                flash("Creat a new page")
                form = WikiEditForm()
                form.set_default_text(text="Input text here")
                return render_template("wiki/edit.html", form=form, name = name)
            else:
                text = wiki.text
                form = WikiEditForm()
                form.set_default_text(text=text)
                return render_template("wiki/edit.html", form=form, name = name)
        if 'attach' in request.form:
            return render_template("wiki/attach.html", form=form, name = name)
        else:
            form = WikiShowForm()
            # get it from database
            wiki = Wiki.query.filter_by(title=name).first()
            # render to template
            return render_template("wiki/show.html", form=form, wiki=wiki, name=name)
    else:
        form = WikiShowForm()
        # get it from database
        wiki = Wiki.query.filter_by(title=name).first()
        # render to template
        return render_template("wiki/show.html", form=form, wiki=wiki, name=name)
Exemple #2
0
def initdb():
    try:
        db.create_all()
    except:
        db.drop_all()
    # add some records
    try:
        admin_pass = generate_password_hash("admin", method="sha1", salt_length=8)
        guest_pass = generate_password_hash("guest", method="sha1", salt_length=8)
        admin = User("admin", admin_pass, "*****@*****.**")
        guest = User("guest", guest_pass, "*****@*****.**")
        # save them into database
        admin.store_to_db()
        flash("user admin added")
        guest.store_to_db()
        flash("user guest added")
    except:
        flash("create User table failed.", "error")

    try:
        todo1 = Todo("check in code", "modify reset")
        todo2 = Todo("talk with somebody", "about cloud")
        todo1.store_to_db()
        flash("todo #1 added")
        todo2.store_to_db()
        flash("todo #2 added")
    except:
        flash("create Todo table failed.", "error")

    try:
        wiki1 = Wiki("MainPage", "wikitest")
        wiki2 = Wiki("Sandbox", "wikitest")
        wiki1.store_to_db()
        flash("wiki #1 added")
        wiki2.store_to_db()
        flash("wiki #2 added")
    except:
        flash("create Wiki table failed.", "error")
    return redirect(url_for("index"))