blog.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from sql import db
  2. from typing import Optional, List
  3. import object.archive
  4. def create_blog(auth_id: int, title: str, subtitle: str, content: str,
  5. archive_list: List[object.archive.Archive]) -> bool:
  6. """ 写入新的blog """
  7. title = title.replace("'", "''")
  8. subtitle = subtitle.replace("'", "''")
  9. content = content.replace("'", "''")
  10. cur = db.insert(table="blog", columns=["Auth", "Title", "SubTitle", "Content"],
  11. values=f"{auth_id}, '{title}', '{subtitle}', '{content}'")
  12. if cur is None or cur.rowcount == 0:
  13. return False
  14. blog_id = cur.lastrowid
  15. for archive in archive_list:
  16. cur = db.insert(table="blog_archive", columns=["BlogID", "ArchiveID"],
  17. values=f"{blog_id}, {archive.archive_id}")
  18. if cur is None or cur.rowcount == 0:
  19. return False
  20. return True
  21. def update_blog(blog_id: int, content: str) -> bool:
  22. """ 更新博客文章 """
  23. content = content.replace("'", "''")
  24. cur = db.update(table="blog",
  25. kw={"UpdateTime": "CURRENT_TIMESTAMP()", "Content": f"'{content}'"},
  26. where=f"ID={blog_id}")
  27. if cur is None or cur.rowcount != 1:
  28. return False
  29. return True
  30. def read_blog(blog_id: int) -> list:
  31. """ 读取blog内容 """
  32. cur = db.search(columns=["Auth", "Title", "SubTitle", "Content", "UpdateTime", "CreateTime", "Top"],
  33. table="blog",
  34. where=f"ID={blog_id}")
  35. if cur is None or cur.rowcount == 0:
  36. return []
  37. return cur.fetchone()
  38. def delete_blog(blog_id: int):
  39. cur = db.delete(table="blog_archive", where=f"BlogID={blog_id}")
  40. if cur is None:
  41. return False
  42. cur = db.delete(table="comment", where=f"BlogID={blog_id}")
  43. if cur is None:
  44. return False
  45. cur = db.delete(table="blog", where=f"ID={blog_id}")
  46. if cur is None or cur.rowcount == 0:
  47. return False
  48. return True
  49. def set_blog_top(blog_id: int, top: bool = True):
  50. cur = db.update(table="blog", kw={"Top": "1" if top else "0"}, where=f"ID={blog_id}")
  51. if cur is None or cur.rowcount != 1:
  52. return False
  53. return True
  54. def get_blog_list(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  55. """ 获得 blog 列表 """
  56. cur = db.search(columns=["ID", "Title", "SubTitle", "UpdateTime", "CreateTime", "Top"], table="blog_with_top",
  57. order_by=[("CreateTime", "DESC"), ("Title", "ASC"), ("SubTitle", "ASC")],
  58. limit=limit,
  59. offset=offset)
  60. if cur is None or cur.rowcount == 0:
  61. return []
  62. return cur.fetchall()
  63. def get_blog_list_not_top(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  64. """ 获得blog列表 忽略置顶 """
  65. cur = db.search(columns=["ID", "Title", "SubTitle", "UpdateTime", "CreateTime"], table="blog",
  66. order_by=[("CreateTime", "DESC"), ("Title", "ASC"), ("SubTitle", "ASC")],
  67. limit=limit,
  68. offset=offset)
  69. if cur is None or cur.rowcount == 0:
  70. return []
  71. return cur.fetchall()
  72. def get_blog_count() -> int:
  73. """ 统计 blog 个数 """
  74. cur = db.search(columns=["count(ID)"], table="blog")
  75. if cur is None or cur.rowcount == 0:
  76. return 0
  77. return cur.fetchone()[0]
  78. def get_archive_blog_list(archive_id, limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  79. """ 获得指定归档的 blog 列表 """
  80. cur = db.search(columns=["BlogID", "Title", "SubTitle", "UpdateTime", "CreateTime", "Top"], table="blog_with_archive",
  81. order_by=[("CreateTime", "DESC"), ("Title", "ASC"), ("SubTitle", "ASC")],
  82. where=f"ArchiveID={archive_id}",
  83. limit=limit,
  84. offset=offset)
  85. if cur is None or cur.rowcount == 0:
  86. return []
  87. return cur.fetchall()
  88. def get_archive_blog_count(archive_id) -> int:
  89. """ 统计指定归档的 blog 个数 """
  90. cur = db.search(columns=["count(ID)"], table="blog_with_archive",
  91. where=f"ArchiveID={archive_id}")
  92. if cur is None or cur.rowcount == 0:
  93. return 0
  94. return cur.fetchone()[0]
  95. def get_user_user_count(user_id: int) -> int:
  96. """ 获得指定用户的 blog 个数 """
  97. cur = db.search(columns=["count(ID)"], table="blog",
  98. where=f"Auth={user_id}")
  99. if cur is None or cur.rowcount == 0:
  100. return 0
  101. return cur.fetchone()[0]