~sirn/fanboi2

ref: baf63a21556905a6c27516a8d5641d31120d1fe3 fanboi2/fanboi2/views/api.py -rw-r--r-- 10.9 KiB
baf63a21Kridsada Thanabulpong Add scope column to banwords. 1 year, 9 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from sqlalchemy.orm.exc import NoResultFound
from webob.multidict import MultiDict

from ..errors import (
    ParamsInvalidError,
    RateLimitedError,
    BanRejectedError,
    BanwordRejectedError,
    BaseError,
)
from ..forms import TopicForm, PostForm
from ..interfaces import (
    IBanQueryService,
    IBanwordQueryService,
    IBoardQueryService,
    IPageQueryService,
    IPostCreateService,
    IPostQueryService,
    IRateLimiterService,
    ITaskQueryService,
    ITopicCreateService,
    ITopicQueryService,
)


def _get_params(request):
    """Return a :class:`MultiDict` of the params given in request
    regardless of whether the request is sent as JSON or as formdata.

    :param request: A :class:`pyramid.request.Request` object.
    """
    params = request.POST
    if request.content_type.startswith("application/json"):
        try:
            params = MultiDict(request.json_body)
        except ValueError:  # pragma: no cover
            pass
    return params


def root(request):
    """Display an API documentation view."""
    return {}


def boards_get(request):
    """Retrieve a list of all boards.

    :param request: A :class:`pyramid.request.Request` object.
    """
    board_query_svc = request.find_service(IBoardQueryService)
    return board_query_svc.list_active()


def board_get(request):
    """Retrieve a full info of a single board.

    :param request: A :class:`pyramid.request.Request` object.
    """
    board_query_svc = request.find_service(IBoardQueryService)
    board_slug = request.matchdict["board"]
    return board_query_svc.board_from_slug(board_slug)


def board_topics_get(request):
    """Retrieve all available topics within a single board.

    :param request: A :class:`pyramid.request.Request` object.
    """
    board_query_svc = request.find_service(IBoardQueryService)
    topic_query_svc = request.find_service(ITopicQueryService)
    board_slug = request.matchdict["board"]
    board_query_svc.board_from_slug(board_slug)  # ensure exists
    topics = topic_query_svc.list_from_board_slug(board_slug)
    return topics


def board_topics_post(request):
    """Create a new topic.

    :param request: A :class:`pyramid.request.Request` object.
    """
    board_query_svc = request.find_service(IBoardQueryService)
    board_slug = request.matchdict["board"]
    board = board_query_svc.board_from_slug(board_slug)

    params = _get_params(request)
    form = TopicForm(params, request=request)

    if not form.validate():
        raise ParamsInvalidError(form.errors)

    ban_query_svc = request.find_service(IBanQueryService)
    if ban_query_svc.is_banned(request.client_addr, scopes={"board": board.slug}):
        raise BanRejectedError()

    banword_query_svc = request.find_service(IBanwordQueryService)
    if banword_query_svc.is_banned(form.body.data):
        raise BanwordRejectedError()

    rate_limiter_svc = request.find_service(IRateLimiterService)
    if rate_limiter_svc:
        payload = {"ip_address": request.client_addr, "board": board.slug}
        if rate_limiter_svc.is_limited(**payload):
            time_left = rate_limiter_svc.time_left(**payload)
            raise RateLimitedError(time_left)

        rate_limiter_svc.limit_for(board.settings["post_delay"], **payload)

    topic_create_svc = request.find_service(ITopicCreateService)
    return topic_create_svc.enqueue(
        board.slug,
        form.title.data,
        form.body.data,
        request.client_addr,
        payload={
            "application_url": request.application_url,
            "referrer": request.referrer,
            "url": request.url,
            "user_agent": request.user_agent,
        },
    )


def task_get(request):
    """Retrieve a task processing status for the given task id.

    :param request: A :class:`pyramid.request.Request` object.
    """
    task_query_svc = request.find_service(ITaskQueryService)
    task_uid = request.matchdict["task"]
    result_proxy = task_query_svc.result_from_uid(task_uid)

    if result_proxy.success():
        obj = result_proxy.deserialize(request)
        if isinstance(obj, BaseError):
            raise obj

    return result_proxy


def topic_get(request):
    """Retrieve a topic info for an individual topic.

    :param request: A :class:`pyramid.request.Request` object.
    """
    topic_query_svc = request.find_service(ITopicQueryService)
    topic_id = request.matchdict["topic"]
    return topic_query_svc.topic_from_id(topic_id)


def topic_posts_get(request):
    """Retrieve all posts in a single topic or by or by search criteria.

    :param request: A :class:`pyramid.request.Request` object.
    """
    post_query_svc = request.find_service(IPostQueryService)
    topic_id = request.matchdict["topic"]
    query = None

    if "query" in request.matchdict:
        query = request.matchdict["query"]

    return post_query_svc.list_from_topic_id(topic_id, query)


def topic_posts_post(request):
    """Create a new post within topic.

    :param request: A :class:`pyramid.request.Request` object.
    """
    topic_query_svc = request.find_service(ITopicQueryService)
    topic_id = request.matchdict["topic"]

    topic = topic_query_svc.topic_from_id(topic_id)
    board = topic.board

    params = _get_params(request)
    form = PostForm(params, request=request)

    if not form.validate():
        raise ParamsInvalidError(form.errors)

    ban_query_svc = request.find_service(IBanQueryService)
    if ban_query_svc.is_banned(
        request.client_addr, scopes={"board": board.slug, "topic": topic.title}
    ):
        raise BanRejectedError()

    banword_query_svc = request.find_service(IBanwordQueryService)
    if banword_query_svc.is_banned(form.body.data):
        raise BanwordRejectedError()

    rate_limiter_svc = request.find_service(IRateLimiterService)
    if rate_limiter_svc:
        payload = {"ip_address": request.client_addr, "board": board.slug}
        if rate_limiter_svc.is_limited(**payload):
            time_left = rate_limiter_svc.time_left(**payload)
            raise RateLimitedError(time_left)

        rate_limiter_svc.limit_for(board.settings["post_delay"], **payload)

    post_create_svc = request.find_service(IPostCreateService)
    return post_create_svc.enqueue(
        topic.id,
        form.body.data,
        form.bumped.data,
        request.client_addr,
        payload={
            "application_url": request.application_url,
            "referrer": request.referrer,
            "url": request.url,
            "user_agent": request.user_agent,
        },
    )


def pages_get(request):
    """Retrieve a list of all pages.

    :param request: A :class:`pyramid.request.Request` object.
    """
    page_query_svc = request.find_service(IPageQueryService)
    return page_query_svc.list_public()


def page_get(request):
    """Retrieve a page.

    :param request: A :class:`pyramid.request.Request` object.
    :param page: Page name to retrieve.
    :param namespace: Page namespace to retrieve from.
    """
    page_query_svc = request.find_service(IPageQueryService)
    page_slug = request.matchdict["page"]
    return page_query_svc.public_page_from_slug(page_slug)


def error_not_found(exc, request):
    """Handle any exception that should cause the app to treat it as
    NotFound resources, such as :class:`pyramid.httpexceptions.HTTPNotFound`
    or :class:`sqlalchemy.orm.exc.NoResultFound`.

    :param exc: An :class:`Exception`.
    :param request: A :class:`pyramid.request.Request` object.
    """
    request.response.status = "404 Not Found"
    return {
        "type": "error",
        "status": "not_found",
        "message": "The resource %s %s could not be found."
        % (request.method, request.path),
    }


def error_base_handler(exc, request):
    """Handle any exception that should be rendered with the serializer.
    Normally this is any subclass of the :class:`fanboi2.errors.BaseError`.

    :param exc: A :class:`fanboi2.errors.BaseError`.
    :param request: A :class:`pyramid.request.Request` object.
    """
    request.response.status = exc.http_status
    return exc


def _api_routes_only(context, request):
    """A predicate for :meth:`pyramid.config.add_view` that returns true
    if the requested route is an API route.
    """
    return request.path.startswith("/api/")


def includeme(config):  # pragma: no cover
    config.add_route("api_root", "/")
    config.add_view(
        root, request_method="GET", route_name="api_root", renderer="api/show.mako"
    )

    #
    # Pages API
    #

    config.add_route("api_pages", "/1.0/pages/")
    config.add_route("api_page", "/1.0/pages/{page:.*}/")

    config.add_view(
        pages_get, request_method="GET", route_name="api_pages", renderer="json"
    )

    config.add_view(
        page_get, request_method="GET", route_name="api_page", renderer="json"
    )

    #
    # Boards API
    #

    config.add_route("api_boards", "/1.0/boards/")
    config.add_route("api_board", "/1.0/boards/{board}/")
    config.add_route("api_board_topics", "/1.0/boards/{board}/topics/")

    config.add_view(
        boards_get, request_method="GET", route_name="api_boards", renderer="json"
    )

    config.add_view(
        board_get, request_method="GET", route_name="api_board", renderer="json"
    )

    config.add_view(
        board_topics_get,
        request_method="GET",
        route_name="api_board_topics",
        renderer="json",
    )

    config.add_view(
        board_topics_post,
        request_method="POST",
        route_name="api_board_topics",
        renderer="json",
    )

    #
    # Task API
    #

    config.add_route("api_task", "/1.0/tasks/{task}/")

    config.add_view(
        task_get, request_method="GET", route_name="api_task", renderer="json"
    )

    #
    # Topics API
    #

    config.add_route("api_topic", "/1.0/topics/{topic:\d+}/")
    config.add_route("api_topic_posts", "/1.0/topics/{topic:\d+}/posts/")
    config.add_route("api_topic_posts_scoped", "/1.0/topics/{topic:\d+}/posts/{query}/")

    config.add_view(
        topic_get, request_method="GET", route_name="api_topic", renderer="json"
    )

    config.add_view(
        topic_posts_get,
        request_method="GET",
        route_name="api_topic_posts",
        renderer="json",
    )

    config.add_view(
        topic_posts_post,
        request_method="POST",
        route_name="api_topic_posts",
        renderer="json",
    )

    config.add_view(
        topic_posts_get,
        request_method="GET",
        route_name="api_topic_posts_scoped",
        renderer="json",
    )

    #
    # Error handling
    #

    config.add_view(
        error_base_handler,
        context=BaseError,
        renderer="json",
        custom_predicates=[_api_routes_only],
    )

    config.add_view(
        error_not_found,
        context=NoResultFound,
        renderer="json",
        custom_predicates=[_api_routes_only],
    )

    config.add_notfound_view(
        error_not_found,
        append_slash=True,
        renderer="json",
        custom_predicates=[_api_routes_only],
    )

    config.scan()