api_bbs_w_comment.py 1.2 KB

123456789101112131415161718192021222324252627282930
  1. from .tool.func import *
  2. def api_bbs_w_comment(sub_code : str = '') -> str:
  3. conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection, None]
  4. with get_db_connect() as conn:
  5. curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor, None] = conn.cursor()
  6. curs.execute(db_change('select set_name, set_data, set_code, set_id from bbs_data where (set_name = "comment" or set_name = "comment_date" or set_name = "comment_user_id") and set_id = ? order by set_code + 0 asc'), [sub_code])
  7. db_data : typing.Union[list[typing.Tuple[str, str, str]], None] = curs.fetchall()
  8. if not db_data:
  9. return flask.jsonify({})
  10. else:
  11. temp_id : str = ''
  12. temp_dict : dict[str, str] = {}
  13. temp_list : list[dict[str, str]] = []
  14. for for_a in db_data:
  15. if temp_id != for_a[2]:
  16. if temp_dict != {}:
  17. temp_list += [dict(temp_dict)]
  18. temp_id = for_a[2]
  19. temp_dict['code'] = for_a[2]
  20. temp_dict[for_a[0]] = for_a[1]
  21. if temp_dict != {}:
  22. temp_list += [dict(temp_dict)]
  23. return flask.jsonify(temp_list)