blog.py 2.4 KB

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