Completed
Push — main ( 80557c...4ad4e5 )
by Jochen
05:36
created

byceps.blueprints.admin.dashboard.views   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
eloc 115
dl 0
loc 187
ccs 66
cts 70
cp 0.9429
rs 10
c 0
b 0
f 0
wmc 7

3 Functions

Rating   Name   Duplication   Size   Complexity  
A view_global() 0 42 1
A view_party() 0 34 2
B view_brand() 0 58 4
1
"""
2
byceps.blueprints.admin.dashboard.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2020 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9 1
from datetime import date, timedelta
10
11 1
from flask import abort
12
13 1
from ....services.brand import (
14
    service as brand_service,
15
    settings_service as brand_settings_service,
16
)
17 1
from ....services.consent import subject_service as consent_subject_service
18 1
from ....services.newsletter import service as newsletter_service
19 1
from ....services.orga import service as orga_service
20 1
from ....services.orga import birthday_service as orga_birthday_service
21 1
from ....services.orga_team import service as orga_team_service
22 1
from ....services.party import service as party_service
23 1
from ....services.seating import (
24
    area_service as seating_area_service,
25
    seat_service,
26
)
27 1
from ....services.site import service as site_service
28 1
from ....services.ticketing import ticket_service
29 1
from ....services.user import stats_service as user_stats_service
30 1
from ....util.framework.blueprint import create_blueprint
31 1
from ....util.framework.templating import templated
32
33 1
from ...common.authorization.decorators import permission_required
34 1
from ...common.authorization.registry import permission_registry
35
36 1
from ..user.service import get_users_created_since
37
38 1
from .authorization import AdminDashboardPermission
39
40
41 1
blueprint = create_blueprint('admin_dashboard', __name__)
42
43
44 1
permission_registry.register_enum(AdminDashboardPermission)
45
46
47 1
@blueprint.route('')
48 1
@permission_required(AdminDashboardPermission.view_global)
49 1
@templated
50
def view_global():
51
    """View dashboard for global entities."""
52 1
    current_sites = site_service.get_current_sites(include_brands=True)
53 1
    active_parties = party_service.get_active_parties(include_brands=True)
54
55 1
    brands = brand_service.get_all_brands()
56 1
    party_count = party_service.count_parties()
57
58 1
    orga_count = orga_service.count_orgas()
59
60 1
    user_count = user_stats_service.count_users()
61
62 1
    one_week_ago = timedelta(days=7)
63 1
    recent_users = get_users_created_since(one_week_ago, limit=4)
64 1
    recent_users_count = user_stats_service.count_users_created_since(
65
        one_week_ago
66
    )
67
68 1
    uninitialized_user_count = user_stats_service.count_uninitialized_users()
69
70 1
    orgas_with_next_birthdays = list(
71
        orga_birthday_service.collect_orgas_with_next_birthdays(limit=3)
72
    )
73
74 1
    return {
75
        'current_sites': current_sites,
76
        'active_parties': active_parties,
77
78
        'brands': brands,
79
        'party_count': party_count,
80
81
        'orga_count': orga_count,
82
83
        'user_count': user_count,
84
        'recent_users': recent_users,
85
        'recent_users_count': recent_users_count,
86
        'uninitialized_user_count': uninitialized_user_count,
87
88
        'orgas_with_next_birthdays': orgas_with_next_birthdays,
89
    }
90
91
92 1
@blueprint.route('/brands/<brand_id>')
93 1
@permission_required(AdminDashboardPermission.view_brand)
94 1
@templated
95
def view_brand(brand_id):
96
    """View dashboard for that brand."""
97 1
    brand = brand_service.find_brand(brand_id)
98 1
    if brand is None:
99
        abort(404)
100
101 1
    current_sites = site_service.get_current_sites(
102
        brand_id=brand.id, include_brands=True
103
    )
104 1
    active_parties = party_service.get_active_parties(
105
        brand_id=brand.id, include_brands=True
106
    )
107
108 1
    party_count = party_service.count_parties_for_brand(brand.id)
109
110 1
    orga_count = orga_service.count_orgas_for_brand(brand.id)
111
112 1
    newsletter_list_id = brand_settings_service.find_setting_value(
113
        brand.id, 'newsletter_list_id'
114
    )
115 1
    newsletter_list = None
116 1
    if newsletter_list_id:
117
        newsletter_list = newsletter_service.find_list(newsletter_list_id)
118
        newsletter_subscriber_count = newsletter_service.count_subscribers_for_list(
119
            newsletter_list.id
120
        )
121
    else:
122 1
        newsletter_subscriber_count = None
123
124 1
    consent_subject_ids = (
125
        consent_subject_service.get_subject_ids_required_for_brand(brand.id)
126
    )
127 1
    consent_subjects_to_consent_counts = (
128
        consent_subject_service.get_subjects_with_consent_counts(
129
            limit_to_subject_ids=consent_subject_ids
130
        )
131
    )
132 1
    consent_subjects_with_consent_counts = sorted(
133
        consent_subjects_to_consent_counts.items(), key=lambda x: x[0].title
134
    )
135
136 1
    return {
137
        'brand': brand,
138
139
        'current_sites': current_sites,
140
        'active_parties': active_parties,
141
142
        'party_count': party_count,
143
144
        'orga_count': orga_count,
145
146
        'newsletter_list': newsletter_list,
147
        'newsletter_subscriber_count': newsletter_subscriber_count,
148
149
        'consent_subjects_with_consent_counts': consent_subjects_with_consent_counts,
150
    }
151
152
153 1
@blueprint.route('/parties/<party_id>')
154 1
@permission_required(AdminDashboardPermission.view_party)
155 1
@templated
156
def view_party(party_id):
157
    """View dashboard for that party."""
158 1
    party = party_service.find_party(party_id)
159 1
    if party is None:
160
        abort(404)
161
162 1
    days_until_party = (party.starts_at.date() - date.today()).days
163
164 1
    orga_count = orga_team_service.count_memberships_for_party(party.id)
165 1
    orga_team_count = orga_team_service.count_teams_for_party(party.id)
166
167 1
    seating_area_count = seating_area_service.count_areas_for_party(party.id)
168 1
    seat_count = seat_service.count_seats_for_party(party.id)
169
170 1
    ticket_sale_stats = ticket_service.get_ticket_sale_stats(party.id)
171 1
    tickets_checked_in = ticket_service.count_tickets_checked_in_for_party(
172
        party.id
173
    )
174
175 1
    return {
176
        'party': party,
177
        'days_until_party': days_until_party,
178
179
        'orga_count': orga_count,
180
        'orga_team_count': orga_team_count,
181
182
        'seating_area_count': seating_area_count,
183
        'seat_count': seat_count,
184
185
        'ticket_sale_stats': ticket_sale_stats,
186
        'tickets_checked_in': tickets_checked_in,
187
    }
188