2
0

bbs_w_post.py 15 KB

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