def serialize_comments(self) -> Any: return [ comments.serialize_comment(comment, self.auth_user) for comment in sorted( self.post.comments, key=lambda comment: comment.creation_time ) ]
def test_serialize_user(user_factory, comment_factory): with patch("szurubooru.func.users.get_avatar_url"): users.get_avatar_url.return_value = "https://example.com/avatar.png" comment = comment_factory(user=user_factory(name="dummy")) comment.comment_id = 77 comment.creation_time = datetime(1997, 1, 1) comment.last_edit_time = datetime(1998, 1, 1) comment.text = "text" db.session.add(comment) db.session.flush() auth_user = user_factory() assert comments.serialize_comment(comment, auth_user) == { "id": comment.comment_id, "postId": comment.post.post_id, "creationTime": datetime(1997, 1, 1, 0, 0), "lastEditTime": datetime(1998, 1, 1, 0, 0), "score": 0, "ownScore": 0, "text": "text", "user": { "name": "dummy", "avatarUrl": "https://example.com/avatar.png", }, "version": 1, }
def test_serialize_user(user_factory, comment_factory): with patch('szurubooru.func.users.get_avatar_url'): users.get_avatar_url.return_value = 'https://example.com/avatar.png' comment = comment_factory(user=user_factory(name='dummy')) comment.comment_id = 77 comment.creation_time = datetime(1997, 1, 1) comment.last_edit_time = datetime(1998, 1, 1) comment.text = 'text' db.session.add(comment) db.session.flush() auth_user = user_factory() assert comments.serialize_comment(comment, auth_user) == { 'id': comment.comment_id, 'postId': comment.post.post_id, 'creationTime': datetime(1997, 1, 1, 0, 0), 'lastEditTime': datetime(1998, 1, 1, 0, 0), 'score': 0, 'ownScore': 0, 'text': 'text', 'user': { 'name': 'dummy', 'avatarUrl': 'https://example.com/avatar.png', }, 'version': 1, }
def serialize_post(post, auth_user, options=None): return util.serialize_entity( post, { 'id': lambda: post.post_id, 'version': lambda: post.version, 'creationTime': lambda: post.creation_time, 'lastEditTime': lambda: post.last_edit_time, 'safety': lambda: SAFETY_MAP[post.safety], 'source': lambda: post.source, 'type': lambda: TYPE_MAP[post.type], 'mimeType': lambda: post.mime_type, 'checksum': lambda: post.checksum, 'fileSize': lambda: post.file_size, 'canvasWidth': lambda: post.canvas_width, 'canvasHeight': lambda: post.canvas_height, 'contentUrl': lambda: get_post_content_url(post), 'thumbnailUrl': lambda: get_post_thumbnail_url(post), 'flags': lambda: post.flags, 'tags': lambda: [ tag.names[0].name for tag in tags.sort_tags(post.tags)], 'relations': lambda: sorted( { post['id']: post for post in [ serialize_micro_post(rel, auth_user) for rel in post.relations] }.values(), key=lambda post: post['id']), 'user': lambda: users.serialize_micro_user(post.user, auth_user), 'score': lambda: post.score, 'ownScore': lambda: scores.get_score(post, auth_user), 'ownFavorite': lambda: len([ user for user in post.favorited_by if user.user_id == auth_user.user_id] ) > 0, 'tagCount': lambda: post.tag_count, 'favoriteCount': lambda: post.favorite_count, 'commentCount': lambda: post.comment_count, 'noteCount': lambda: post.note_count, 'relationCount': lambda: post.relation_count, 'featureCount': lambda: post.feature_count, 'lastFeatureTime': lambda: post.last_feature_time, 'favoritedBy': lambda: [ users.serialize_micro_user(rel.user, auth_user) for rel in post.favorited_by ], 'hasCustomThumbnail': lambda: files.has(get_post_thumbnail_backup_path(post)), 'notes': lambda: sorted( [serialize_note(note) for note in post.notes], key=lambda x: x['polygon']), 'comments': lambda: [ comments.serialize_comment(comment, auth_user) for comment in sorted( post.comments, key=lambda comment: comment.creation_time)], }, options)
def serialize_post_with_details(post, authenticated_user): comment_list = [] if post: for comment in post.comments: comment_list.append( comments.serialize_comment(comment, authenticated_user)) return { 'post': serialize_post(post, authenticated_user), 'snapshots': snapshots.get_serialized_history(post), 'comments': comment_list, }
def _serialize(ctx: rest.Context, comment: model.Comment) -> rest.Response: return comments.serialize_comment( comment, ctx.user, options=serialization.get_serialization_options(ctx))
def get(self, ctx): auth.verify_privilege(ctx.user, 'comments:list') return self._search_executor.execute_and_serialize( ctx, lambda comment: comments.serialize_comment(comment, ctx.user))
def _serialize( ctx: rest.Context, comment: model.Comment) -> rest.Response: return comments.serialize_comment( comment, ctx.user, options=serialization.get_serialization_options(ctx))
def serialize_comments(self) -> Any: return [ comments.serialize_comment(comment, self.auth_user) for comment in sorted( self.post.comments, key=lambda comment: comment.creation_time)]
def _serialize(ctx, comment, **kwargs): return comments.serialize_comment( comment, ctx.user, options=util.get_serialization_options(ctx), **kwargs)