2017-02-04 00:08:12 +00:00
|
|
|
from datetime import datetime
|
2020-06-05 22:03:37 +00:00
|
|
|
from typing import Any, Callable, Dict, List, Optional
|
|
|
|
|
|
|
|
from szurubooru import db, errors, model, rest
|
|
|
|
from szurubooru.func import scores, serialization, users
|
2016-04-24 07:50:11 +00:00
|
|
|
|
2016-08-14 12:22:53 +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
|
|
|
|
2017-02-04 00:08:12 +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 {
|
2020-06-05 22:03:37 +00:00
|
|
|
"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,
|
2017-02-04 00:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2020-06-05 22:03:37 +00:00
|
|
|
comment: model.Comment, auth_user: model.User, options: List[str] = []
|
|
|
|
) -> rest.Response:
|
2017-02-04 00:08:12 +00:00
|
|
|
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 (
|
2020-06-05 22:03:37 +00:00
|
|
|
db.session.query(model.Comment)
|
2017-04-24 21:30:53 +00:00
|
|
|
.filter(model.Comment.comment_id == comment_id)
|
2020-06-05 22:03:37 +00:00
|
|
|
.one_or_none()
|
|
|
|
)
|
2016-04-24 08:13:22 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def get_comment_by_id(comment_id: int) -> model.Comment:
|
2016-04-24 12:24:41 +00:00
|
|
|
comment = try_get_comment_by_id(comment_id)
|
|
|
|
if comment:
|
|
|
|
return comment
|
2020-06-05 22:03:37 +00:00
|
|
|
raise CommentNotFoundError("Comment %r not found." % comment_id)
|
2016-04-24 12:24:41 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def create_comment(
|
2020-06-05 22:03:37 +00:00
|
|
|
user: model.User, post: model.Post, text: str
|
|
|
|
) -> model.Comment:
|
2017-02-04 00:08:12 +00:00
|
|
|
comment = model.Comment()
|
2016-04-24 07:50:11 +00:00
|
|
|
comment.user = user
|
|
|
|
comment.post = post
|
|
|
|
update_comment_text(comment, text)
|
2017-02-04 00:08:12 +00:00
|
|
|
comment.creation_time = datetime.utcnow()
|
2016-04-24 07:50:11 +00:00
|
|
|
return comment
|
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
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:
|
2020-06-05 22:03:37 +00:00
|
|
|
raise EmptyCommentTextError("Comment text cannot be empty.")
|
2016-04-24 07:50:11 +00:00
|
|
|
comment.text = text
|