blog.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from typing import Optional
  2. from sql.base import DBBit
  3. from sql.blog import (get_blog_list,
  4. get_blog_count,
  5. get_archive_blog_list,
  6. get_archive_blog_count,
  7. get_blog_list_not_top,
  8. read_blog,
  9. update_blog,
  10. create_blog,
  11. delete_blog,
  12. set_blog_top,
  13. get_user_user_count)
  14. from sql.archive import add_blog_to_archive, sub_blog_from_archive
  15. import object.user
  16. import object.archive
  17. import object.comment
  18. class LoadBlogError(Exception):
  19. pass
  20. def load_blog_by_id(blog_id) -> "Optional[BlogArticle]":
  21. blog_id = blog_id
  22. blog = read_blog(blog_id)
  23. if len(blog) == 0:
  24. return None
  25. auth = object.user.load_user_by_id(blog[0])
  26. if auth is None:
  27. return None
  28. title = blog[1]
  29. subtitle = blog[2]
  30. content = blog[3]
  31. update_time = blog[4]
  32. create_time = blog[5]
  33. top = blog[6] == DBBit.BIT_1
  34. comment = object.comment.load_comment_list(blog_id)
  35. archive = object.archive.Archive.get_blog_archive(blog_id)
  36. return BlogArticle(blog_id, auth, title, subtitle, content, update_time, create_time, top, comment, archive)
  37. class BlogArticle:
  38. def __init__(self, blog_id, auth, title, subtitle, content, update_time=None, create_time=None, top=False,
  39. comment=None, archive=None):
  40. self.blog_id = blog_id
  41. self.user = auth
  42. self.title = title
  43. self.subtitle = subtitle
  44. self.content = content
  45. self.update_time = update_time
  46. self.create_time = create_time
  47. self.top = top
  48. self.comment = [] if comment is None else comment
  49. self.archive = [] if archive is None else archive
  50. @staticmethod
  51. def get_blog_list(archive_id=None, limit=None, offset=None, not_top=False):
  52. if archive_id is None:
  53. if not_top:
  54. return get_blog_list_not_top(limit=limit, offset=offset)
  55. return get_blog_list(limit=limit, offset=offset)
  56. return get_archive_blog_list(archive_id, limit=limit, offset=offset)
  57. @staticmethod
  58. def get_blog_count(archive_id=None, auth=None):
  59. if archive_id is None:
  60. return get_blog_count()
  61. if auth is None:
  62. return get_archive_blog_count(archive_id)
  63. return get_user_user_count(auth.get_user_id())
  64. def create(self):
  65. if self.blog_id is not None: # 只有 blog_id为None时才使用
  66. return False
  67. return create_blog(self.user.get_user_id(), self.title, self.subtitle, self.content, self.archive)
  68. def delete(self):
  69. return delete_blog(self.blog_id)
  70. def update(self, content: str):
  71. if update_blog(self.blog_id, content):
  72. self.content = content
  73. return True
  74. return False
  75. def set_top(self, top: bool):
  76. set_blog_top(self.blog_id, top)
  77. def add_to_archive(self, archive_id: int):
  78. return add_blog_to_archive(self.blog_id, archive_id)
  79. def sub_from_archive(self, archive_id: int):
  80. return sub_blog_from_archive(self.blog_id, archive_id)