2
0

api_topic.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from .tool.func import *
  2. from .bbs_w_post import bbs_w_post_make_thread
  3. def api_topic(topic_num = 1, tool = 'normal', num = '', render = ''):
  4. with get_db_connect() as conn:
  5. curs = conn.cursor()
  6. topic_num = str(topic_num)
  7. if acl_check('', 'topic_view', topic_num) != 1:
  8. if tool == 'normal':
  9. if num != '':
  10. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? and id + 0 = ? + 0 order by id + 0 asc"), [topic_num, num])
  11. else:
  12. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? order by id + 0 asc"), [topic_num])
  13. elif tool == 'top':
  14. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? and top = 'O' order by id + 0 asc"), [topic_num])
  15. else:
  16. # tool == 'length'
  17. curs.execute(db_change("select id from topic where code = ? order by id + 0 desc limit 1"), [topic_num])
  18. db_data = curs.fetchall()
  19. if db_data:
  20. return flask.jsonify({ 'length' : db_data[0][0] })
  21. else:
  22. return flask.jsonify({})
  23. data = curs.fetchall()
  24. if data:
  25. data_a = {}
  26. admin = admin_check(3)
  27. curs.execute(db_change("select ip from topic where code = ? order by id + 0 asc limit 1"), [topic_num])
  28. data_f = curs.fetchall()
  29. data_f = data_f[0][0] if data_f else ''
  30. data_a['data_main'] = {
  31. "ip_first" : ip_pas(data_f, 1),
  32. "admin" : str(admin)
  33. }
  34. data_a['data'] = []
  35. ip_a = ip_pas([i[3] for i in data])
  36. ip_a_2 = ip_pas([i[3] for i in data], 1)
  37. for i in data:
  38. data_v = i[1] if i[4] != 'O' or admin == 1 else ''
  39. if data_v != '':
  40. data_v = render_set(
  41. doc_data = data_v,
  42. data_type = 'api_thread',
  43. data_in = 'topic_' + topic_num + '_' + i[0]
  44. )
  45. else:
  46. data_v = ['', '']
  47. data_a['data'] += [{
  48. "id" : i[0],
  49. "data" : data_v,
  50. "date" : i[2],
  51. "ip" : ip_a_2[i[3]],
  52. "blind" : i[4],
  53. "ip_pas" : ip_a[i[3]],
  54. "data_pas" : data_v
  55. }]
  56. if render == '':
  57. return flask.jsonify(data_a)
  58. else:
  59. data_r = ''
  60. if 'data' in data_a:
  61. for for_a in data_a['data']:
  62. if tool == 'top':
  63. color = 'red'
  64. else:
  65. if data_a['data_main']["ip_first"] == for_a["ip"]:
  66. color = 'green'
  67. else:
  68. color = 'default'
  69. data_r += bbs_w_post_make_thread(
  70. for_a["ip_pas"],
  71. '<a href="/thread/' + topic_num + '/comment/' + for_a["id"] + '/tool">(' + load_lang('tool') + ')</a> ' + for_a["date"],
  72. for_a["data_pas"][0] + '<script>' + for_a["data_pas"][1] + '</script>',
  73. for_a["id"],
  74. color = color,
  75. blind = for_a["blind"],
  76. add_style = ''
  77. )
  78. data_r += '<hr class="main_hr">'
  79. return flask.jsonify({ "data" : data_r })
  80. else:
  81. return flask.jsonify({})
  82. else:
  83. return flask.jsonify({})