Passed
Branch main (854eb5)
by Jochen
04:24
created

mark_all_topics_in_category_as_viewed()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 1
dl 0
loc 14
ccs 2
cts 6
cp 0.3333
crap 1.2963
rs 9.95
c 0
b 0
f 0
1
"""
2
byceps.blueprints.site.board.views_category
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from flask import abort, g, url_for
10 1
from flask_babel import gettext
11
12 1
from ....services.board import (
13
    category_query_service as board_category_query_service,
14
    last_view_service as board_last_view_service,
15
    topic_query_service as board_topic_query_service,
16
)
17 1
from ....util.framework.flash import flash_success
18 1
from ....util.framework.templating import templated
19 1
from ....util.views import respond_no_content_with_location
20
21 1
from .blueprint import blueprint
22 1
from . import _helpers as h, service
23
24
25 1
@blueprint.get('/')
26 1
@templated
27
def category_index():
28
    """List categories."""
29 1
    board_id = h.get_board_id()
30 1
    user = g.user
31
32 1
    h.require_board_access(board_id, user.id)
33
34 1
    categories = board_category_query_service.get_categories_with_last_updates(
35
        board_id
36
    )
37
38 1
    categories_with_flag = service.add_unseen_postings_flag_to_categories(
39
        categories, user
40
    )
41
42 1
    return {
43
        'categories': categories_with_flag,
44
    }
45
46
47 1
@blueprint.get('/categories/<slug>', defaults={'page': 1})
48 1
@blueprint.get('/categories/<slug>/pages/<int:page>')
49 1
@templated
50
def category_view(slug, page):
51
    """List latest topics in the category."""
52 1
    board_id = h.get_board_id()
53 1
    user = g.user
54
55 1
    h.require_board_access(board_id, user.id)
56
57 1
    category = board_category_query_service.find_category_by_slug(
58
        board_id, slug
59
    )
60
61 1
    if category is None:
62
        abort(404)
63
64 1
    if category.hidden:
65
        abort(404)
66
67 1
    if user.authenticated:
68 1
        board_last_view_service.mark_category_as_just_viewed(
69
            category.id, user.id
70
        )
71
72 1
    include_hidden = service.may_current_user_view_hidden()
73 1
    topics_per_page = service.get_topics_per_page_value()
74
75 1
    topics = board_topic_query_service.paginate_topics_of_category(
76
        category.id, include_hidden, page, topics_per_page
77
    )
78
79 1
    service.add_topic_creators(topics.items)
80 1
    service.add_topic_unseen_flag(topics.items, user)
81
82 1
    return {
83
        'category': category,
84
        'topics': topics,
85
    }
86
87
88 1
@blueprint.post('/categories/<category_id>/mark_all_topics_as_read')
89 1
@respond_no_content_with_location
90
def mark_all_topics_in_category_as_viewed(category_id):
91
    category = h.get_category_or_404(category_id)
92
93
    board_last_view_service.mark_all_topics_in_category_as_viewed(
94
        category_id, g.user.id
95
    )
96
97
    flash_success(
98
        gettext('All topics in this category have been marked as read.')
99
    )
100
101
    return url_for('.category_view', slug=category.slug)
102