bbs_w_post.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. from .tool.func import *
  2. from .api_bbs_w_post import api_bbs_w_post
  3. from .api_bbs_w_comment import api_bbs_w_comment
  4. from .go_api_topic import api_topic_thread_make, api_topic_thread_pre_render
  5. from .edit import edit_editor
  6. def bbs_w_post_comment(conn, user_id, sub_code, comment_num, bbs_num_str, post_num_str):
  7. comment_data = ''
  8. comment_select = ''
  9. comment_count = 0
  10. comment_add_count = 0
  11. thread_data = orjson.loads(api_bbs_w_comment(sub_code).data)
  12. comment_count += len(thread_data)
  13. comment_add_count += comment_count
  14. for temp_dict in thread_data:
  15. if temp_dict['comment_user_id'] != '':
  16. color = 'default'
  17. if user_id == temp_dict['comment_user_id']:
  18. color = 'green'
  19. sub_code_check = re.sub(r'^[0-9]+-[0-9]+-', '', sub_code + '-' + temp_dict['code'])
  20. margin_count = sub_code_check.count('-')
  21. date = ''
  22. date += '<a href="javascript:opennamu_change_comment(\'' + sub_code_check + '\');">(' + get_lang(conn, 'comment') + ')</a> '
  23. date += '<a href="/bbs/tool/' + bbs_num_str + '/' + post_num_str + '/' + sub_code_check + '">(' + get_lang(conn, 'tool') + ')</a> '
  24. date += temp_dict['comment_date']
  25. comment_data += '<span style="padding-left: 20px;"></span>' * margin_count
  26. comment_data += api_topic_thread_make(
  27. ip_pas(temp_dict['comment_user_id']),
  28. date,
  29. render_set(conn, doc_data = temp_dict['comment']),
  30. sub_code_check,
  31. color = color,
  32. add_style = 'width: calc(100% - ' + str(margin_count * 20) + 'px);'
  33. )
  34. comment_default = ''
  35. if comment_num == sub_code_check:
  36. comment_default = 'selected'
  37. comment_select += '<option value="' + sub_code_check + '" ' + comment_default + '>' + sub_code_check + '</option>'
  38. temp_data = bbs_w_post_comment(conn, user_id, sub_code + '-' + temp_dict['code'], comment_num, bbs_num_str, post_num_str)
  39. comment_data += temp_data[0]
  40. comment_select += temp_data[1]
  41. comment_add_count += temp_data[3]
  42. return (comment_data, comment_select, comment_count, comment_add_count)
  43. def bbs_w_post(bbs_num = '', post_num = ''):
  44. with get_db_connect() as conn:
  45. curs = conn.cursor()
  46. curs.execute(db_change('select set_data from bbs_set where set_id = ? and set_name = "bbs_name"'), [bbs_num])
  47. db_data_3 = curs.fetchall()
  48. if not db_data_3:
  49. return redirect(conn, '/bbs/main')
  50. bbs_name = db_data_3[0][0]
  51. bbs_num_str = str(bbs_num)
  52. post_num_str = str(post_num)
  53. bbs_comment_acl = acl_check(bbs_num_str, 'bbs_comment')
  54. ip = ip_check()
  55. temp_dict = orjson.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
  56. if temp_dict == {}:
  57. return redirect(conn, '/bbs/main')
  58. curs.execute(db_change('select set_data from bbs_set where set_id = ? and set_name = "bbs_type"'), [bbs_num])
  59. db_data_2 = curs.fetchall()
  60. if not db_data_2:
  61. return redirect(conn, '/bbs/main')
  62. elif db_data_2[0][0] == 'thread':
  63. if flask.request.method == 'POST':
  64. if bbs_comment_acl == 1:
  65. return redirect(conn, '/bbs/set/' + bbs_num_str)
  66. if captcha_post(conn, flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  67. return re_error(conn, '/error/13')
  68. set_id = bbs_num_str + '-' + post_num_str
  69. curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? order by set_code + 0 desc'), [set_id])
  70. db_data_4 = curs.fetchall()
  71. id_data = str(int(db_data_4[0][0]) + 1) if db_data_4 else '1'
  72. data = flask.request.form.get('content', '')
  73. if data == '':
  74. # re_error로 대체 예정
  75. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str)
  76. data = data.replace('\r', '')
  77. data = api_topic_thread_pre_render(conn, data, id_data, ip, set_id, bbs_name, temp_dict['title'], 'post')
  78. date = get_time()
  79. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
  80. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
  81. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_user_id', ?, ?, ?)"), [id_data, set_id, ip])
  82. add_alarm(conn, temp_dict['user_id'], ip, 'BBS <a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + id_data + '">' + html.escape(bbs_name) + ' - ' + html.escape(temp_dict['title']) + '#' + id_data + '</a>')
  83. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + id_data)
  84. else:
  85. if acl_check(bbs_num_str, 'bbs_view') == 1:
  86. return re_error(conn, '/ban')
  87. text = ''
  88. date = ''
  89. date += temp_dict['date']
  90. data = ''
  91. data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
  92. data += api_topic_thread_make(
  93. ip_pas(temp_dict['user_id']),
  94. date,
  95. render_set(conn, doc_data = temp_dict['data'], data_type = 'thread'),
  96. '0',
  97. color = 'green'
  98. )
  99. user_id = temp_dict['user_id']
  100. count = 0
  101. thread_data = orjson.loads(api_bbs_w_comment(bbs_num_str + '-' + post_num_str).data)
  102. for temp_dict in thread_data:
  103. count += 1
  104. if user_id == temp_dict['comment_user_id']:
  105. color = 'green'
  106. else:
  107. color = 'default'
  108. date = ''
  109. date += '<a href="/bbs/tool/' + bbs_num_str + '/' + post_num_str + '/' + str(count) + '">(' + get_lang(conn, 'tool') + ')</a> '
  110. date += temp_dict['comment_date']
  111. data += api_topic_thread_make(
  112. ip_pas(temp_dict['comment_user_id']),
  113. date,
  114. render_set(conn, doc_data = temp_dict['comment'], data_type = 'thread'),
  115. str(count),
  116. color = color
  117. )
  118. data += '''
  119. <form method="post">
  120. ''' + (edit_editor(conn, ip, text, 'bbs_comment') if bbs_comment_acl == 0 else '') + '''
  121. </form>
  122. '''
  123. return easy_minify(conn, flask.render_template(skin_check(conn),
  124. imp = [bbs_name, wiki_set(conn), wiki_custom(conn), wiki_css(['(' + get_lang(conn, 'bbs') + ')', 0])],
  125. data = data,
  126. menu = [['bbs/in/' + bbs_num_str, get_lang(conn, 'return')], ['bbs/edit/' + bbs_num_str + '/' + post_num_str, get_lang(conn, 'edit')], ['bbs/tool/' + bbs_num_str + '/' + post_num_str, get_lang(conn, 'tool')]]
  127. ))
  128. else:
  129. # db_data_2[0][0] == 'comment'
  130. if flask.request.method == 'POST':
  131. if bbs_comment_acl == 1:
  132. return redirect(conn, '/bbs/set/' + bbs_num_str)
  133. if captcha_post(conn, flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  134. return re_error(conn, '/error/13')
  135. select = flask.request.form.get('comment_select', '0')
  136. select = '' if select == '0' else select
  137. comment_user_name = ''
  138. if select != '':
  139. select_split = select.split('-')
  140. if len(select_split) < 2:
  141. curs.execute(db_change('select set_data from bbs_data where set_name = "comment_user_id" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str, select_split[0]])
  142. db_data_6 = curs.fetchall()
  143. if not db_data_6:
  144. # re_error로 변경 예정
  145. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str)
  146. else:
  147. set_id = bbs_num_str + '-' + post_num_str + '-' + select_split[0]
  148. comment_user_name = db_data_6[0][0]
  149. else:
  150. curs.execute(db_change('select set_data from bbs_data where set_name = "comment_user_id" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split[0:len(select_split) - 1]), select_split[len(select_split) - 1]])
  151. db_data_7 = curs.fetchall()
  152. if not db_data_7:
  153. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str)
  154. else:
  155. set_id = bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split)
  156. comment_user_name = db_data_7[0][0]
  157. else:
  158. set_id = bbs_num_str + '-' + post_num_str
  159. curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? order by set_code + 0 desc limit 1'), [set_id])
  160. db_data_5 = curs.fetchall()
  161. id_data = str(int(db_data_5[0][0]) + 1) if db_data_5 else '1'
  162. data = flask.request.form.get('content', '')
  163. if data == '':
  164. # re_error로 대체 예정
  165. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str)
  166. date = get_time()
  167. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
  168. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
  169. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_user_id', ?, ?, ?)"), [id_data, set_id, ip])
  170. if set_id == '':
  171. end_id = id_data
  172. else:
  173. set_id = re.sub(r'^[0-9]+-[0-9]+-?', '', set_id)
  174. set_id += '-' if set_id != '' else ''
  175. end_id = set_id + id_data
  176. add_alarm(conn, temp_dict['user_id'], ip, 'BBS <a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + end_id + '">' + html.escape(bbs_name) + ' - ' + html.escape(temp_dict['title']) + '#' + end_id + '</a>')
  177. if comment_user_name != '':
  178. add_alarm(conn, comment_user_name, ip, 'BBS <a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + end_id + '">' + html.escape(bbs_name) + ' - ' + html.escape(temp_dict['title']) + '#' + end_id + '</a>')
  179. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + end_id)
  180. else:
  181. if acl_check(bbs_num_str, 'bbs_view') == 1:
  182. return re_error(conn, '/ban')
  183. text = ''
  184. comment_num = ''
  185. date = ''
  186. date += '<a href="javascript:opennamu_change_comment(\'0\');">(' + get_lang(conn, 'comment') + ')</a> '
  187. date += temp_dict['date']
  188. data = ''
  189. data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
  190. data += api_topic_thread_make(
  191. ip_pas(temp_dict['user_id']),
  192. date,
  193. render_set(conn, doc_data = temp_dict['data']),
  194. '0',
  195. color = 'red'
  196. )
  197. user_id = temp_dict['user_id']
  198. comment_data = ''
  199. comment_select = '<select id="opennamu_comment_select" name="comment_select">'
  200. comment_select += '<option value="0">' + get_lang(conn, 'normal') + '</option>'
  201. comment_count = 0
  202. comment_add_count = 0
  203. temp_data = bbs_w_post_comment(conn, user_id, bbs_num_str + '-' + post_num_str, comment_num, bbs_num_str, post_num_str)
  204. comment_data += temp_data[0]
  205. comment_select += temp_data[1]
  206. comment_count += temp_data[2]
  207. comment_add_count += temp_data[3]
  208. comment_add_count -= comment_count
  209. if comment_data != '':
  210. data += '<hr>'
  211. comment_select += '</select>'
  212. if comment_data != '':
  213. data += get_lang(conn, 'comment') + ' : ' + str(comment_count) + '<hr class="main_hr">'
  214. data += get_lang(conn, 'reply') + ' : ' + str(comment_add_count) + '<hr class="main_hr">'
  215. data += comment_data
  216. else:
  217. data += '<hr class="main_hr">'
  218. bbs_comment_form = ''
  219. if bbs_comment_acl == 0:
  220. bbs_comment_form += '''
  221. ''' + comment_select + ''' <a href="javascript:opennamu_return_comment();">(''' + get_lang(conn, 'return') + ''')</a>
  222. <hr class="main_hr">
  223. ''' + edit_editor(conn, ip, text, 'bbs_comment') + '''
  224. '''
  225. data += '''
  226. <form method="post">
  227. ''' + bbs_comment_form + '''
  228. </form>
  229. <script defer src="/views/main_css/js/route/bbs_w_post.js''' + cache_v() + '''"></script>
  230. '''
  231. return easy_minify(conn, flask.render_template(skin_check(conn),
  232. imp = [bbs_name, wiki_set(conn), wiki_custom(conn), wiki_css(['(' + get_lang(conn, 'bbs') + ')', 0])],
  233. data = data,
  234. menu = [['bbs/in/' + bbs_num_str, get_lang(conn, 'return')], ['bbs/edit/' + bbs_num_str + '/' + post_num_str, get_lang(conn, 'edit')], ['bbs/tool/' + bbs_num_str + '/' + post_num_str, get_lang(conn, 'tool')]]
  235. ))