~homeworkprod/byceps

ref: 533662eefb314d54b168e5a46bc07c88b5f88be4 byceps/byceps/blueprints/admin/webhook/views.py -rw-r--r-- 5.3 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
byceps.blueprints.admin.webhook.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from flask import abort, request
from flask_babel import gettext

from ....announce.events import EVENT_TYPES_TO_NAMES
from ....announce.helpers import call_webhook
from ....services.webhooks import service as webhook_service
from ....services.webhooks.transfer.models import OutgoingWebhook, WebhookID
from ....util.authorization import register_permission_enum
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import permission_required, redirect_to, respond_no_content

from .authorization import WebhookPermission
from .forms import (
    assemble_create_form,
    assemble_update_form,
    _create_event_field_name,
)


blueprint = create_blueprint('webhook_admin', __name__)


register_permission_enum(WebhookPermission)


WEBHOOK_EVENT_NAMES = frozenset(EVENT_TYPES_TO_NAMES.values())


@blueprint.get('/')
@permission_required(WebhookPermission.view)
@templated
def index():
    """Show webhooks."""
    webhooks = webhook_service.get_all_webhooks()

    webhooks.sort(key=lambda w: (w.description is None, w.description or ''))

    return {
        'webhooks': webhooks,
    }


@blueprint.get('/create')
@permission_required(WebhookPermission.administrate)
@templated
def create_form(erroneous_form=None):
    """Show form to create a webhook."""
    event_names = WEBHOOK_EVENT_NAMES
    CreateForm = assemble_create_form(event_names)

    form = erroneous_form if erroneous_form else CreateForm()

    return {
        'form': form,
        'event_names': event_names,
    }


@blueprint.post('/')
@permission_required(WebhookPermission.administrate)
def create():
    """Create a webhook."""
    event_names = WEBHOOK_EVENT_NAMES
    CreateForm = assemble_create_form(event_names)

    form = CreateForm(request.form)

    if not form.validate():
        return create_form(form)

    event_selectors = {}
    for event_name in event_names:
        if form.get_field_for_event_name(event_name).data:
            event_selectors[event_name] = None

    format = form.format.data.strip()
    url = form.url.data.strip()
    description = form.description.data.strip()
    enabled = False

    webhook = webhook_service.create_outgoing_webhook(
        event_selectors, format, url, enabled, description=description
    )

    flash_success(gettext('Webhook has been created.'))

    return redirect_to('.index')


@blueprint.get('/webhooks/<uuid:webhook_id>/update')
@permission_required(WebhookPermission.administrate)
@templated
def update_form(webhook_id, erroneous_form=None):
    """Show form to update a webhook."""
    webhook = _get_webhook_or_404(webhook_id)

    event_names = WEBHOOK_EVENT_NAMES
    UpdateForm = assemble_update_form(event_names)

    # Pre-fill event type checkboxes.
    event_type_fields = {}
    for event_type_name in webhook.event_selectors:
        field_name = _create_event_field_name(event_type_name)
        event_type_fields[field_name] = True

    form = (
        erroneous_form
        if erroneous_form
        else UpdateForm(obj=webhook, **event_type_fields)
    )

    return {
        'webhook': webhook,
        'form': form,
        'event_names': event_names,
    }


@blueprint.post('/webhooks/<uuid:webhook_id>')
@permission_required(WebhookPermission.administrate)
def update(webhook_id):
    """Update the webhook."""
    webhook = _get_webhook_or_404(webhook_id)

    event_names = WEBHOOK_EVENT_NAMES
    UpdateForm = assemble_update_form(event_names)

    form = UpdateForm(request.form)

    if not form.validate():
        return update_form(webhook.id, form)

    event_selectors = {}
    for event_name in event_names:
        if form.get_field_for_event_name(event_name).data:
            event_selectors[event_name] = None

    format = form.format.data.strip()
    url = form.url.data.strip()
    text_prefix = form.text_prefix.data.lstrip()  # Allow trailing whitespace.
    description = form.description.data.strip()
    enabled = form.enabled.data

    webhook = webhook_service.update_outgoing_webhook(
        webhook.id,
        event_selectors,
        format,
        url,
        enabled,
        text_prefix=text_prefix,
        description=description,
    )

    flash_success(gettext('Webhook has been updated.'))

    return redirect_to('.index')


@blueprint.post('/webhooks/<uuid:webhook_id>/test')
@permission_required(WebhookPermission.administrate)
@respond_no_content
def test(webhook_id):
    """Call the webhook (synchronously)."""
    webhook = _get_webhook_or_404(webhook_id)

    text = 'Test, test … is this thing on?!'
    try:
        call_webhook(webhook, text)
        flash_success(
            gettext('Webhook call has been successful.'), icon='success'
        )
    except Exception as e:
        flash_error(
            gettext('Webhook call failed:')
            + f'<br><pre style="white-space: pre-wrap;">{e}</pre>',
            icon='warning',
            text_is_safe=True,
        )


# helpers


def _get_webhook_or_404(webhook_id: WebhookID) -> OutgoingWebhook:
    webhook = webhook_service.find_webhook(webhook_id)

    if webhook is None:
        abort(404)

    return webhook