blog.py 2.6 KB

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