topic.py 7.5 KB

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