~sirn/fanboi2

ref: 98796226802a26b82c888365ad5b9cd331006792 fanboi2/fanboi2/serializers.py -rw-r--r-- 6.8 KiB
98796226Kridsada Thanabulpong Bump copyright year. 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
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
import datetime
import pytz
from fanboi2.helpers.formatters import format_post, format_page


def _datetime_adapter(obj, request):
    """Serialize :type:`datetime.datetime` object into a string.

    :param obj: A :class:`datetime.datetime` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: datetime.datetime
    :type request: pyramid.request.Request
    """
    settings = request.registry.settings
    assert isinstance(settings, dict)
    tz = pytz.timezone(settings['app.timezone'])
    return obj.astimezone(tz).isoformat()


def _sqlalchemy_query_adapter(obj, request):
    """Serialize SQLAlchemy query into a list.

    :param obj: An iterable SQLAlchemy's :class:`sqlalchemy.orm.Query` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: sqlalchemy.orm.Query
    :type request: pyramid.request.Request
    """
    return [item for item in obj]


def _board_serializer(obj, request):
    """Serialize :class:`fanboi2.models.Board` into a :type:`dict`.

    :param obj: A :class:`fanboi2.models.Board` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: fanboi2.models.Board
    :type request: pyramid.request.Request
    :rtype: dict
    """
    result = {
        'type': 'board',
        'id': obj.id,
        'agreements': obj.agreements,
        'description': obj.description,
        'settings': obj.settings,
        'slug': obj.slug,
        'status': obj.status,
        'title': obj.title,
        'path': request.route_path('api_board', board=obj.slug),
    }
    if request.params.get('topics') and not 'board' in request.params:
        result['topics'] = obj.topics.limit(10)
    return result


def _topic_serializer(obj, request):
    """Serialize :class:`fanboi2.models.Topic` into a :type:`dict`.

    :param obj: A :class:`fanboi2.models.Topic` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: fanboi2.models.Topic
    :type request: pyramid.request.Request
    :rtype: dict
    """
    result = {
        'type': 'topic',
        'id': obj.id,
        'board_id': obj.board_id,
        'bumped_at': obj.meta.bumped_at,
        'created_at': obj.created_at,
        'post_count': obj.meta.post_count,
        'posted_at': obj.meta.posted_at,
        'status': obj.status,
        'title': obj.title,
        'path': request.route_path('api_topic', topic=obj.id),
    }
    if request.params.get('board'):
        result['board'] = obj.board
    if request.params.get('posts') and not 'topic' in request.params:
        result['posts'] = obj.recent_posts()
    return result


def _post_serializer(obj, request):
    """Serialize :class:`fanboi2.models.Post` into a :type:`dict`.

    :param obj: A :class:`fanboi2.models.Post` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: fanboi2.models.Post
    :type request: pyramid.request.Request
    :rtype: dict
    """
    result = {
        'type': 'post',
        'id': obj.id,
        'body': obj.body,
        'body_formatted': format_post(None, request, obj),
        'bumped': obj.bumped,
        'created_at': obj.created_at,
        'ident': obj.ident,
        'name': obj.name,
        'number': obj.number,
        'topic_id': obj.topic_id,
        'path': request.route_path(
            'api_topic_posts_scoped',
            topic=obj.topic_id,
            query=obj.number,
        ),
    }
    if request.params.get('topic'):
        result['topic'] = obj.topic
    return result


def _page_serializer(obj, request):
    """Serialize :class:`fanboi2.models.Page` into a :type:`dict`.

    :param obj: A :class:`fanboi2.models.Page` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: fanboi2.models.Page
    :type request: pyramid.request.Request
    :rtype: dict
    """
    return {
        'type': 'page',
        'id': obj.id,
        'body': obj.body,
        'body_formatted': format_page(None, request, obj),
        'formatter': obj.formatter,
        'namespace': obj.namespace,
        'slug': obj.slug,
        'title': obj.title,
        'updated_at': obj.updated_at or obj.created_at,
        'path': request.route_path(
            'api_page',
            page=obj.slug,
        ),
    }


def _result_proxy_serializer(obj, request):
    """Serialize :class:`fanboi2.tasks.ResultProxy` into a :type:`dict`.

    :param obj: A :class:`fanboi2.tasks.ResultProxy` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: fanboi2.tasks.ResultProxy
    :type request: pyramid.request.Request
    :rtype: dict
    """
    result = {
        'type': 'task',
        'id': obj.id,
        'status': obj.status.lower(),
        'path': request.route_path('api_task', task=obj.id),
    }
    if obj.success():
        result['data'] = obj.object
    return result


def _async_result_serializer(obj, request):
    """Serialize :class:`celery.result.AsyncResult` into a :type:`dict`.

    :param obj: A :class:`celery.result.AsyncResult` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: celery.result.AsyncResult
    :type request: pyramid.request.Request
    :rtype: dict
    """
    return {
        'type': 'task',
        'id': obj.id,
        'status': 'queued',
        'path': request.route_path('api_task', task=obj.id),
    }


def _base_error_serializer(obj, request):
    """Serialize :class:`fanboi2.errors.BaseError` and its subclasses
    into :type:`dict` using message and the name defined in the class.

    :param obj: A :class:`fanboi2.errors.BaseError` object.
    :param request: A :class:`pyramid.request.Request` object.

    :type obj: fanboi2.errors.BaseError
    :type request: pyramid.request.Request
    :rtype: dict
    """
    return {
        'type': 'error',
        'status': obj.name,
        'message': obj.message(request)
    }


def initialize_renderer():
    from celery.result import AsyncResult
    from pyramid.renderers import JSON
    from sqlalchemy.orm import Query
    from fanboi2.models import Board, Topic, Post, Page
    from fanboi2.errors import BaseError
    from fanboi2.tasks import ResultProxy
    json_renderer = JSON()
    json_renderer.add_adapter(datetime.datetime, _datetime_adapter)
    json_renderer.add_adapter(Query, _sqlalchemy_query_adapter)
    json_renderer.add_adapter(Board, _board_serializer)
    json_renderer.add_adapter(Topic, _topic_serializer)
    json_renderer.add_adapter(Post, _post_serializer)
    json_renderer.add_adapter(Page, _page_serializer)
    json_renderer.add_adapter(ResultProxy, _result_proxy_serializer)
    json_renderer.add_adapter(AsyncResult, _async_result_serializer)
    json_renderer.add_adapter(BaseError, _base_error_serializer)
    return json_renderer


def includeme(config):  # pragma: no cover
    json_renderer = initialize_renderer()
    config.add_renderer('json', json_renderer)