blog.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from typing import List
  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. set_blog_top,
  12. get_user_user_count)
  13. from sql.archive import add_blog_to_archive, sub_blog_from_archive
  14. from sql.user import get_user_email
  15. import object.user
  16. import object.archive
  17. import object.comment
  18. class LoadBlogError(Exception):
  19. pass
  20. class _BlogArticle:
  21. @staticmethod
  22. def get_blog_list(archive_id=None, limit=None, offset=None, not_top=False):
  23. if archive_id is None:
  24. if not_top:
  25. return get_blog_list_not_top(limit=limit, offset=offset)
  26. return get_blog_list(limit=limit, offset=offset)
  27. return get_archive_blog_list(archive_id, limit=limit, offset=offset)
  28. @staticmethod
  29. def get_blog_count(archive_id=None, auth=None):
  30. if archive_id is None:
  31. return get_blog_count()
  32. if auth is None:
  33. return get_archive_blog_count(archive_id)
  34. return get_user_user_count(auth.id)
  35. @staticmethod
  36. def create(title, subtitle, content, archive: "List[object.archive.Archive]", user: "object.user.User"):
  37. return create_blog(user.id, title, subtitle, content, archive)
  38. class BlogArticle(_BlogArticle):
  39. def __init__(self, blog_id):
  40. self.id = blog_id
  41. @property
  42. def info(self):
  43. return read_blog(self.id)
  44. @property
  45. def user(self):
  46. return object.user.User(get_user_email(self.info[0]))
  47. @property
  48. def title(self):
  49. return self.info[1]
  50. @property
  51. def subtitle(self):
  52. return self.info[2]
  53. @property
  54. def content(self):
  55. return self.info[3]
  56. @property
  57. def update_time(self):
  58. return self.info[4]
  59. @property
  60. def create_time(self):
  61. return self.info[5]
  62. @property
  63. def top(self):
  64. return self.info[6]
  65. @top.setter
  66. def top(self, top: bool):
  67. set_blog_top(self.id, top)
  68. @property
  69. def comment(self):
  70. return object.comment.load_comment_list(self.id)
  71. @property
  72. def archive(self):
  73. return object.archive.Archive.get_blog_archive(self.id)
  74. @property
  75. def is_delete(self):
  76. return not self.user.is_authenticated and len(self.content) != 0
  77. def delete(self):
  78. return delete_blog(self.id)
  79. def update(self, content: str):
  80. if update_blog(self.id, content):
  81. return True
  82. return False
  83. def add_to_archive(self, archive_id: int):
  84. return add_blog_to_archive(self.id, archive_id)
  85. def sub_from_archive(self, archive_id: int):
  86. return sub_blog_from_archive(self.id, archive_id)