~homeworkprod/byceps

ref: 533662eefb314d54b168e5a46bc07c88b5f88be4 byceps/byceps/blueprints/site/board/views_category.py -rw-r--r-- 2.6 KiB
533662ee — Jochen Kupperschmidt Use Flask v2.0's `delete`/`get`/`patch`/`post` route shortcuts 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
byceps.blueprints.site.board.views_category
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from flask import abort, g, url_for
from flask_babel import gettext

from ....services.board import (
    category_query_service as board_category_query_service,
    last_view_service as board_last_view_service,
    topic_query_service as board_topic_query_service,
)
from ....util.framework.flash import flash_success
from ....util.framework.templating import templated
from ....util.views import respond_no_content_with_location

from .blueprint import blueprint
from . import _helpers as h, service


@blueprint.get('/')
@templated
def category_index():
    """List categories."""
    board_id = h.get_board_id()
    user = g.user

    h.require_board_access(board_id, user.id)

    categories = board_category_query_service.get_categories_with_last_updates(
        board_id
    )

    categories_with_flag = service.add_unseen_postings_flag_to_categories(
        categories, user
    )

    return {
        'categories': categories_with_flag,
    }


@blueprint.get('/categories/<slug>', defaults={'page': 1})
@blueprint.get('/categories/<slug>/pages/<int:page>')
@templated
def category_view(slug, page):
    """List latest topics in the category."""
    board_id = h.get_board_id()
    user = g.user

    h.require_board_access(board_id, user.id)

    category = board_category_query_service.find_category_by_slug(
        board_id, slug
    )

    if category is None:
        abort(404)

    if category.hidden:
        abort(404)

    if user.authenticated:
        board_last_view_service.mark_category_as_just_viewed(
            category.id, user.id
        )

    include_hidden = service.may_current_user_view_hidden()
    topics_per_page = service.get_topics_per_page_value()

    topics = board_topic_query_service.paginate_topics_of_category(
        category.id, include_hidden, page, topics_per_page
    )

    service.add_topic_creators(topics.items)
    service.add_topic_unseen_flag(topics.items, user)

    return {
        'category': category,
        'topics': topics,
    }


@blueprint.post('/categories/<category_id>/mark_all_topics_as_read')
@respond_no_content_with_location
def mark_all_topics_in_category_as_viewed(category_id):
    category = h.get_category_or_404(category_id)

    board_last_view_service.mark_all_topics_in_category_as_viewed(
        category_id, g.user.id
    )

    flash_success(
        gettext('All topics in this category have been marked as read.')
    )

    return url_for('.category_view', slug=category.slug)