blog.py 3.1 KB

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