bbs_edit.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. from .tool.func import *
  2. from .api_bbs_w_post import api_bbs_w_post
  3. def bbs_edit(bbs_num = '', post_num = '', do_type = ''):
  4. with get_db_connect() as conn:
  5. curs = conn.cursor()
  6. bbs_num_str = str(bbs_num)
  7. post_num_str = str(post_num)
  8. ip = ip_check()
  9. curs.execute(db_change('select set_id from bbs_set where set_id = ? and set_name = "bbs_name"'), [bbs_num_str])
  10. if not curs.fetchall():
  11. return redirect('/bbs/main')
  12. if post_num != '':
  13. curs.execute(db_change('select set_data from bbs_data where set_name = "user_id" and set_id = ? and set_code = ?'), [bbs_num, post_num])
  14. db_data = curs.fetchall()
  15. if not db_data:
  16. return redirect('/bbs/main')
  17. else:
  18. if not db_data[0][0] == ip and admin_check() != 1:
  19. return re_error('/ban')
  20. if acl_check(bbs_num_str, 'bbs_edit') == 1:
  21. return redirect('/bbs/set/' + bbs_num_str)
  22. i_list = ['post_view_acl', 'post_comment_acl']
  23. if flask.request.method == 'POST' and do_type != 'preview':
  24. if captcha_post(flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  25. return re_error('/error/13')
  26. else:
  27. captcha_post('', 0)
  28. if post_num == '':
  29. curs.execute(db_change('select set_code from bbs_data where set_name = "title" and set_id = ? order by set_code + 0 desc'), [bbs_num_str])
  30. db_data = curs.fetchall()
  31. id_data = str(int(db_data[0][0]) + 1) if db_data else '1'
  32. else:
  33. id_data = post_num_str
  34. title = flask.request.form.get('title', 'test')
  35. title = 'test' if title == '' else title
  36. data = flask.request.form.get('content', '')
  37. if data == '':
  38. # re_error로 대체 예정
  39. return redirect('/bbs/w/' + bbs_num_str)
  40. date = get_time()
  41. if post_num == '':
  42. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('title', ?, ?, ?)"), [id_data, bbs_num_str, title])
  43. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('data', ?, ?, ?)"), [id_data, bbs_num_str, data])
  44. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('date', ?, ?, ?)"), [id_data, bbs_num_str, date])
  45. curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('user_id', ?, ?, ?)"), [id_data, bbs_num_str, ip])
  46. else:
  47. curs.execute(db_change("update bbs_data set set_data = ? where set_name = 'title' and set_code = ? and set_id = ?"), [title, post_num, bbs_num_str])
  48. curs.execute(db_change("update bbs_data set set_data = ? where set_name = 'data' and set_code = ? and set_id = ?"), [data, id_data, bbs_num_str])
  49. curs.execute(db_change("update bbs_data set set_data = ? where set_name = 'date' and set_code = ? and set_id = ?"), [date, id_data, bbs_num_str])
  50. return redirect('/bbs/w/' + bbs_num_str + '/' + id_data)
  51. else:
  52. d_list = ['' for _ in range(0, len(i_list))]
  53. if do_type == 'preview':
  54. title = flask.request.form.get('title', '')
  55. data = flask.request.form.get('content', '')
  56. data = data.replace('\r', '')
  57. data_preview = render_set(
  58. doc_data = data,
  59. data_type = 'thread',
  60. data_in = 'bbs'
  61. ) + '<hr>'
  62. for for_a in range(0, len(i_list)):
  63. d_list[for_a] = flask.request.form.get(i_list[for_a], 'normal')
  64. else:
  65. if post_num == '':
  66. title = ''
  67. data = ''
  68. data_preview = ''
  69. else:
  70. curs.execute(db_change('select set_name, set_data, set_code from bbs_data where set_id = ? and set_code = ?'), [bbs_num, post_num])
  71. db_data = curs.fetchall()
  72. db_data = list(db_data) if db_data else []
  73. temp_dict = json.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
  74. title = temp_dict['title']
  75. data = temp_dict['data']
  76. data_preview = ''
  77. acl_div = ['' for _ in range(0, len(i_list))]
  78. acl_list = get_acl_list()
  79. for for_a in range(0, len(i_list)):
  80. for data_list in acl_list:
  81. if data_list == d_list[for_a]:
  82. check = 'selected="selected"'
  83. else:
  84. check = ''
  85. acl_div[for_a] += '<option value="' + data_list + '" ' + check + '>' + (data_list if data_list != '' else 'normal') + '</option>'
  86. if post_num == '':
  87. form_action = 'formaction="/bbs/edit/' + bbs_num_str + '"'
  88. form_action_preview = 'formaction="/bbs/edit/preview/' + bbs_num_str + '"'
  89. else:
  90. form_action = 'formaction="/bbs/edit/' + bbs_num_str + '/' + post_num_str + '"'
  91. form_action_preview = 'formaction="/bbs/edit/preview/' + bbs_num_str + '/' + post_num_str + '"'
  92. editor_top_text = '<a href="/edit_filter">(' + load_lang('edit_filter_rule') + ')</a>'
  93. monaco_on = get_main_skin_set(curs, flask.session, 'main_css_monaco', ip)
  94. if monaco_on == 'use':
  95. editor_display = 'style="display: none;"'
  96. monaco_display = ''
  97. add_get_file = '''
  98. <link rel="stylesheet"
  99. data-name="vs/editor/editor.main"
  100. href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.37.1/min/vs/editor/editor.main.min.css">
  101. <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.37.1/min/vs/loader.min.js"></script>
  102. '''
  103. editor_top_text += ' <a href="javascript:opennamu_edit_turn_off_monaco();">(' + load_lang('turn_off_monaco') + ')</a>'
  104. if flask.request.cookies.get('main_css_darkmode', '0') == '1':
  105. monaco_thema = 'vs-dark'
  106. else:
  107. monaco_thema = ''
  108. add_script = 'do_monaco_init("' + monaco_thema + '");'
  109. else:
  110. editor_display = ''
  111. monaco_display = 'style="display: none;"'
  112. add_get_file = ''
  113. add_script = 'opennamu_edit_turn_off_monaco();'
  114. if editor_top_text != '':
  115. editor_top_text += '<hr class="main_hr">'
  116. if post_num == '':
  117. bbs_title = load_lang('post_add')
  118. else:
  119. bbs_title = load_lang('post_edit')
  120. return easy_minify(flask.render_template(skin_check(),
  121. imp = [bbs_title, wiki_set(), wiki_custom(), wiki_css([0, 0])],
  122. data = editor_top_text + add_get_file + '''
  123. <form method="post">
  124. <textarea style="display: none;" id="opennamu_edit_origin" name="doc_data_org"></textarea>
  125. <div>''' + edit_button('opennamu_edit_textarea', 'opennamu_monaco_editor') + '''</div>
  126. <input placeholder="''' + load_lang('title') + '''" name="title" value="''' + html.escape(title) + '''">
  127. <hr class="main_hr">
  128. <div id="opennamu_monaco_editor" class="opennamu_textarea_500" ''' + monaco_display + '''></div>
  129. <textarea id="opennamu_edit_textarea" ''' + editor_display + ''' class="opennamu_textarea_500" name="content">''' + html.escape(data) + '''</textarea>
  130. <hr class="main_hr">
  131. ''' + captcha_get() + ip_warning() + '''
  132. <button id="opennamu_save_button" type="submit" ''' + form_action + ''' onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('save') + '''</button>
  133. <button id="opennamu_preview_button" type="submit" ''' + form_action_preview + ''' onclick="do_monaco_to_textarea(); do_stop_exit_release();">''' + load_lang('preview') + '''</button>
  134. <hr class="main_hr">
  135. <div id="opennamu_preview_area">''' + data_preview + '''</div>
  136. <script>
  137. do_stop_exit();
  138. do_paste_image('opennamu_edit_textarea', 'opennamu_monaco_editor');
  139. ''' + add_script + '''
  140. </script>
  141. ''' + render_simple_set('''
  142. <hr class="main_hr">
  143. <a href="/acl/TEST#exp">(''' + load_lang('reference') + ''')</a>
  144. <h2>''' + load_lang('acl') + '''</h2>
  145. <h3>''' + load_lang('post_view_acl') + '''</h3>
  146. <select name="post_view_acl">''' + acl_div[0] + '''</select>
  147. <h4>''' + load_lang('post_comment_acl') + '''</h4>
  148. <select name="post_comment_acl">''' + acl_div[1] + '''</select>
  149. <h2>''' + load_lang('markup') + '''</h2>
  150. ''' + load_lang('not_working') + '''
  151. ''') + '''
  152. </form>
  153. ''',
  154. menu = [['bbs/w/' + bbs_num_str, load_lang('return')]]
  155. ))