blog.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from sql import db
  2. from sql.base import DBBit
  3. from sql.archive import add_blog_to_archive
  4. from sql.cache import (write_blog_to_cache, get_blog_from_cache, delete_blog_from_cache,
  5. write_blog_count_to_cache, get_blog_count_from_cache, delete_blog_count_from_cache,
  6. write_archive_blog_count_to_cache, get_archive_blog_count_from_cache,
  7. delete_all_archive_blog_count_from_cache, delete_archive_blog_count_from_cache,
  8. write_user_blog_count_to_cache, get_user_blog_count_from_cache,
  9. delete_all_user_blog_count_from_cache, delete_user_blog_count_from_cache,
  10. delete_blog_archive_from_cache)
  11. import object.archive
  12. from typing import Optional, List
  13. def create_blog(auth_id: int, title: str, subtitle: str, content: str,
  14. archive_list: List[object.archive.Archive]) -> bool:
  15. """ 写入新的blog """
  16. delete_blog_count_from_cache()
  17. delete_user_blog_count_from_cache(auth_id)
  18. # archive cache 在下面循环删除
  19. cur = db.insert("INSERT INTO blog(Auth, Title, SubTitle, Content) "
  20. "VALUES (%s, %s, %s, %s)", auth_id, title, subtitle, content)
  21. if cur is None or cur.rowcount == 0:
  22. return False
  23. blog_id = cur.lastrowid
  24. for archive in archive_list:
  25. delete_archive_blog_count_from_cache(archive.id)
  26. if not add_blog_to_archive(blog_id, archive.id):
  27. return False
  28. return True
  29. def update_blog(blog_id: int, content: str) -> bool:
  30. """ 更新博客文章 """
  31. delete_blog_from_cache(blog_id)
  32. cur = db.update("Update blog "
  33. "SET UpdateTime=CURRENT_TIMESTAMP(), Content=%s "
  34. "WHERE ID=%s", content, blog_id)
  35. if cur is None or cur.rowcount != 1:
  36. return False
  37. return True
  38. def read_blog(blog_id: int) -> list:
  39. """ 读取blog内容 """
  40. res = get_blog_from_cache(blog_id)
  41. if res is not None:
  42. return res
  43. cur = db.search("SELECT Auth, Title, SubTitle, Content, UpdateTime, CreateTime, Top "
  44. "FROM blog "
  45. "WHERE ID=%s", blog_id)
  46. if cur is None or cur.rowcount == 0:
  47. return [-1, "", "", "", 0, -1, False]
  48. res = cur.fetchone()
  49. write_blog_to_cache(blog_id, *res, is_db_bit=True)
  50. return [*res[:6], res[-1] == DBBit.BIT_1]
  51. def delete_blog(blog_id: int):
  52. delete_blog_count_from_cache()
  53. delete_all_archive_blog_count_from_cache()
  54. delete_all_user_blog_count_from_cache()
  55. delete_blog_from_cache(blog_id)
  56. delete_blog_archive_from_cache(blog_id)
  57. cur = db.delete("DELETE FROM blog_archive WHERE BlogID=%s", blog_id)
  58. if cur is None:
  59. return False
  60. cur = db.delete("DELETE FROM comment WHERE BlogID=%s", blog_id)
  61. if cur is None:
  62. return False
  63. cur = db.delete("DELETE FROM blog WHERE ID=%s", blog_id)
  64. if cur is None or cur.rowcount == 0:
  65. return False
  66. return True
  67. def set_blog_top(blog_id: int, top: bool = True):
  68. delete_blog_from_cache(blog_id)
  69. cur = db.update("UPDATE blog "
  70. "SET Top=%s "
  71. "WHERE ID=%s", 1 if top else 0, blog_id)
  72. if cur is None or cur.rowcount != 1:
  73. return False
  74. return True
  75. def get_blog_list(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  76. """ 获得 blog 列表 """
  77. if limit is not None and offset is not None:
  78. cur = db.search("SELECT ID "
  79. "FROM blog "
  80. "ORDER BY Top DESC, CreateTime DESC, Title, SubTitle "
  81. "LIMIT %s OFFSET %s", limit, offset)
  82. else:
  83. cur = db.search("SELECT ID "
  84. "FROM blog "
  85. "ORDER BY Top DESC, CreateTime DESC, Title, SubTitle")
  86. if cur is None or cur.rowcount == 0:
  87. return []
  88. return [i[0] for i in cur.fetchall()]
  89. def get_blog_list_not_top(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  90. """ 获得blog列表 忽略置顶 """
  91. if limit is not None and offset is not None:
  92. cur = db.search("SELECT ID "
  93. "FROM blog "
  94. "ORDER BY CreateTime DESC, Title, SubTitle "
  95. "LIMIT %s OFFSET %s", limit, offset)
  96. else:
  97. cur = db.search("SELECT ID "
  98. "FROM blog "
  99. "ORDER BY CreateTime DESC, Title, SubTitle")
  100. if cur is None or cur.rowcount == 0:
  101. return []
  102. return [i[0] for i in cur.fetchall()]
  103. def get_archive_blog_list(archive_id, limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  104. """ 获得指定归档的 blog 列表 """
  105. if limit is not None and offset is not None:
  106. cur = db.search("SELECT BlogID "
  107. "FROM blog_with_archive "
  108. "WHERE ArchiveID=%s "
  109. "ORDER BY Top DESC, CreateTime DESC, Title, SubTitle "
  110. "LIMIT %s OFFSET %s", archive_id, limit, offset)
  111. else:
  112. cur = db.search("SELECT BlogID "
  113. "FROM blog_with_archive "
  114. "WHERE ArchiveID=%s "
  115. "ORDER BY Top DESC, CreateTime DESC, Title, SubTitle")
  116. if cur is None or cur.rowcount == 0:
  117. return []
  118. return [i[0] for i in cur.fetchall()]
  119. def get_blog_count() -> int:
  120. """ 统计 blog 个数 """
  121. res = get_blog_count_from_cache()
  122. if res is not None:
  123. return res
  124. cur = db.search("SELECT COUNT(*) FROM blog")
  125. if cur is None or cur.rowcount == 0:
  126. return 0
  127. res = cur.fetchone()[0]
  128. write_blog_count_to_cache(res)
  129. return res
  130. def get_archive_blog_count(archive_id) -> int:
  131. """ 统计指定归档的 blog 个数 """
  132. res = get_archive_blog_count_from_cache(archive_id)
  133. if res is not None:
  134. return res
  135. cur = db.search("SELECT COUNT(*) FROM blog_with_archive WHERE ArchiveID=%s", archive_id)
  136. if cur is None or cur.rowcount == 0:
  137. return 0
  138. res = cur.fetchone()[0]
  139. write_archive_blog_count_to_cache(archive_id, res)
  140. return res
  141. def get_user_blog_count(user_id: int) -> int:
  142. """ 获得指定用户的 blog 个数 """
  143. res = get_user_blog_count_from_cache(user_id)
  144. if res is not None:
  145. return res
  146. cur = db.search("SELECT COUNT(*) FROM blog WHERE Auth=%s", user_id)
  147. if cur is None or cur.rowcount == 0:
  148. return 0
  149. res = cur.fetchone()[0]
  150. write_user_blog_count_to_cache(user_id, res)
  151. return res