blog.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, context: str,
  5. archive_list: List[object.archive.Archive]) -> bool:
  6. """ 写入新的blog """
  7. title = title.replace("'", "''")
  8. subtitle = subtitle.replace("'", "''")
  9. context = context.replace("'", "''")
  10. cur = db.insert(table="blog", columns=["Auth", "Title", "SubTitle", "Context"],
  11. values=f"{auth_id}, '{title}', '{subtitle}', '{context}'")
  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, context: str) -> bool:
  22. """ 更新博客文章 """
  23. context = context.replace("'", "''")
  24. cur = db.update(table="blog",
  25. kw={"UpdateTime": "CURRENT_TIMESTAMP()", "Context": f"'{context}'"},
  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", "Context", "UpdateTime", "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 get_blog_list(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  50. """ 获得 blog 列表 """
  51. cur = db.search(columns=["ID", "Title", "SubTitle", "UpdateTime", "Top"], table="blog_with_top",
  52. limit=limit,
  53. offset=offset)
  54. if cur is None or cur.rowcount == 0:
  55. return []
  56. return cur.fetchall()
  57. def get_blog_list_not_top(limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  58. """ 获得blog列表 忽略置顶 """
  59. cur = db.search(columns=["ID", "Title", "SubTitle", "UpdateTime"], table="blog",
  60. order_by=[("UpdateTime", "DESC")],
  61. limit=limit,
  62. offset=offset)
  63. if cur is None or cur.rowcount == 0:
  64. return []
  65. return cur.fetchall()
  66. def get_blog_count() -> int:
  67. """ 统计 blog 个数 """
  68. cur = db.search(columns=["count(ID)"], table="blog")
  69. if cur is None or cur.rowcount == 0:
  70. return 0
  71. return cur.fetchone()[0]
  72. def get_archive_blog_list(archive_id, limit: Optional[int] = None, offset: Optional[int] = None) -> list:
  73. """ 获得指定归档的 blog 列表 """
  74. cur = db.search(columns=["BlogID", "Title", "SubTitle", "UpdateTime", "Top"], table="blog_with_archive",
  75. where=f"ArchiveID={archive_id}",
  76. limit=limit,
  77. offset=offset)
  78. if cur is None or cur.rowcount == 0:
  79. return []
  80. return cur.fetchall()
  81. def get_archive_blog_count(archive_id) -> int:
  82. """ 统计指定归档的 blog 个数 """
  83. cur = db.search(columns=["count(ID)"], table="blog_with_archive",
  84. where=f"ArchiveID={archive_id}")
  85. if cur is None or cur.rowcount == 0:
  86. return 0
  87. return cur.fetchone()[0]
  88. def get_user_user_count(user_id: int) -> int:
  89. """ 获得指定用户的 blog 个数 """
  90. cur = db.search(columns=["count(ID)"], table="blog",
  91. where=f"Auth={user_id}")
  92. if cur is None or cur.rowcount == 0:
  93. return 0
  94. return cur.fetchone()[0]