bbs_w_post.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 = json.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(conn, 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(conn, bbs_num_str, 'bbs_comment')
  54. ip = ip_check()
  55. temp_dict = json.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. else:
  69. captcha_post(conn, '', 0)
  70. set_id = bbs_num_str + '-' + post_num_str
  71. 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])
  72. db_data_4 = curs.fetchall()
  73. id_data = str(int(db_data_4[0][0]) + 1) if db_data_4 else '1'
  74. data = flask.request.form.get('content', '')
  75. if data == '':
  76. # re_error로 대체 예정
  77. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str)
  78. data = data.replace('\r', '')
  79. data = api_topic_thread_pre_render(conn, data, id_data, ip, set_id, bbs_name, temp_dict['title'], 'post')
  80. date = get_time()
  81. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
  82. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
  83. 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])
  84. 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>')
  85. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + id_data)
  86. else:
  87. if acl_check(conn, bbs_num_str, 'bbs_view') == 1:
  88. return re_error(conn, '/ban')
  89. text = ''
  90. date = ''
  91. date += temp_dict['date']
  92. data = ''
  93. data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
  94. data += api_topic_thread_make(
  95. ip_pas(conn, temp_dict['user_id']),
  96. date,
  97. render_set(conn, doc_data = temp_dict['data'], data_type = 'thread'),
  98. '0',
  99. color = 'green'
  100. )
  101. user_id = temp_dict['user_id']
  102. count = 0
  103. thread_data = json.loads(api_bbs_w_comment(bbs_num_str + '-' + post_num_str).data)
  104. for temp_dict in thread_data:
  105. count += 1
  106. if user_id == temp_dict['comment_user_id']:
  107. color = 'green'
  108. else:
  109. color = 'default'
  110. date = ''
  111. date += '<a href="/bbs/tool/' + bbs_num_str + '/' + post_num_str + '/' + str(count) + '">(' + get_lang(conn, 'tool') + ')</a> '
  112. date += temp_dict['comment_date']
  113. data += api_topic_thread_make(
  114. ip_pas(conn, temp_dict['comment_user_id']),
  115. date,
  116. render_set(conn, doc_data = temp_dict['comment'], data_type = 'thread'),
  117. str(count),
  118. color = color
  119. )
  120. data += '''
  121. <form method="post">
  122. ''' + (edit_editor(conn, ip, text, 'bbs_comment') if bbs_comment_acl == 0 else '') + '''
  123. </form>
  124. '''
  125. return easy_minify(conn, flask.render_template(skin_check(conn),
  126. imp = [bbs_name, wiki_set(conn), wiki_custom(conn), wiki_css(['(' + get_lang(conn, 'bbs') + ')', 0])],
  127. data = data,
  128. menu = [['bbs/w/' + 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')]]
  129. ))
  130. else:
  131. # db_data_2[0][0] == 'comment'
  132. if flask.request.method == 'POST':
  133. if bbs_comment_acl == 1:
  134. return redirect(conn, '/bbs/set/' + bbs_num_str)
  135. if captcha_post(conn, flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  136. return re_error(conn, '/error/13')
  137. else:
  138. captcha_post(conn, '', 0)
  139. select = flask.request.form.get('comment_select', '0')
  140. select = '' if select == '0' else select
  141. comment_user_name = ''
  142. if select != '':
  143. select_split = select.split('-')
  144. if len(select_split) < 2:
  145. 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]])
  146. db_data_6 = curs.fetchall()
  147. if not db_data_6:
  148. # re_error로 변경 예정
  149. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str)
  150. else:
  151. set_id = bbs_num_str + '-' + post_num_str + '-' + select_split[0]
  152. comment_user_name = db_data_6[0][0]
  153. else:
  154. 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]])
  155. db_data_7 = curs.fetchall()
  156. if not db_data_7:
  157. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str)
  158. else:
  159. set_id = bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split)
  160. comment_user_name = db_data_7[0][0]
  161. else:
  162. set_id = bbs_num_str + '-' + post_num_str
  163. 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])
  164. db_data_5 = curs.fetchall()
  165. id_data = str(int(db_data_5[0][0]) + 1) if db_data_5 else '1'
  166. data = flask.request.form.get('content', '')
  167. if data == '':
  168. # re_error로 대체 예정
  169. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str)
  170. date = get_time()
  171. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
  172. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
  173. 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])
  174. if set_id == '':
  175. end_id = id_data
  176. else:
  177. set_id = re.sub(r'^[0-9]+-[0-9]+-?', '', set_id)
  178. set_id += '-' if set_id != '' else ''
  179. end_id = set_id + id_data
  180. 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>')
  181. if comment_user_name != '':
  182. 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>')
  183. return redirect(conn, '/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + end_id)
  184. else:
  185. if acl_check(conn, bbs_num_str, 'bbs_view') == 1:
  186. return re_error(conn, '/ban')
  187. text = ''
  188. comment_num = ''
  189. date = ''
  190. date += '<a href="javascript:opennamu_change_comment(\'0\');">(' + get_lang(conn, 'comment') + ')</a> '
  191. date += temp_dict['date']
  192. data = ''
  193. data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
  194. data += api_topic_thread_make(
  195. ip_pas(conn, temp_dict['user_id']),
  196. date,
  197. render_set(conn, doc_data = temp_dict['data']),
  198. '0',
  199. color = 'red'
  200. )
  201. user_id = temp_dict['user_id']
  202. comment_data = ''
  203. comment_select = '<select id="opennamu_comment_select" name="comment_select">'
  204. comment_select += '<option value="0">' + get_lang(conn, 'normal') + '</option>'
  205. comment_count = 0
  206. comment_add_count = 0
  207. temp_data = bbs_w_post_comment(conn, user_id, bbs_num_str + '-' + post_num_str, comment_num, bbs_num_str, post_num_str)
  208. comment_data += temp_data[0]
  209. comment_select += temp_data[1]
  210. comment_count += temp_data[2]
  211. comment_add_count += temp_data[3]
  212. comment_add_count -= comment_count
  213. if comment_data != '':
  214. data += '<hr>'
  215. comment_select += '</select>'
  216. if comment_data != '':
  217. data += get_lang(conn, 'comment') + ' : ' + str(comment_count) + '<hr class="main_hr">'
  218. data += get_lang(conn, 'reply') + ' : ' + str(comment_add_count) + '<hr class="main_hr">'
  219. data += comment_data
  220. else:
  221. data += '<hr class="main_hr">'
  222. bbs_comment_form = ''
  223. if bbs_comment_acl == 0:
  224. bbs_comment_form += '''
  225. ''' + comment_select + ''' <a href="javascript:opennamu_return_comment();">(''' + get_lang(conn, 'return') + ''')</a>
  226. <hr class="main_hr">
  227. ''' + edit_editor(conn, ip, text, 'bbs_comment') + '''
  228. '''
  229. data += '''
  230. <form method="post">
  231. ''' + bbs_comment_form + '''
  232. </form>
  233. <script src="/views/main_css/js/route/bbs_w_post.js''' + cache_v() + '''"></script>
  234. '''
  235. return easy_minify(conn, flask.render_template(skin_check(conn),
  236. imp = [bbs_name, wiki_set(conn), wiki_custom(conn), wiki_css(['(' + get_lang(conn, 'bbs') + ')', 0])],
  237. data = data,
  238. menu = [['bbs/w/' + 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')]]
  239. ))