~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/site/dashboard/views.py -rw-r--r-- 3.3 KiB
342e5296 — Jochen Kupperschmidt Require body blocks in base templates 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
byceps.blueprints.site.dashboard.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Current user's dashboard

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

from __future__ import annotations

from flask import abort, g

from ....services.authentication.session.models.current_user import CurrentUser
from ....services.board.dbmodels.topic import Topic as DbTopic
from ....services.board import (
    access_control_service as board_access_control_service,
    topic_query_service as board_topic_query_service,
)
from ....services.news import service as news_item_service
from ....services.news.transfer.models import Headline as NewsHeadline
from ....services.shop.order import service as order_service
from ....services.shop.order.transfer.models import Order
from ....services.shop.storefront import service as storefront_service
from ....services.site import service as site_service
from ....services.site.transfer.models import Site
from ....services.ticketing import ticket_service
from ....services.ticketing.dbmodels.ticket import Ticket as DbTicket
from ....services.user import service as user_service
from ....typing import UserID
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import login_required

from ..board import service as board_helper_service


blueprint = create_blueprint('dashboard', __name__)


@blueprint.route('')
@login_required
@templated
def index():
    """Show current user's dashboard."""
    user = user_service.find_active_db_user(g.user.id)
    if user is None:
        abort(404)

    site = site_service.get_site(g.site_id)

    open_orders = _get_open_orders(site, g.user.id)
    tickets = _get_tickets(g.user.id)
    news_headlines = _get_news_headlines(site)
    board_topics = _get_board_topics(site, g.user)

    return {
        'open_orders': open_orders,
        'tickets': tickets,
        'news_headlines': news_headlines,
        'board_topics': board_topics,
    }


def _get_open_orders(site: Site, user_id: UserID) -> list[Order]:
    storefront_id = site.storefront_id
    if storefront_id is None:
        return []

    storefront = storefront_service.get_storefront(storefront_id)

    orders = order_service.get_orders_placed_by_user_for_shop(
        user_id, storefront.shop_id
    )

    orders = [order for order in orders if order.is_open]

    return orders


def _get_tickets(user_id: UserID) -> list[DbTicket]:
    if g.party_id is None:
        return []

    return ticket_service.find_tickets_used_by_user(user_id, g.party_id)


def _get_news_headlines(site: Site) -> list[NewsHeadline]:
    channel_id = site.news_channel_id
    if channel_id is None:
        return []

    return news_item_service.get_recent_headlines(channel_id, limit=4)


def _get_board_topics(site: Site, current_user: CurrentUser) -> list[DbTopic]:
    board_id = site.board_id
    if board_id is None:
        return []

    has_access = board_access_control_service.has_user_access_to_board(
        current_user.id, board_id
    )
    if not has_access:
        return []

    include_hidden = board_helper_service.may_current_user_view_hidden()
    topics = board_topic_query_service.get_recent_topics(
        board_id, include_hidden, limit=6
    )

    board_helper_service.add_topic_unseen_flag(topics, current_user)

    return topics