1
0

blog.py 2.9 KB

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