blog.py 7.4 KB

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