gallery.accords-library.com/server/szurubooru/func/comments.py

105 lines
3.0 KiB
Python
Raw Normal View History

from datetime import datetime
from typing import Any, Optional, List, Dict, Callable
from szurubooru import db, model, errors, rest
2017-04-24 21:30:53 +00:00
from szurubooru.func import users, scores, serialization
2016-04-24 07:50:11 +00:00
class InvalidCommentIdError(errors.ValidationError):
pass
class CommentNotFoundError(errors.NotFoundError):
pass
class EmptyCommentTextError(errors.ValidationError):
pass
2016-04-24 07:50:11 +00:00
class CommentSerializer(serialization.BaseSerializer):
def __init__(self, comment: model.Comment, auth_user: model.User) -> None:
self.comment = comment
self.auth_user = auth_user
def _serializers(self) -> Dict[str, Callable[[], Any]]:
return {
'id': self.serialize_id,
'user': self.serialize_user,
'postId': self.serialize_post_id,
'version': self.serialize_version,
'text': self.serialize_text,
'creationTime': self.serialize_creation_time,
'lastEditTime': self.serialize_last_edit_time,
'score': self.serialize_score,
'ownScore': self.serialize_own_score,
}
def serialize_id(self) -> Any:
return self.comment.comment_id
def serialize_user(self) -> Any:
return users.serialize_micro_user(self.comment.user, self.auth_user)
def serialize_post_id(self) -> Any:
return self.comment.post.post_id
def serialize_version(self) -> Any:
return self.comment.version
def serialize_text(self) -> Any:
return self.comment.text
def serialize_creation_time(self) -> Any:
return self.comment.creation_time
def serialize_last_edit_time(self) -> Any:
return self.comment.last_edit_time
def serialize_score(self) -> Any:
return self.comment.score
def serialize_own_score(self) -> Any:
return scores.get_score(self.comment, self.auth_user)
def serialize_comment(
comment: model.Comment,
auth_user: model.User,
2017-04-24 21:30:53 +00:00
options: List[str] = []) -> rest.Response:
if comment is None:
return None
return CommentSerializer(comment, auth_user).serialize(options)
def try_get_comment_by_id(comment_id: int) -> Optional[model.Comment]:
comment_id = int(comment_id)
2017-04-24 21:30:53 +00:00
return (
db.session
.query(model.Comment)
.filter(model.Comment.comment_id == comment_id)
.one_or_none())
2016-04-24 08:13:22 +00:00
def get_comment_by_id(comment_id: int) -> model.Comment:
comment = try_get_comment_by_id(comment_id)
if comment:
return comment
raise CommentNotFoundError('Comment %r not found.' % comment_id)
def create_comment(
user: model.User, post: model.Post, text: str) -> model.Comment:
comment = model.Comment()
2016-04-24 07:50:11 +00:00
comment.user = user
comment.post = post
update_comment_text(comment, text)
comment.creation_time = datetime.utcnow()
2016-04-24 07:50:11 +00:00
return comment
def update_comment_text(comment: model.Comment, text: str) -> None:
2016-08-14 08:45:00 +00:00
assert comment
2016-04-24 07:50:11 +00:00
if not text:
raise EmptyCommentTextError('Comment text cannot be empty.')
comment.text = text