bbs_w_post.py 14 KB

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