blog.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. create_time = blog[5]
  30. top = blog[6]
  31. comment = object.comment.load_comment_list(blog_id)
  32. archive = object.archive.Archive.get_blog_archive(blog_id)
  33. return BlogArticle(blog_id, auth, title, subtitle, context, update_time, create_time, top, comment, archive)
  34. class BlogArticle:
  35. def __init__(self, blog_id, auth, title, subtitle, context, update_time=None, create_time=None, top=False, comment=None, archive=None):
  36. self.blog_id = blog_id
  37. self.user = auth
  38. self.title = title
  39. self.subtitle = subtitle
  40. self.context = context
  41. self.update_time = update_time
  42. self.create_time = create_time
  43. self.top = top
  44. self.comment = [] if comment is None else comment
  45. self.archive = [] if archive is None else archive
  46. @staticmethod
  47. def get_blog_list(archive_id=None, limit=None, offset=None, not_top=False):
  48. if archive_id is None:
  49. if not_top:
  50. return get_blog_list_not_top(limit=limit, offset=offset)
  51. return get_blog_list(limit=limit, offset=offset)
  52. return get_archive_blog_list(archive_id, limit=limit, offset=offset)
  53. @staticmethod
  54. def get_blog_count(archive_id=None, auth=None):
  55. if archive_id is None:
  56. return get_blog_count()
  57. if auth is None:
  58. return get_archive_blog_count(archive_id)
  59. return get_user_user_count(auth.get_user_id())
  60. def create(self):
  61. if self.blog_id is not None: # 只有 blog_id为None时才使用
  62. return False
  63. return create_blog(self.user.get_user_id(), self.title, self.subtitle, self.context, self.archive)
  64. def delete(self):
  65. return delete_blog(self.blog_id)
  66. def update(self, context: str):
  67. if update_blog(self.blog_id, context):
  68. self.context = context
  69. return True
  70. return False