blog.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from sql.blog import (get_blog_list,
  2. get_blog_count,
  3. get_blog_list_with_file,
  4. get_blog_with_file_count,
  5. get_blog_list_not_top,
  6. read_blog,
  7. write_blog,
  8. get_blog_user_count)
  9. import core.user
  10. import core.file
  11. import core.comment
  12. class LoadBlogError(Exception):
  13. pass
  14. def load_blog_by_id(blog_id) -> "BlogArticle":
  15. blog_id = blog_id
  16. blog = read_blog(blog_id)
  17. if len(blog) == 0:
  18. raise LoadBlogError
  19. try:
  20. auth = core.user.User.load_user_by_id(blog[0])
  21. except core.user.LoaderUserError:
  22. raise LoadBlogError
  23. title = blog[1]
  24. subtitle = blog[2]
  25. context = blog[3]
  26. update_time = blog[6]
  27. top = blog[7]
  28. comment = core.comment.load_comment_list(blog_id)
  29. file = core.file.File.get_blog_file(blog_id)
  30. return BlogArticle(blog_id, auth, title, subtitle, context, update_time, top, comment, file)
  31. class BlogArticle:
  32. def __init__(self, blog_id, auth, title, subtitle, context, update_time=None, top=False, comment=None, file=None):
  33. self.blog_id = blog_id
  34. self.user = auth
  35. self.title = title
  36. self.subtitle = subtitle
  37. self.context = context
  38. self.update_time = update_time
  39. self.top = top
  40. self.comment = [] if comment is None else comment
  41. self.file = [] if file is None else file
  42. @staticmethod
  43. def get_blog_list(file_id=None, limit=None, offset=None, not_top=False):
  44. if file_id is None:
  45. if not_top:
  46. return get_blog_list_not_top(limit=limit, offset=offset)
  47. return get_blog_list(limit=limit, offset=offset)
  48. return get_blog_list_with_file(file_id, limit=limit, offset=offset)
  49. @staticmethod
  50. def get_blog_count(file_id=None, auth=None):
  51. if file_id is None:
  52. return get_blog_count()
  53. if auth is None:
  54. return get_blog_with_file_count(file_id)
  55. return get_blog_user_count(auth.get_user_id())
  56. def write_blog(self):
  57. if self.blog_id is not None: # 只有 blog_id为None时才使用
  58. return False
  59. return write_blog(self.user.get_user_id(), self.title, self.subtitle, self.context, self.file)