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