topic.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from .tool.func import *
  2. from .go_api_topic import api_topic_thread_pre_render
  3. from .edit import edit_editor
  4. def topic(topic_num = 0, do_type = '', doc_name = 'Test'):
  5. with get_db_connect() as conn:
  6. curs = conn.cursor()
  7. topic_num = str(topic_num)
  8. if topic_num == '0':
  9. name = get_lang(conn, 'make_new_topic')
  10. sub = get_lang(conn, 'make_new_topic')
  11. name_value = doc_name
  12. sub_value = ''
  13. else:
  14. curs.execute(db_change("select title, sub from rd where code = ?"), [topic_num])
  15. name = curs.fetchall()
  16. if name:
  17. sub = name[0][1]
  18. name = name[0][0]
  19. name_value = name
  20. sub_value = sub
  21. else:
  22. return redirect(conn, '/')
  23. topic_acl = acl_check(name_value, 'topic', topic_num)
  24. topic_view_acl = acl_check('', 'topic_view', topic_num)
  25. if topic_view_acl == 1:
  26. return re_error(conn, '/ban')
  27. ip = ip_check()
  28. if flask.request.method == 'POST' and do_type == '':
  29. if do_edit_slow_check(conn, 'thread') == 1:
  30. return re_error(conn, '/error/42')
  31. name = flask.request.form.get('topic', 'Test')
  32. sub = flask.request.form.get('title', 'Test')
  33. data = flask.request.form.get('content', 'Test').replace('\r', '')
  34. if do_title_length_check(conn, name) == 1:
  35. return re_error(conn, '/error/38')
  36. if do_title_length_check(conn, sub, 'topic') == 1:
  37. return re_error(conn, '/error/38')
  38. if do_edit_filter(conn, sub) == 1:
  39. return re_error(conn, '/error/21')
  40. if do_edit_filter(conn, data) == 1:
  41. return re_error(conn, '/error/21')
  42. if topic_num == '0':
  43. curs.execute(db_change("select code from topic order by code + 0 desc limit 1"))
  44. t_data = curs.fetchall()
  45. topic_num = str(int(t_data[0][0]) + 1) if t_data else '1'
  46. if flask.request.form.get('content', 'Test') == '':
  47. return redirect(conn, '/thread/' + topic_num)
  48. if captcha_post(conn, flask.request.form.get('g-recaptcha-response', flask.request.form.get('g-recaptcha', ''))) == 1:
  49. return re_error(conn, '/error/13')
  50. today = get_time()
  51. if topic_acl == 1:
  52. return re_error(conn, '/ban')
  53. curs.execute(db_change("select id from topic where code = ? order by id + 0 desc limit 1"), [topic_num])
  54. old_num = curs.fetchall()
  55. num = str((int(old_num[0][0]) + 1) if old_num else 1)
  56. match = re.search(r'^user:([^/]+)', name)
  57. if match:
  58. match = match.group(1)
  59. y_check = 0
  60. if ip_or_user(match) == 1:
  61. curs.execute(db_change("select ip from history where ip = ? limit 1"), [match])
  62. u_data = curs.fetchall()
  63. if u_data:
  64. y_check = 1
  65. else:
  66. curs.execute(db_change("select ip from topic where ip = ? limit 1"), [match])
  67. u_data = curs.fetchall()
  68. if u_data:
  69. y_check = 1
  70. else:
  71. curs.execute(db_change("select id from user_set where id = ?"), [match])
  72. u_data = curs.fetchall()
  73. if u_data:
  74. y_check = 1
  75. if y_check == 1:
  76. add_alarm(conn, match, ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  77. curs.execute(db_change("select ip from topic where code = ? and id = '1'"), [topic_num])
  78. ip_data = curs.fetchall()
  79. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  80. add_alarm(conn, ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  81. data = api_topic_thread_pre_render(conn, data, num, ip, topic_num, name, sub)
  82. do_add_thread(conn,
  83. topic_num,
  84. data,
  85. '',
  86. num
  87. )
  88. do_reload_recent_thread(conn,
  89. topic_num,
  90. today,
  91. name,
  92. sub
  93. )
  94. return redirect(conn, '/thread/' + topic_num + '#' + num)
  95. else:
  96. acl_display = 'display: none;' if topic_acl == 1 else ''
  97. name_display = 'display: none;' if topic_num != '0' else ''
  98. shortcut = '<div class="opennamu_thread_shortcut" id="thread_shortcut">'
  99. curs.execute(db_change("select id from topic where code = ? order by id + 0 asc"), [topic_num])
  100. db_data = curs.fetchall()
  101. for for_a in db_data:
  102. shortcut += '<a href="#' + for_a[0] + '">#' + for_a[0] + '</a> '
  103. shortcut += '</div>'
  104. return easy_minify(conn, flask.render_template(skin_check(conn),
  105. imp = [name, wiki_set(conn), wiki_custom(conn), wiki_css(['(' + get_lang(conn, 'discussion') + ')', 0])],
  106. data = '''
  107. <script defer src="/views/main_css/js/route/topic.js''' + cache_v() + '''"></script>
  108. <style id="opennamu_remove_blind">
  109. .opennamu_comment_blind_js {
  110. display: none;
  111. }
  112. </style>
  113. <input type="checkbox" onclick="opennamu_do_remove_blind_thread();" checked> ''' + get_lang(conn, 'remove_blind_thread') + '''
  114. <hr class="main_hr">
  115. ''' + shortcut + '''
  116. <h2 id="topic_top_title">''' + html.escape(sub) + '''</h2>
  117. <div id="opennamu_top_thread"></div>
  118. <div id="opennamu_main_thread"></div>
  119. <div id="opennamu_reload_thread"></div>
  120. <script>
  121. window.addEventListener("DOMContentLoaded", function() {
  122. opennamu_get_thread("''' + topic_num + '''", "top");
  123. opennamu_get_thread("''' + topic_num + '''");
  124. });
  125. </script>
  126. <a href="javascript:opennamu_thread_blind();">(''' + get_lang(conn, 'hide') + ''' | ''' + get_lang(conn, 'hide_release') + ''')</a>
  127. <a href="javascript:opennamu_thread_delete();">(''' + get_lang(conn, 'delete') + ''')</a>
  128. <a href="/thread/''' + topic_num + '/tool">(' + get_lang(conn, 'topic_tool') + ''')</a>
  129. <hr class="main_hr">
  130. <form style="''' + acl_display + '''" method="post">
  131. <div style="''' + name_display + '''">
  132. <input placeholder="''' + get_lang(conn, 'document_name') + '''" name="topic" value="''' + html.escape(name_value) + '''">
  133. <hr class="main_hr">
  134. <input placeholder="''' + get_lang(conn, 'discussion_name') + '''" name="title" value="''' + html.escape(sub_value) + '''">
  135. <hr class="main_hr">
  136. </div>
  137. ''' + edit_editor(conn, ip, '', 'thread') + '''
  138. </form>
  139. ''',
  140. menu = [['topic/' + url_pas(name), get_lang(conn, 'list')]]
  141. ))