예제 #1
0
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)
예제 #2
0
def serialize_category(category, options=None):
    return util.serialize_entity(
        category,
        {
            'name': lambda: category.name,
            'version': lambda: category.version,
            'color': lambda: category.color,
            'usages': lambda: category.tag_count,
            'default': lambda: category.default,
        },
        options)
예제 #3
0
def serialize_comment(comment, auth_user, options=None):
    return util.serialize_entity(
        comment,
        {
            'id': lambda: comment.comment_id,
            'user':
                lambda: users.serialize_micro_user(comment.user, auth_user),
            'postId': lambda: comment.post.post_id,
            'version': lambda: comment.version,
            'text': lambda: comment.text,
            'creationTime': lambda: comment.creation_time,
            'lastEditTime': lambda: comment.last_edit_time,
            'score': lambda: comment.score,
            'ownScore': lambda: scores.get_score(comment, auth_user),
        },
        options)
예제 #4
0
def serialize_tag(tag, options=None):
    return util.serialize_entity(
        tag,
        {
            'names': lambda: [tag_name.name for tag_name in tag.names],
            'category': lambda: tag.category.name,
            'version': lambda: tag.version,
            'description': lambda: tag.description,
            'creationTime': lambda: tag.creation_time,
            'lastEditTime': lambda: tag.last_edit_time,
            'usages': lambda: tag.post_count,
            'suggestions': lambda: [
                relation.names[0].name
                for relation in sort_tags(tag.suggestions)],
            'implications': lambda: [
                relation.names[0].name
                for relation in sort_tags(tag.implications)],
        },
        options)
예제 #5
0
def serialize_user(user, auth_user, options=None, force_show_email=False):
    return util.serialize_entity(
        user,
        {
            'name': lambda: user.name,
            'creationTime': lambda: user.creation_time,
            'lastLoginTime': lambda: user.last_login_time,
            'version': lambda: user.version,
            'rank': lambda: user.rank,
            'avatarStyle': lambda: user.avatar_style,
            'avatarUrl': lambda: get_avatar_url(user),
            'commentCount': lambda: user.comment_count,
            'uploadedPostCount': lambda: user.post_count,
            'favoritePostCount': lambda: user.favorite_post_count,
            'likedPostCount':
                lambda: get_liked_post_count(user, auth_user),
            'dislikedPostCount':
                lambda: get_disliked_post_count(user, auth_user),
            'email':
                lambda: get_email(user, auth_user, force_show_email),
        },
        options)