blog.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from sql import db
  2. from sql.archive import add_blog_to_archive
  3. from typing import Optional, List
  4. import object.archive
  5. def create_blog(auth_id: int, title: str, subtitle: str, content: str,
  6. archive_list: List[object.archive.Archive]) -> bool:
  7. """ 写入新的blog """
  8. title = title.replace("'", "''")
  9. subtitle = subtitle.replace("'", "''")
  10. content = content.replace("'", "''")
  11. cur = db.insert("INSERT INTO blog(Auth, Title, SubTitle, Content) "
  12. "VALUES (%s, %s, %s, %s)", auth_id, title, subtitle, content)
  13. if cur is None or cur.rowcount == 0:
  14. return False
  15. blog_id = cur.lastrowid
  16. for archive in archive_list:
  17. if not add_blog_to_archive(blog_id, archive.id):
  18. return False
  19. return True
  20. def update_blog(blog_id: int, content: str) -> bool:
  21. """ 更新博客文章 """
  22. content = content.replace("'", "''")
  23. cur = db.update("Update blog "
  24. "SET UpdateTime=CURRENT_TIMESTAMP(), Content=%s "
  25. "WHERE ID=%s", content, blog_id)
  26. if cur is None or cur.rowcount != 1:
  27. return False
  28. return True
  29. def read_blog(blog_id: int) -> list:
  30. """ 读取blog内容 """
  31. cur = db.search("SELECT Auth, Title, SubTitle, Content, UpdateTime, CreateTime, Top "
  32. "FROM blog "
  33. "WHERE ID=%s", blog_id)
  34. if cur is None or cur.rowcount == 0:
  35. return [-1, "", "", "", 0, -1, False]
  36. return cur.fetchone()
  37. def delete_blog(blog_id: int):
  38. cur = db.delete("DELETE FROM blog_archive WHERE BlogID=%s", blog_id)
  39. if cur is None:
  40. return False
  41. cur = db.delete("DELETE FROM comment WHERE BlogID=%s", blog_id)
  42. if cur is None:
  43. return False
  44. cur = db.delete("DELETE FROM blog WHERE ID=%s", blog_id)
  45. if cur is None or cur.rowcount == 0:
  46. return False
  47. return True
  48. def set_blog_top(blog_id: int, top: bool = True):
  49. cur = db.update("UPDATE blog "
  50. "SET Top=%s "
  51. "WHERE ID=%s", 1 if top else 0, blog_id)
  52. if cur is None or cur.rowcount != 1:
  53. return False
  54. return True
  55. def get_blog_list(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  56. """ 获得 blog 列表 """
  57. if limit is not None and offset is not None:
  58. cur = db.search("SELECT ID, Title, SubTitle, UpdateTime, CreateTime, Top "
  59. "FROM blog "
  60. "ORDER BY Top DESC, CreateTime DESC, Title, SubTitle "
  61. "LIMIT %s OFFSET %s", limit, offset)
  62. else:
  63. cur = db.search("SELECT ID, Title, SubTitle, UpdateTime, CreateTime, Top "
  64. "FROM blog "
  65. "ORDER BY Top DESC, CreateTime DESC, Title, SubTitle")
  66. if cur is None or cur.rowcount == 0:
  67. return []
  68. return cur.fetchall()
  69. def get_blog_list_not_top(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  70. """ 获得blog列表 忽略置顶 """
  71. if limit is not None and offset is not None:
  72. cur = db.search("SELECT ID, Title, SubTitle, UpdateTime, CreateTime "
  73. "FROM blog "
  74. "ORDER BY CreateTime DESC, Title, SubTitle "
  75. "LIMIT %s OFFSET %s", limit, offset)
  76. else:
  77. cur = db.search("SELECT ID, Title, SubTitle, UpdateTime, CreateTime "
  78. "FROM blog "
  79. "ORDER BY CreateTime DESC, Title, SubTitle")
  80. if cur is None or cur.rowcount == 0:
  81. return []
  82. return cur.fetchall()
  83. def get_blog_count() -> int:
  84. """ 统计 blog 个数 """
  85. cur = db.search("SELECT COUNT(*) FROM blog")
  86. if cur is None or cur.rowcount == 0:
  87. return 0
  88. return cur.fetchone()[0]
  89. def get_archive_blog_list(archive_id, 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 BlogID, Title, SubTitle, UpdateTime, CreateTime, Top "
  93. "FROM blog_with_archive "
  94. "WHERE ArchiveID=%s "
  95. "ORDER BY Top DESC, CreateTime DESC, Title, SubTitle "
  96. "LIMIT %s OFFSET %s", archive_id, limit, offset)
  97. else:
  98. cur = db.search("SELECT BlogID, Title, SubTitle, UpdateTime, CreateTime, Top "
  99. "FROM blog_with_archive "
  100. "WHERE ArchiveID=%s "
  101. "ORDER BY Top DESC, CreateTime DESC, Title, SubTitle")
  102. if cur is None or cur.rowcount == 0:
  103. return []
  104. return cur.fetchall()
  105. def get_archive_blog_count(archive_id) -> int:
  106. """ 统计指定归档的 blog 个数 """
  107. cur = db.search("SELECT COUNT(*) FROM blog_with_archive WHERE ArchiveID=%s", archive_id)
  108. if cur is None or cur.rowcount == 0:
  109. return 0
  110. return cur.fetchone()[0]
  111. def get_user_user_count(user_id: int) -> int:
  112. """ 获得指定用户的 blog 个数 """
  113. cur = db.search("SELECT COUNT(*) FROM blog WHERE Auth=%s", user_id)
  114. if cur is None or cur.rowcount == 0:
  115. return 0
  116. return cur.fetchone()[0]