예제 #1
0
def admin_post_edit(post_id: int):
    try:
        categories = CategoriesService.get_all_categories()
        authors = AuthorsService.get_all_authors()
        post = PostsService.get_post_by_id(post_id)
        if request.method == "GET":
            # Отрендерить шаблон admin_post.html для редактирования существующего поста
            return render_template("admin_post.html",
                                   categories=categories,
                                   authors=authors,
                                   post=post)
        elif request.method == "POST":
            preview = None
            fs = request.files.get("preview")
            if fs:
                preview = FilesService.save_file(fs)
            # Редактируем существующий пост
            edited_post = PostsService.edit_post_by_id(
                post_id,
                request.form.get("title"),
                request.form.get("category_id"),
                request.form.get("author_id"),
                request.form.get("body"),
            )
            return redirect(url_for("admin.admin_posts"))
    except PostsServiceNotFoundException:
        abort(404)
예제 #2
0
def catalog_page():
    try:
        page = int(request.args.get("page", 1))
        posts = PostsService.get_all_posts(limit=2, page=page)
        count = PostsService.get_count()
        page_count = math.ceil(count / 2)
        return render_template("catalog.html",
                               posts=posts,
                               page_count=page_count,
                               int=int)
    except PostsServiceNotFoundException as error:
        abort(404)
예제 #3
0
def admin_post_edit(post_id: int):
    categories = CategoriesService.get_all_categories()
    authors = AuthorsService.get_all_authors()
    post = PostsService.get_post_by_id(post_id)
    if request.method == "GET":
        # Отрендерить шаблон admin_post.html для редактирования существующего поста
        return render_template("admin_post_edit.html",
                               categories=categories,
                               authors=authors,
                               post=post)
    elif request.method == "POST":
        # Редактируем существующий пост
        edited_post = PostsService.edit_post_by_id(
            post_id,
            request.form.get("title"),
            request.form.get("category_id"),
            request.form.get("author_id"),
            request.form.get("body"),
        )
        return redirect(url_for("admin_posts"))
예제 #4
0
def admin_post_new():
    categories = CategoriesService.get_all_categories()
    authors = AuthorsService.get_all_authors()
    if request.method == "GET":
        return render_template("admin_post.html",
                               categories=categories,
                               authors=authors)
    elif request.method == "POST":
        created_post = PostsService.create_new_post(
            request.form.get("title"),
            request.form.get("category_id"),
            request.form.get("author_id"),
            request.form.get("body"),
        )
        return redirect(url_for("admin_posts"))
예제 #5
0
def index_page():
    """
    Функция обработчик запросов, получающая запрос и возвращающая html ответ из шаблона (index.html)

    Для формирования страницы используется сервис PostsService
    """
    posts = PostsService.get_all_posts()
    categories = CategoriesService.get_all_categories()
    authors = AuthorsService.get_all_authors()
    first_posts = posts[:2]
    posts = posts[2:]
    return render_template(
        "index.html",
        first_posts=first_posts,
        posts=posts,
        categories=categories,
        authors=authors,
    )
예제 #6
0
def admin_post_new():
    """
    View-Function для обработки запросов со страницы создания нового поста
    """
    categories = CategoriesService.get_all_categories()
    authors = AuthorsService.get_all_authors()
    if request.method == "GET":
        # Отрендерить шаблон admin_post.html для создания нового поста
        return render_template("admin_post.html",
                               categories=categories,
                               authors=authors)
    elif request.method == "POST":
        preview = None
        fs = request.files.get("preview")
        if fs:
            preview = FilesService.save_file(fs)
        # Создать новый пост
        created_post = PostsService.create_new_post(
            request.form.get("title"), request.form.get("category_id"),
            request.form.get("author_id"), request.form.get("body"), preview)
        return redirect(url_for("admin.admin_posts"))
def delete_post(post_id):
    return jsonify(PostsService().delete(post_id))
def update_post(post_id):
    return jsonify(PostsService().update(post_id, request.get_json()))
def create_post():
    return jsonify(PostsService().create(request.get_json()))
예제 #10
0
def list_post():
    return jsonify(PostsService().list())
예제 #11
0
def admin_posts():
    posts = PostsService.get_all_posts()
    return render_template("admin_posts.html", posts=posts)
예제 #12
0
def admin_posts_delete(post_id: int):
    PostsService.delete_pots_by_id(post_id)
    return redirect(url_for("admin.admin_posts"))
예제 #13
0
def author_page(author_id):
    author = AuthorsService.get_author_by_id(author_id)
    author_posts = PostsService.get_all_posts_by_author(author_id)
    return render_template("author.html",
                           author=author,
                           author_posts=author_posts)
예제 #14
0
def category_page(category_id):
    category = CategoriesService.get_category_by_id(category_id)
    category_posts = PostsService.get_all_posts_by_category(category_id)
    return render_template("category.html",
                           category=category,
                           category_posts=category_posts)
예제 #15
0
def post_page(post_id):
    post = PostsService.get_post_by_id(post_id)
    return render_template("article.html", post=post)
예제 #16
0
def post_page(post_id):
    try:
        post = PostsService.get_post_by_id(post_id)
        return render_template("article.html", post=post)
    except PostsServiceNotFoundException as error:
        abort(404)