1
0

blog.py 3.5 KB

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