flask-htmx-board1/app.py

177 lines
5.3 KiB
Python
Raw Normal View History

2023-07-01 20:29:27 +10:00
from flask import Flask, render_template, request
from flask_assets import Bundle, Environment
2023-07-01 22:41:34 +10:00
from todo import todos
2023-07-12 01:50:29 +10:00
#from boards import board_list
2023-07-03 23:18:37 +10:00
from threads import threads_lists
2023-07-03 23:45:49 +10:00
from threads_with_posts import open_threads
2023-07-12 01:50:29 +10:00
from arango import ArangoClient
2023-07-01 20:29:27 +10:00
2023-07-03 23:02:48 +10:00
import socket
2023-07-01 20:29:27 +10:00
app = Flask(__name__)
assets = Environment(app)
2023-07-03 23:28:41 +10:00
css = Bundle("src/*.css", output="dist/main.css")
2023-07-01 20:29:27 +10:00
# https://unpkg.com/htmx.org
js = Bundle("src/*.js", output="dist/main.js")
2023-07-03 23:28:41 +10:00
assets.register("css", css)
2023-07-01 20:29:27 +10:00
assets.register("js", js)
2023-07-03 23:28:41 +10:00
css.build()
2023-07-01 20:29:27 +10:00
js.build()
2023-07-14 01:52:00 +10:00
docker_short_id = socket.gethostname()
2023-07-12 01:43:59 +10:00
2023-07-17 12:56:15 +10:00
arango_client = ArangoClient(hosts='https://arango.guaranteedstruggle.host')
2023-07-13 15:02:52 +10:00
db = arango_client.db('board1', username='root', password='stolendick527')
2023-07-12 01:43:59 +10:00
2023-07-12 01:50:29 +10:00
# предполагается что меняться список будет весьма редко, поэтому подхватываем при лишь при перезапуске
boards0 = db.collection('boards')
board_list = [ k['_key'] for k in boards0]
2023-07-12 01:43:59 +10:00
2023-07-03 22:46:40 +10:00
@app.route('/liveness')
def healthx():
return "<h1><center>Liveness check completed</center><h1>"
@app.route('/readiness')
def healthz():
return "<h1><center>Readiness check completed</center><h1>"
2023-07-01 20:29:27 +10:00
@app.route("/")
2023-07-03 23:18:37 +10:00
def homepage():
2023-07-03 23:02:48 +10:00
return render_template("main-page.html", host_id=docker_short_id, boards=board_list)
2023-07-03 22:41:49 +10:00
2023-07-18 23:59:10 +10:00
2023-07-03 23:18:37 +10:00
2023-07-19 00:00:22 +10:00
@app.route('/db_posts')
2023-07-18 23:59:10 +10:00
@app.route("/db_posts/<target_post_id>")
def page_posts_from_db(target_post_id=None):
2023-07-13 19:50:45 +10:00
2023-07-13 19:57:40 +10:00
postos = db.collection('posts')
postos = [ p for p in postos]
2023-07-13 22:01:47 +10:00
postos = sorted(postos, key=lambda posto: posto['_key'], reverse=False)
2023-07-18 23:59:10 +10:00
if not target_post_id:
2023-07-19 00:01:15 +10:00
target_post_id = postos[0]['_key']
2023-07-18 23:59:10 +10:00
return render_template("interactive-posts.html", host_id=docker_short_id, postos=postos, target_post_id=target_post_id)
2023-07-13 19:50:45 +10:00
2023-07-19 00:28:45 +10:00
2023-07-19 00:34:30 +10:00
@app.route("/answer_post/<target_post_id>", methods=['POST'])
2023-07-19 00:29:33 +10:00
def answer_post(target_post_id):
2023-07-19 00:28:45 +10:00
postos = db.collection('posts')
postos = [ p for p in postos]
postos = sorted(postos, key=lambda posto: posto['_key'], reverse=False)
if not target_post_id:
target_post_id = postos[0]['_key']
return render_template("answer-post.html", target_post_id=target_post_id)
2023-07-13 19:50:45 +10:00
2023-07-13 21:56:36 +10:00
@app.route('/post_my_post/<post_key>', methods=['POST'])
def post_a_post(post_key):
2023-07-13 21:26:04 +10:00
postos = db.collection('posts')
2023-07-13 21:56:36 +10:00
2023-07-13 21:26:04 +10:00
#### TODO проверить как-то по-умному что мы таки его добавили
2023-07-13 21:56:36 +10:00
metadata = postos.insert({
'texto': 'next post is this',
2023-07-14 12:34:27 +10:00
'parent_post': f'{post_key}'
2023-07-13 21:56:36 +10:00
}, overwrite_mode='update')
2023-07-13 22:03:03 +10:00
metadata = db.collection('post_parents').insert({
2023-07-13 22:06:10 +10:00
# '_key': post_key,
'_from': f'posts/{post_key}',
2023-07-13 21:56:36 +10:00
'_to': f'posts/{metadata["_key"]}'
}, overwrite_mode='update')
2023-07-14 01:32:56 +10:00
2023-07-14 01:40:19 +10:00
2023-07-14 01:32:56 +10:00
cursor = db.aql.execute(
"""FOR ppp IN posts
LET children = (FOR v IN 1..9999 OUTBOUND ppp post_parents RETURN v)
UPDATE ppp WITH { children_num : COUNT_DISTINCT( children[*]._key ) } IN posts"""
)
2023-07-14 01:40:19 +10:00
cursor = db.aql.execute(
"""FOR ppp IN posts
LET children = (FOR v IN 1OUTBOUND ppp post_parents RETURN v)
UPDATE ppp WITH { answers_num : COUNT_DISTINCT( children[*]._key ) } IN posts"""
)
2023-07-13 21:56:36 +10:00
2023-07-19 01:01:55 +10:00
cursor = db.aql.execute(
"""FOR ppp IN posts
LET children = (FOR v IN 1OUTBOUND ppp post_parents RETURN v)
2023-07-19 01:26:52 +10:00
UPDATE ppp WITH { answers_list : [ children[*]._key, children[*].answers_num, children[*].children_num ] } IN posts"""
2023-07-19 01:01:55 +10:00
)
2023-07-13 22:01:47 +10:00
postos = db.collection('posts')
postos = sorted(postos, key=lambda posto: posto['_key'], reverse=False)
2023-07-14 01:52:00 +10:00
return render_template("i-posts.html", host_id=docker_short_id, postos=postos)
2023-07-13 21:26:04 +10:00
2023-07-13 01:05:54 +10:00
2023-07-19 00:14:13 +10:00
@app.route('/post_to_another_post/<post_key>', methods=['POST'])
2023-07-19 00:18:15 +10:00
def post_to_post(post_key):
2023-07-19 00:14:13 +10:00
postos = db.collection('posts')
data = request.form['send_this_text']
metadata = postos.insert({
'texto': data,
'parent_post': f'{post_key}'
}, overwrite_mode='update')
metadata = db.collection('post_parents').insert({
'_from': f'posts/{post_key}',
'_to': f'posts/{metadata["_key"]}'
}, overwrite_mode='update')
cursor = db.aql.execute(
"""FOR ppp IN posts
LET children = (FOR v IN 1..9999 OUTBOUND ppp post_parents RETURN v)
UPDATE ppp WITH { children_num : COUNT_DISTINCT( children[*]._key ) } IN posts"""
)
cursor = db.aql.execute(
"""FOR ppp IN posts
LET children = (FOR v IN 1OUTBOUND ppp post_parents RETURN v)
UPDATE ppp WITH { answers_num : COUNT_DISTINCT( children[*]._key ) } IN posts"""
)
2023-07-19 01:07:42 +10:00
cursor = db.aql.execute(
"""FOR ppp IN posts
LET children = (FOR v IN 1OUTBOUND ppp post_parents RETURN v)
2023-07-19 01:26:52 +10:00
UPDATE ppp WITH { answers_list : [ children[*]._key, children[*].answers_num, children[*].children_num ] } IN posts"""
2023-07-19 01:07:42 +10:00
)
2023-07-19 00:14:13 +10:00
postos = db.collection('posts')
postos = sorted(postos, key=lambda posto: posto['_key'], reverse=False)
return render_template("i-posts.html", host_id=docker_short_id, postos=postos)
2023-07-13 15:12:58 +10:00
# @app.route('/post_my_file', method=['POST'])
# def post_a_file():
# pass
2023-07-13 01:05:54 +10:00
2023-07-01 20:29:27 +10:00
2023-07-01 22:41:34 +10:00
2023-07-01 20:29:27 +10:00
if __name__ == "__main__":
2023-07-02 00:46:17 +10:00
app.run(debug=True, host='0.0.0.0')