~homeworkprod/byceps

ref: 43bf3630475839afde6e691f0ac561dcb01aaba6 byceps/byceps/config.py -rw-r--r-- 4.2 KiB
43bf3630 — Jochen Kupperschmidt Upgrade pytest on Travis CI to get v3.3 rather than v3.0 provided by default 4 years 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
byceps.config
~~~~~~~~~~~~~

:Copyright: 2006-2017 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from enum import Enum

from flask import current_app


STATIC_URL_PREFIX_GLOBAL = '/global'
STATIC_URL_PREFIX_BRAND = '/brand'
STATIC_URL_PREFIX_PARTY = '/party'

EXTENSION_KEY = 'byceps_config'
KEY_SITE_MODE = 'site_mode'
KEY_PARTY_ID = 'party_id'
KEY_SEAT_MANAGEMENT_ENABLED = 'seat_management_enabled'
KEY_TICKET_MANAGEMENT_ENABLED = 'ticket_management_enabled'
KEY_USER_REGISTRATION_ENABLED = 'user_registration_enabled'


SiteMode = Enum('SiteMode', ['public', 'admin'])
SiteMode.is_admin = lambda self: self == SiteMode.admin
SiteMode.is_public = lambda self: self == SiteMode.public


def init_app(app):
    app.extensions[EXTENSION_KEY] = {}

    site_mode = determine_site_mode(app)
    update_extension_value(app, KEY_SITE_MODE, site_mode)

    if site_mode.is_public():
        party_id = determine_party_id(app)
        update_extension_value(app, KEY_PARTY_ID, party_id)

    user_registration_enabled = determine_user_registration_enabled(app,
                                                                    site_mode)
    update_extension_value(app, KEY_USER_REGISTRATION_ENABLED,
                           user_registration_enabled)

    ticket_management_enabled = determine_ticket_management_enabled(app,
                                                                    site_mode)
    update_extension_value(app, KEY_TICKET_MANAGEMENT_ENABLED,
                           ticket_management_enabled)

    seat_management_enabled = determine_seat_management_enabled(app, site_mode)
    update_extension_value(app, KEY_SEAT_MANAGEMENT_ENABLED,
                           seat_management_enabled)


def update_extension_value(app, key, value):
    """Set/replace the value value for the key in this application's
    own extension namespace.
    """
    app.extensions[EXTENSION_KEY][key] = value


# -------------------------------------------------------------------- #
# site mode


def determine_site_mode(app):
    value = app.config.get('MODE')
    if value is None:
        raise Exception('No site mode configured.')

    try:
        return SiteMode[value]
    except KeyError:
        raise Exception('Invalid site mode "{}" configured.'.format(value))


def get_site_mode(app=None):
    """Return the mode the site should run in."""
    return _get_config_dict(app)[KEY_SITE_MODE]


# -------------------------------------------------------------------- #
# party ID


def determine_party_id(app):
    party_id = app.config.get('PARTY')
    if party_id is None:
        raise Exception('No party configured.')

    return party_id


def get_current_party_id(app=None):
    """Return the id of the current party."""
    return _get_config_dict(app)[KEY_PARTY_ID]


# -------------------------------------------------------------------- #
# user registration


def determine_user_registration_enabled(app, site_mode):
    if site_mode.is_admin():
        return False

    return app.config['USER_REGISTRATION_ENABLED']


def get_user_registration_enabled(app=None):
    """Return `True` if guests may register user accounts."""
    return _get_config_dict(app)[KEY_USER_REGISTRATION_ENABLED]


# -------------------------------------------------------------------- #
# ticket management


def determine_ticket_management_enabled(app, site_mode):
    if site_mode.is_admin():
        return False

    return app.config['TICKET_MANAGEMENT_ENABLED']


def get_ticket_management_enabled(app=None):
    """Return `True` if users may manage tickets."""
    return _get_config_dict(app)[KEY_TICKET_MANAGEMENT_ENABLED]


# -------------------------------------------------------------------- #
# seat management


def determine_seat_management_enabled(app, site_mode):
    if site_mode.is_admin():
        return False

    return app.config['SEAT_MANAGEMENT_ENABLED']


def get_seat_management_enabled(app=None):
    """Return `True` if users may manage seats."""
    return _get_config_dict(app)[KEY_SEAT_MANAGEMENT_ENABLED]


# -------------------------------------------------------------------- #


def _get_config_dict(app=None):
    if app is None:
        app = current_app

    return app.extensions[EXTENSION_KEY]