Пример #1
0
def view_draft(id):
    post = orm.select(p for p in Post if p.id == id).get()

    if post is None:
        return abort(404)

    if request.method == "POST":
        post.title = request.form["title"]
        post.content = request.form["content"]
        post.header_image = request.form["header_image"]
        post.header_caption = request.form["header_caption"]

        if post.draft:
            # Only update the slug if the post is a draft.
            post.slug = slugify(post.title)
        elif not post.is_local_header_image():
            post.download_header_image()  # Possible race condition

        resp = {"success": False}

        try:
            orm.commit()
        except CommitException:
            resp["error"] = "Title must be unique"
        else:
            resp["success"] = True

        post.clear_cache()

        return Response(json.dumps(resp),  mimetype='application/json')

    return render_template("edit_post.html",
                           post=post)
Пример #2
0
def view_draft(id):
    post = orm.select(p for p in Post if p.id == id).get()

    if post is None:
        return abort(404)

    if request.method == "POST":
        post.title = request.form["title"]
        post.content = request.form["content"]
        post.header_image = request.form["header_image"]
        post.header_caption = request.form["header_caption"]

        if post.draft:
            # Only update the slug if the post is a draft.
            post.slug = slugify(post.title)
        elif not post.is_local_header_image():
            post.download_header_image()  # Possible race condition

        resp = {"success": False}

        try:
            orm.commit()
        except CommitException:
            resp["error"] = "Title must be unique"
        else:
            resp["success"] = True

        post.clear_cache()

        return Response(json.dumps(resp),  mimetype='application/json')

    return render_template("edit_post.html",
                           post=post)
Пример #3
0
def new_post():
    p = Post(title=request.form["title"],
             slug=slugify(request.form["title"]),
             created=datetime.date.today(),
             modified=datetime.datetime.now(),
             content=" ",
             is_special_page=False,
             draft=True)

    resp = {"success": False}

    try:
        orm.commit()
    except CommitException:
        resp["error"] = "Title must be unique"
    else:
        resp["success"] = True
        resp["url"] = url_for("view_draft", id=p.id)

    return Response(json.dumps(resp), mimetype='application/json')
Пример #4
0
def fake_posts(count=10):
    """ Create an empty database and fill it with fake posts """
    try:
        from faker import Faker
        fake = Faker()
    except ImportError:
        print(
            "Error: module fake-factory is not installed. Cannot create fake data"
        )
        return

    try:
        from simple.app import orm, Post
        from simple.util import slugify
    except Exception:
        print("Error: Cannot import simple. Have you created a config file?")
        return

    start_time = time.time()

    with orm.db_session():
        if orm.select(p for p in Post).count() == 0:
            for i in range(count):
                title = fake.lorem.sentence(100)[:100]
                content = fake.lorem.paragraphs(20)

                created = fake.date_time_this_month()
                posted = fake.date_time_this_month()
                modified = fake.date_time_this_month()

                p = Post(title=title,
                         slug=slugify(title),
                         created=created,
                         posted=posted,
                         modified=modified,
                         is_special_page=False,
                         content=content,
                         draft=random.choice((True, False)))
            orm.commit()

    print("Created {0} posts in {1}".format(count, time.time() - start_time))
Пример #5
0
def fake_posts(count=10):
    """ Create an empty database and fill it with fake posts """
    try:
        from faker import Faker
        fake = Faker()
    except ImportError:
        print("Error: module fake-factory is not installed. Cannot create fake data")
        return

    try:
        from simple.app import orm, Post
        from simple.util import slugify
    except Exception:
        print("Error: Cannot import simple. Have you created a config file?")
        return

    start_time = time.time()

    with orm.db_session():
        if orm.select(p for p in Post).count() == 0:
            for i in range(count):
                title = fake.lorem.sentence(100)[:100]
                content = fake.lorem.paragraphs(20)

                created = fake.date_time_this_month()
                posted = fake.date_time_this_month()
                modified = fake.date_time_this_month()

                p = Post(
                    title=title,
                    slug=slugify(title),
                    created=created,
                    posted=posted,
                    modified=modified,
                    is_special_page=False,
                    content=content,
                    draft=random.choice((True, False))
                )
            orm.commit()

    print("Created {0} posts in {1}".format(count, time.time()-start_time))
Пример #6
0
def new_post():
    p = Post(
        title=request.form["title"],
        slug=slugify(request.form["title"]),
        created=datetime.date.today(),
        modified=datetime.datetime.now(),
        content=" ",
        is_special_page=False,
        draft=True
    )

    resp = {"success": False}

    try:
        orm.commit()
    except CommitException:
        resp["error"] = "Title must be unique"
    else:
        resp["success"] = True
        resp["url"] = url_for("view_draft", id=p.id)

    return Response(json.dumps(resp),  mimetype='application/json')