bbs_w_post.py 15 KB

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