~sirn/fanboi2

ref: 834edf0edc5dd633c0ecea16231b6ed2d728476d fanboi2/fanboi2/tasks/post.py -rw-r--r-- 1.5 KiB
834edf0eKridsada Thanabulpong Massive cleanup in preparation for 0.30 (#25) 3 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
import pyramid.scripting

from ..errors import StatusRejectedError
from ..interfaces import IFilterService, IPostCreateService
from ._base import celery, ModelTask


@celery.task(base=ModelTask, bind=True)
def add_post(
        self,
        topic_id,
        body,
        bumped,
        ip_address,
        payload={},
        _request=None,
        _registry=None):
    """Insert a post to a topic.

    :param self: A :class:`celery.Task` object.
    :param topic_id: The ID of a topic to add a post to.
    :param body: Content of the post as submitted by the user.
    :param bumped: A :type:`bool` whether to bump the topic.
    :param ip_address: An IP address of the poster.
    :param payload: A request payload containing request metadata.
    """
    with pyramid.scripting.prepare(
            request=_request,
            registry=_registry) as env:
        request = env['request']

        filter_svc = request.find_service(IFilterService)
        filter_result = filter_svc.evaluate(payload={
            'body': body,
            'ip_address': ip_address,
            **payload})
        if filter_result.rejected_by:
            return 'failure', "%s_rejected" % (filter_result.rejected_by,)

        post_create_svc = request.find_service(IPostCreateService)
        try:
            post = post_create_svc.create(
                topic_id,
                body,
                bumped,
                ip_address)
        except StatusRejectedError as e:
            return 'failure', e.name, e.status
        return 'post', post.id