blog.py 3.1 KB

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