blog.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from typing import List, Optional
  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_blog_count)
  14. from sql.statistics import get_blog_click
  15. from sql.archive import add_blog_to_archive, sub_blog_from_archive
  16. from sql.user import get_user_email
  17. from sql.base import DBBit
  18. import object.user
  19. import object.archive
  20. import object.comment
  21. class LoadBlogError(Exception):
  22. pass
  23. class _BlogArticle:
  24. article_tuple = namedtuple("Article", "auth title subtitle content update_time create_time top")
  25. @staticmethod
  26. def get_blog_list(archive_id=None, limit=None, offset=None, not_top=False):
  27. if archive_id is None:
  28. if not_top:
  29. res = get_blog_list_not_top(limit=limit, offset=offset)
  30. else:
  31. res = get_blog_list(limit=limit, offset=offset)
  32. else:
  33. res = get_archive_blog_list(archive_id, limit=limit, offset=offset)
  34. ret = []
  35. for i in res:
  36. ret.append(BlogArticle(i))
  37. return ret
  38. @staticmethod
  39. def get_blog_count(archive_id=None, auth=None):
  40. if archive_id is None and auth is None:
  41. return get_blog_count()
  42. if auth is None:
  43. return get_archive_blog_count(archive_id)
  44. return get_user_blog_count(auth.id)
  45. @staticmethod
  46. def create(title, subtitle, content, archive: "List[object.archive.Archive]", user: "object.user.User"):
  47. return create_blog(user.id, title, subtitle, content, archive)
  48. class BlogArticle(_BlogArticle):
  49. def __init__(self, blog_id):
  50. self.id = blog_id
  51. @property
  52. def info(self):
  53. return BlogArticle.article_tuple(*read_blog(self.id))
  54. @property
  55. def user(self):
  56. return object.user.User(get_user_email(self.info.auth))
  57. @property
  58. def title(self):
  59. return self.info.title
  60. @property
  61. def subtitle(self):
  62. return self.info.subtitle
  63. @property
  64. def content(self):
  65. return self.info.content
  66. @property
  67. def update_time(self):
  68. return self.info.update_time
  69. @property
  70. def create_time(self):
  71. return self.info.create_time
  72. @property
  73. def clicks(self):
  74. return get_blog_click(self.id)
  75. @property
  76. def top(self):
  77. return self.info.top == DBBit.BIT_1
  78. @top.setter
  79. def top(self, top: bool):
  80. set_blog_top(self.id, top)
  81. @property
  82. def comment(self):
  83. return object.comment.load_comment_list(self.id)
  84. @property
  85. def archive(self):
  86. return object.archive.Archive.get_blog_archive(self.id)
  87. @property
  88. def is_delete(self):
  89. return not self.user.is_authenticated and len(self.content) != 0
  90. def delete(self):
  91. return delete_blog(self.id)
  92. def update(self, content: str):
  93. if update_blog(self.id, content):
  94. return True
  95. return False
  96. def add_to_archive(self, archive_id: int):
  97. return add_blog_to_archive(self.id, archive_id)
  98. def sub_from_archive(self, archive_id: int):
  99. return sub_blog_from_archive(self.id, archive_id)