bbs_w_post.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. def bbs_w_post_make_thread(user_id : str, date : str, data : str, code : str, color : str = '', blind : str = '', add_style : str = '') -> str:
  5. if blind != '':
  6. if data == '':
  7. color_b = 'opennamu_comment_blind'
  8. else:
  9. color_b = 'opennamu_comment_blind_admin'
  10. else:
  11. color_b = 'opennamu_comment_blind_not'
  12. return '''
  13. <table class="opennamu_comment" style="''' + add_style + '''">
  14. <tr>
  15. <td class="opennamu_comment_color_''' + color + '''">
  16. <a href="#thread_shortcut" id="''' + code + '''">#''' + code + '''</a>
  17. ''' + user_id + '''
  18. <span style="float: right;">''' + date + '''</span>
  19. </td>
  20. </tr>
  21. <tr>
  22. <td class="''' + color_b + '''" id="opennamu_comment_data_main">
  23. ''' + data + '''
  24. </td>
  25. </tr>
  26. </table>
  27. '''
  28. def bbs_w_post_comment(user_id : str, sub_code : str, comment_num : str, bbs_num_str : str, post_num_str : str) -> typing.Tuple[str, str, int, int]:
  29. comment_data : str = ''
  30. comment_select : str = ''
  31. comment_count : int = 0
  32. comment_add_count : int = 0
  33. thread_data : typing.List[dict[str, str]] = json.loads(api_bbs_w_comment(sub_code).data)
  34. comment_count += len(thread_data)
  35. comment_add_count += comment_count
  36. temp_dict : dict[str, str]
  37. for temp_dict in thread_data:
  38. color : str = 'default'
  39. if user_id == temp_dict['comment_user_id']:
  40. color = 'green'
  41. sub_code_check : str = re.sub(r'^[0-9]+-[0-9]+-', '', sub_code + '-' + temp_dict['code'])
  42. margin_count : int = sub_code_check.count('-')
  43. date : str = '<a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '/comment/' + sub_code_check + '/tool">(' + load_lang('tool') + ')</a> ' + temp_dict['comment_date']
  44. comment_data += '<span style="padding-left: 20px;"></span>' * margin_count
  45. comment_data += bbs_w_post_make_thread(
  46. ip_pas(temp_dict['comment_user_id']),
  47. date,
  48. render_set(
  49. doc_name = '',
  50. doc_data = temp_dict['comment'],
  51. data_in = 'from'
  52. ),
  53. sub_code_check,
  54. color = color,
  55. add_style = 'width: calc(100% - ' + str(margin_count * 20) + 'px);'
  56. )
  57. comment_default : str = ''
  58. if comment_num == sub_code_check:
  59. comment_default = 'selected'
  60. comment_select += '<option value="' + sub_code_check + '" ' + comment_default + '>' + sub_code_check + '</option>'
  61. comment_data += '<hr class="main_hr">'
  62. temp_data : typing.Tuple[str, str, int, int] = bbs_w_post_comment(user_id, sub_code + '-' + temp_dict['code'], comment_num, bbs_num_str, post_num_str)
  63. comment_data += temp_data[0]
  64. comment_select += temp_data[1]
  65. comment_add_count += temp_data[3]
  66. return (comment_data, comment_select, comment_count, comment_add_count)
  67. def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[int, str] = '', do_type : str = '') -> flask.Response:
  68. conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection]
  69. with get_db_connect() as conn:
  70. curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor] = conn.cursor()
  71. curs.execute(db_change('select set_data from bbs_set where set_id = ? and set_name = "bbs_name"'), [bbs_num])
  72. db_data_3 : typing.Optional[typing.List[typing.Tuple[str]]] = curs.fetchall()
  73. if not db_data_3:
  74. return redirect('/bbs/main')
  75. bbs_name : str = db_data_3[0][0]
  76. curs.execute(db_change('select set_name, set_data, set_code from bbs_data where set_id = ? and set_code = ?'), [bbs_num, post_num])
  77. db_data : typing.Optional[typing.List[typing.Tuple[str, str, str]]] = curs.fetchall()
  78. if not db_data:
  79. return redirect('/bbs/main')
  80. bbs_num_str : str = str(bbs_num)
  81. post_num_str : str = str(post_num)
  82. bbs_comment_acl : int = acl_check(bbs_num_str, 'bbs_comment')
  83. ip : str = ip_check()
  84. set_id : str
  85. text : str
  86. data_preview : str
  87. user_id : str
  88. bbs_comment_form : str
  89. id_data : str
  90. data : str
  91. date : str
  92. temp_dict : dict[str, str]
  93. curs.execute(db_change('select set_data from bbs_set where set_id = ? and set_name = "bbs_type"'), [bbs_num])
  94. db_data_2 : typing.Optional[typing.List[typing.Tuple[str]]] = curs.fetchall()
  95. if not db_data_2:
  96. return redirect('/bbs/main')
  97. elif db_data_2[0][0] == 'thread':
  98. if flask.request.method == 'POST' and do_type != 'preview':
  99. if bbs_comment_acl == 1:
  100. return redirect('/bbs/set/' + bbs_num_str)
  101. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  102. return re_error('/error/13')
  103. else:
  104. captcha_post('', 0)
  105. set_id = bbs_num_str + '-' + post_num_str
  106. 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])
  107. db_data_4 : typing.Optional[typing.List[typing.Tuple[str]]] = curs.fetchall()
  108. id_data = str(int(db_data_4[0][0]) + 1) if db_data_4 else '1'
  109. data = flask.request.form.get('content', '')
  110. if data == '':
  111. # re_error로 대체 예정
  112. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
  113. date = get_time()
  114. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
  115. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
  116. 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])
  117. conn.commit()
  118. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + str(int(id_data) + 1))
  119. else:
  120. if acl_check(bbs_num_str, 'bbs_view') == 1:
  121. return re_error('/ban')
  122. text = ''
  123. data_preview = ''
  124. if do_type == 'preview':
  125. text = flask.request.form.get('content', '')
  126. text = text.replace('\r', '')
  127. data_preview = render_set(
  128. doc_name = '',
  129. doc_data = text,
  130. data_in = 'from'
  131. )
  132. temp_dict = json.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
  133. date = '<a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '/tool">(' + load_lang('tool') + ')</a> ' + temp_dict['date']
  134. data = ''
  135. data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
  136. data += bbs_w_post_make_thread(
  137. ip_pas(temp_dict['user_id']),
  138. date,
  139. render_set(
  140. doc_name = '',
  141. doc_data = temp_dict['data'],
  142. data_in = 'from'
  143. ),
  144. '1',
  145. color = 'green'
  146. )
  147. data += '<hr class="main_hr">'
  148. user_id = temp_dict['user_id']
  149. count : int = 1
  150. thread_data : typing.List[dict[str, str]] = json.loads(api_bbs_w_comment(bbs_num_str + '-' + post_num_str).data)
  151. for temp_dict in thread_data:
  152. count += 1
  153. if user_id == temp_dict['comment_user_id']:
  154. color = 'green'
  155. else:
  156. color = 'default'
  157. date = '<a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '/comment/' + str(count) + '/tool">(' + load_lang('tool') + ')</a> ' + temp_dict['comment_date']
  158. data += bbs_w_post_make_thread(
  159. ip_pas(temp_dict['comment_user_id']),
  160. date,
  161. render_set(
  162. doc_name = '',
  163. doc_data = temp_dict['comment'],
  164. data_in = 'from'
  165. ),
  166. str(count),
  167. color = color
  168. )
  169. data += '<hr class="main_hr">'
  170. bbs_comment_form = ''
  171. if bbs_comment_acl == 0:
  172. bbs_comment_form += '''
  173. <textarea name="content" id="opennamu_edit_textarea" class="opennamu_textarea_100">''' + html.escape(text) + '''</textarea>
  174. <hr class="main_hr">
  175. ''' + captcha_get() + ip_warning() + '''
  176. <button id="opennamu_save_button" formaction="/bbs/w/''' + bbs_num_str + '''/''' + post_num_str + '''" type="submit">''' + load_lang('send') + '''</button>
  177. <button id="opennamu_preview_button" formaction="/bbs/w/preview/''' + bbs_num_str + '''/''' + post_num_str + '''#opennamu_edit_textarea" type="submit">''' + load_lang('preview') + '''</button>
  178. <hr class="main_hr">
  179. '''
  180. data += '''
  181. <form method="post">
  182. ''' + bbs_comment_form + '''
  183. ''' + data_preview + '''
  184. </form>
  185. '''
  186. return easy_minify(flask.render_template(skin_check(),
  187. imp = [bbs_name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('bbs') + ')', 0])],
  188. data = data,
  189. menu = [['bbs/w/' + bbs_num_str, load_lang('return')], ['bbs/edit/' + bbs_num_str + '/' + post_num_str, load_lang('edit')]]
  190. ))
  191. else:
  192. # db_data_2[0][0] == 'comment'
  193. if flask.request.method == 'POST' and do_type != 'preview':
  194. if bbs_comment_acl == 1:
  195. return redirect('/bbs/set/' + bbs_num_str)
  196. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  197. return re_error('/error/13')
  198. else:
  199. captcha_post('', 0)
  200. select : str = flask.request.form.get('comment_select', 'default')
  201. select = '' if select == 'default' else select
  202. if select != '':
  203. select_split : typing.List[str] = select.split('-')
  204. if len(select_split) < 2:
  205. curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str, select_split[0]])
  206. if not curs.fetchall():
  207. # re_error로 변경 예정
  208. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
  209. else:
  210. set_id = bbs_num_str + '-' + post_num_str + '-' + select_split[0]
  211. else:
  212. curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split[0:len(select) - 1]), select_split[len(select_split) - 1]])
  213. if not curs.fetchall():
  214. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
  215. else:
  216. set_id = bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split)
  217. else:
  218. set_id = bbs_num_str + '-' + post_num_str
  219. 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])
  220. db_data_5 : typing.Optional[typing.List[typing.Tuple[str]]] = curs.fetchall()
  221. id_data = str(int(db_data_5[0][0]) + 1) if db_data_5 else '1'
  222. data = flask.request.form.get('content', '')
  223. if data == '':
  224. # re_error로 대체 예정
  225. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
  226. date = get_time()
  227. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
  228. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
  229. 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])
  230. conn.commit()
  231. if set_id == '':
  232. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + id_data)
  233. else:
  234. set_id = re.sub(r'^[0-9]+-[0-9]+-?', '', set_id)
  235. return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str + '#' + set_id + '-' + id_data)
  236. else:
  237. if acl_check(bbs_num_str, 'bbs_view') == 1:
  238. return re_error('/ban')
  239. text = ''
  240. comment_num : str = ''
  241. data_preview = ''
  242. if do_type == 'preview':
  243. text = flask.request.form.get('content', '')
  244. text = text.replace('\r', '')
  245. comment_num = flask.request.form.get('comment_select', '')
  246. data_preview = render_set(
  247. doc_name = '',
  248. doc_data = text,
  249. data_in = 'from'
  250. )
  251. temp_dict = json.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
  252. date = '<a href="/bbs/w/' + bbs_num_str + '/' + post_num_str + '/tool">(' + load_lang('tool') + ')</a> ' + temp_dict['date']
  253. data = ''
  254. data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
  255. data += bbs_w_post_make_thread(
  256. ip_pas(temp_dict['user_id']),
  257. date,
  258. render_set(
  259. doc_name = '',
  260. doc_data = temp_dict['data'],
  261. data_in = 'from'
  262. ),
  263. '0',
  264. color = 'red'
  265. )
  266. user_id = temp_dict['user_id']
  267. comment_data : str = ''
  268. comment_select : str = '<hr class="main_hr"><select name="comment_select">'
  269. comment_select += '<option value="default">' + load_lang('normal') + '</option>'
  270. comment_count : int = 0
  271. comment_add_count : int = 0
  272. temp_data : typing.Tuple[str, str, int, int] = bbs_w_post_comment(user_id, bbs_num_str + '-' + post_num_str, comment_num, bbs_num_str, post_num_str)
  273. comment_data += temp_data[0]
  274. comment_select += temp_data[1]
  275. comment_count += temp_data[2]
  276. comment_add_count += temp_data[3]
  277. comment_add_count -= comment_count
  278. if comment_data != '':
  279. data += '<hr class="main_hr"><hr>'
  280. comment_select += '</select>'
  281. if comment_data != '':
  282. data += load_lang('comment') + ' : ' + str(comment_count) + '<hr class="main_hr">'
  283. data += load_lang('reply') + ' : ' + str(comment_add_count) + '<hr class="main_hr">'
  284. data += comment_data
  285. bbs_comment_form = ''
  286. if bbs_comment_acl == 0:
  287. bbs_comment_form += '''
  288. ''' + comment_select + '''
  289. <hr class="main_hr">
  290. <textarea name="content" id="opennamu_edit_textarea" class="opennamu_textarea_100">''' + html.escape(text) + '''</textarea>
  291. <hr class="main_hr">
  292. ''' + captcha_get() + ip_warning() + '''
  293. <button id="opennamu_save_button" formaction="/bbs/w/''' + bbs_num_str + '''/''' + post_num_str + '''" type="submit">''' + load_lang('send') + '''</button>
  294. <button id="opennamu_preview_button" formaction="/bbs/w/preview/''' + bbs_num_str + '''/''' + post_num_str + '''#opennamu_edit_textarea" type="submit">''' + load_lang('preview') + '''</button>
  295. <hr class="main_hr">
  296. '''
  297. data += '''
  298. <form method="post">
  299. ''' + bbs_comment_form + '''
  300. ''' + data_preview + '''
  301. </form>
  302. '''
  303. return easy_minify(flask.render_template(skin_check(),
  304. imp = [bbs_name, wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('bbs') + ')', 0])],
  305. data = data,
  306. menu = [['bbs/w/' + bbs_num_str, load_lang('return')], ['bbs/edit/' + bbs_num_str + '/' + post_num_str, load_lang('edit')]]
  307. ))