~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/site/board/views_posting.py -rw-r--r-- 8.4 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
byceps.blueprints.site.board.views_posting
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

import dataclasses

from flask import g, redirect, request
from flask_babel import gettext

from ....services.board import (
    last_view_service as board_last_view_service,
    posting_command_service as board_posting_command_service,
    posting_query_service as board_posting_query_service,
)
from ....services.text_markup.service import get_smileys
from ....services.user import service as user_service
from ....signals import board as board_signals
from ....util.authorization import has_current_user_permission
from ....util.framework.flash import flash_error, flash_success
from ....util.framework.templating import templated
from ....util.views import permission_required, respond_no_content_with_location

from .authorization import BoardPermission, BoardPostingPermission
from .blueprint import blueprint
from .forms import PostingCreateForm, PostingUpdateForm
from . import _helpers as h, service


@blueprint.route('/postings/<uuid:posting_id>')
def posting_view(posting_id):
    """Show the page of the posting's topic that contains the posting,
    as seen by the current user.
    """
    posting = h.get_posting_or_404(posting_id)

    page = service.calculate_posting_page_number(posting)

    return redirect(
        h.build_url_for_posting_in_topic_view(posting, page, _external=True)
    )


@blueprint.route('/topics/<uuid:topic_id>/create')
@permission_required(BoardPostingPermission.create)
@templated
def posting_create_form(topic_id, erroneous_form=None):
    """Show a form to create a posting to the topic."""
    topic = h.get_topic_or_404(topic_id)

    form = erroneous_form if erroneous_form else PostingCreateForm()

    quoted_posting_bbcode = quote_posting_as_bbcode()
    if quoted_posting_bbcode:
        form.body.data = quoted_posting_bbcode

    return {
        'topic': topic,
        'form': form,
        'smileys': get_smileys(),
    }


def quote_posting_as_bbcode():
    posting_id = request.args.get('quote', type=str)
    if not posting_id:
        return

    posting = board_posting_query_service.find_posting_by_id(posting_id)
    if posting is None:
        flash_error(gettext('The posting to quote has not been found.'))
        return

    creator = user_service.get_user(posting.creator_id)

    return f'[quote author="{creator.screen_name}"]{posting.body}[/quote]'


@blueprint.route('/topics/<uuid:topic_id>/create', methods=['POST'])
@permission_required(BoardPostingPermission.create)
def posting_create(topic_id):
    """Create a posting to the topic."""
    topic = h.get_topic_or_404(topic_id)

    form = PostingCreateForm(request.form)
    if not form.validate():
        return posting_create_form(topic_id, form)

    creator = g.user
    body = form.body.data.strip()

    if topic.locked:
        flash_error(
            gettext('This topic is locked. It cannot be replied to.'),
            icon='lock',
        )
        return redirect(h.build_url_for_topic(topic.id))

    if topic.posting_limited_to_moderators and not has_current_user_permission(
        BoardPermission.announce
    ):
        flash_error(
            gettext('Only moderators are allowed to reply in this topic.'),
            icon='announce',
        )
        return redirect(h.build_url_for_topic(topic.id))

    posting, event = board_posting_command_service.create_posting(
        topic.id, creator.id, body
    )

    if g.user.authenticated:
        board_last_view_service.mark_category_as_just_viewed(
            topic.category.id, g.user.id
        )

    flash_success(gettext('Your reply has been added.'))

    event = dataclasses.replace(
        event, url=h.build_external_url_for_posting(posting.id)
    )
    board_signals.posting_created.send(None, event=event)

    postings_per_page = service.get_postings_per_page_value()
    page_count = topic.count_pages(postings_per_page)

    return redirect(h.build_url_for_posting_in_topic_view(posting, page_count))


@blueprint.route('/postings/<uuid:posting_id>/update')
@permission_required(BoardPostingPermission.update)
@templated
def posting_update_form(posting_id, erroneous_form=None):
    """Show form to update a posting."""
    posting = h.get_posting_or_404(posting_id)

    page = service.calculate_posting_page_number(posting)
    url = h.build_url_for_posting_in_topic_view(posting, page)

    user_may_update = service.may_posting_be_updated_by_current_user(posting)

    if posting.topic.locked and not user_may_update:
        flash_error(
            gettext(
                'The posting must not be edited because '
                'the topic it belongs to is locked.'
            )
        )
        return redirect(url)

    if posting.topic.hidden or posting.hidden:
        flash_error(gettext('The posting must not be edited.'))
        return redirect(url)

    if not user_may_update:
        flash_error(gettext('You are not allowed to edit this posting.'))
        return redirect(url)

    form = erroneous_form if erroneous_form else PostingUpdateForm(obj=posting)

    return {
        'form': form,
        'posting': posting,
        'smileys': get_smileys(),
    }


@blueprint.route('/postings/<uuid:posting_id>', methods=['POST'])
@permission_required(BoardPostingPermission.update)
def posting_update(posting_id):
    """Update a posting."""
    posting = h.get_posting_or_404(posting_id)

    page = service.calculate_posting_page_number(posting)
    url = h.build_url_for_posting_in_topic_view(posting, page)

    user_may_update = service.may_posting_be_updated_by_current_user(posting)

    if posting.topic.locked and not user_may_update:
        flash_error(
            gettext(
                'The posting must not be edited because '
                'the topic it belongs to is locked.'
            )
        )
        return redirect(url)

    if posting.topic.hidden or posting.hidden:
        flash_error(gettext('The posting must not be edited.'))
        return redirect(url)

    if not user_may_update:
        flash_error(gettext('You are not allowed to edit this posting.'))
        return redirect(url)

    form = PostingUpdateForm(request.form)
    if not form.validate():
        return posting_update_form(posting_id, form)

    event = board_posting_command_service.update_posting(
        posting.id, g.user.id, form.body.data
    )

    flash_success(gettext('The posting has been updated.'))

    event = dataclasses.replace(
        event, url=h.build_external_url_for_posting(posting.id)
    )
    board_signals.posting_updated.send(None, event=event)

    return redirect(url)


@blueprint.route('/postings/<uuid:posting_id>/moderate')
@permission_required(BoardPermission.hide)
@templated
def posting_moderate_form(posting_id):
    """Show a form to moderate the posting."""
    posting = h.get_posting_or_404(posting_id)

    posting.creator = user_service.get_user(posting.creator_id)

    return {
        'posting': posting,
    }


@blueprint.route('/postings/<uuid:posting_id>/flags/hidden', methods=['POST'])
@permission_required(BoardPermission.hide)
@respond_no_content_with_location
def posting_hide(posting_id):
    """Hide a posting."""
    posting = h.get_posting_or_404(posting_id)
    moderator_id = g.user.id

    event = board_posting_command_service.hide_posting(posting.id, moderator_id)

    page = service.calculate_posting_page_number(posting)

    flash_success(gettext('The posting has been hidden.'), icon='hidden')

    event = dataclasses.replace(
        event, url=h.build_external_url_for_posting(posting.id)
    )
    board_signals.posting_hidden.send(None, event=event)

    return h.build_url_for_posting_in_topic_view(posting, page)


@blueprint.route('/postings/<uuid:posting_id>/flags/hidden', methods=['DELETE'])
@permission_required(BoardPermission.hide)
@respond_no_content_with_location
def posting_unhide(posting_id):
    """Un-hide a posting."""
    posting = h.get_posting_or_404(posting_id)
    moderator_id = g.user.id

    event = board_posting_command_service.unhide_posting(
        posting.id, moderator_id
    )

    page = service.calculate_posting_page_number(posting)

    flash_success(
        gettext('The posting has been made visible again.'), icon='view'
    )

    event = dataclasses.replace(
        event, url=h.build_external_url_for_posting(posting.id)
    )
    board_signals.posting_unhidden.send(None, event=event)

    return h.build_url_for_posting_in_topic_view(posting, page)