flask-htmx-board1/app.py
Simple_Not 2f630ddde0
All checks were successful
continuous-integration/drone/push Build is passing
we can send now?
2023-07-20 01:32:57 +10:00

204 lines
6.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Flask, render_template, request
from flask_assets import Bundle, Environment
from todo import todos
#from boards import board_list
from threads import threads_lists
from threads_with_posts import open_threads
from arango import ArangoClient
from minio import Minio
import socket, os
app = Flask(__name__)
assets = Environment(app)
css = Bundle("src/*.css", output="dist/main.css")
# https://unpkg.com/htmx.org
js = Bundle("src/*.js", output="dist/main.js")
assets.register("css", css)
assets.register("js", js)
css.build()
js.build()
docker_short_id = socket.gethostname()
arango_client = ArangoClient(hosts='https://arango.guaranteedstruggle.host')
db = arango_client.db('board1', username='root', password='stolendick527')
# предполагается что меняться список будет весьма редко, поэтому подхватываем при лишь при перезапуске
boards0 = db.collection('boards')
board_list = [ k['_key'] for k in boards0]
@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>"
@app.route("/")
def homepage():
return render_template("main-page.html", host_id=docker_short_id, boards=board_list)
@app.route('/db_posts')
@app.route("/db_posts/<target_post_id>")
def page_posts_from_db(target_post_id=None):
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("interactive-posts.html", host_id=docker_short_id, postos=postos, target_post_id=target_post_id)
@app.route("/answer_post/<target_post_id>", methods=['POST'])
def answer_post(target_post_id):
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)
@app.route('/post_my_post/<post_key>', methods=['POST'])
def post_a_post(post_key):
postos = db.collection('posts')
#### TODO проверить как-то по-умному что мы таки его добавили
metadata = postos.insert({
'texto': 'next post is this',
'parent_post': f'{post_key}'
}, overwrite_mode='update')
metadata = db.collection('post_parents').insert({
# '_key': post_key,
'_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 1 OUTBOUND ppp post_parents RETURN v)
UPDATE ppp WITH { answers_num : COUNT_DISTINCT( children[*]._key ) } IN posts"""
)
cursor = db.aql.execute(
"""FOR ppp IN posts
LET children0 = (FOR v IN 1 OUTBOUND ppp post_parents RETURN v)
LET children = (FOR c IN children0
SORT c._key DESC
RETURN c)
UPDATE ppp WITH { answers_list : [ children[*]._key, children[*].answers_num, children[*].children_num ] } IN posts"""
)
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)
@app.route('/post_to_another_post/<post_key>', methods=['POST'])
def post_to_post(post_key):
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 1 OUTBOUND ppp post_parents RETURN v)
UPDATE ppp WITH { answers_num : COUNT_DISTINCT( children[*]._key ) } IN posts"""
)
cursor = db.aql.execute(
"""FOR ppp IN posts
LET children0 = (FOR v IN 1 OUTBOUND ppp post_parents RETURN v)
LET children = (FOR c IN children0
SORT c._key DESC
RETURN c)
UPDATE ppp WITH { answers_list : [ children[*]._key, children[*].answers_num, children[*].children_num ] } IN posts"""
)
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)
# @app.route('/post_my_file', method=['POST'])
# def post_a_file():
# pass
@app.route("/send_dudes", methods=["POST"])
def upload_file():
if request.method == "POST":
uploaded_file = request.files["file"]
minioClient = Minio(
"static.guaranteedstruggle.host",
)
if uploaded_file:
bucket_name = "thread-pics"
size = os.fstat(uploaded_file.fileno()).st_size
minioClient.put_object(
bucket_name, uploaded_file.filename, uploaded_file, size
)
return "OK"
return " NE OK "
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')