api_bbs_w_comment.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. from .tool.func import *
  2. def api_bbs_w_comment(sub_code : str = '') -> flask.Response:
  3. conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection]
  4. with get_db_connect() as conn:
  5. curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor] = 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.Optional[list[tuple[str, str, str]]] = 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_a : tuple[str, str, str]
  15. for for_a in db_data:
  16. if temp_id != for_a[2]:
  17. if temp_dict != {}:
  18. temp_list += [dict(temp_dict)]
  19. temp_id = for_a[2]
  20. temp_dict['code'] = for_a[2]
  21. temp_dict[for_a[0]] = for_a[1]
  22. if temp_dict != {}:
  23. temp_list += [dict(temp_dict)]
  24. return flask.jsonify(temp_list)