From 911d3ef845e7d7091d240358510f248e7b06f24c Mon Sep 17 00:00:00 2001 From: pi3c Date: Wed, 11 Oct 2023 14:43:58 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D1=82=D0=B0=20=D0=B8=20=D0=B2=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=B2=D0=BA=D0=B0=20=D0=BA=D0=B0=D1=80=D1=82=D0=B8=D0=BD?= =?UTF-8?q?=D0=BE=D0=BA=20=D0=B4=D0=B8=D0=BD=D0=B0=D0=BC=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D0=BA=D0=B8=20=D1=81=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=BA=D0=BE=D0=B9=20=D0=B2=20=D0=BF=D0=B0=D0=BF=D0=BA?= =?UTF-8?q?=D1=83=20uploads?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + pyproger/app.py | 20 ++++++++++++++++++-- pyproger/config.py | 4 ++++ pyproger/templates/admin/edit.html | 6 ++++++ pyproger/uploads/.gitkeep | 0 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 pyproger/uploads/.gitkeep diff --git a/.gitignore b/.gitignore index 5369c30..fad6f04 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +pyproger/upload/* config.ini data/ diff --git a/pyproger/app.py b/pyproger/app.py index 33c699f..0dc952f 100644 --- a/pyproger/app.py +++ b/pyproger/app.py @@ -1,9 +1,10 @@ import os from dotenv import load_dotenv -from flask import Flask, render_template_string, url_for +from flask import (Flask, render_template_string, request, send_from_directory, + url_for) from flask_admin import helpers -from flask_ckeditor import CKEditor +from flask_ckeditor import CKEditor, upload_fail, upload_success from flask_migrate import Migrate from flask_security.core import Security @@ -168,4 +169,19 @@ def create_app(test_config=None): def hello(): return render_template_string("pong") + @app.route("/files/") + def uploaded_files(filename): + path = app.config["UPLOADED_PATH"] + return send_from_directory(path, filename) + + @app.route("/upload", methods=["POST"]) + def upload(): + f = request.files.get("upload") + extension = f.filename.split(".")[-1].lower() + if extension not in ["jpg", "gif", "png", "jpeg"]: + return upload_fail(message="Image only!") + f.save(os.path.join(app.config["UPLOADED_PATH"], f.filename)) + url = url_for("uploaded_files", filename=f.filename) + return upload_success(url=url) + return app diff --git a/pyproger/config.py b/pyproger/config.py index 23be885..fa139dc 100755 --- a/pyproger/config.py +++ b/pyproger/config.py @@ -40,7 +40,11 @@ SECURITY_SEND_PASSWORD_RESET_NOTICE_EMAIL = False LANGUAGES = ["ru"] BABEL_TRANSLATION_DIRECTORIES = os.path.join(os.path.curdir, "translations") +basedir = os.path.abspath(os.path.dirname(__file__)) CKEDITOR_PKG_TYPE = "full" CKEDITOR_SERVE_LOCAL = True CKEDITOR_ENABLE_CODESNIPPET = True CKEDITOR_CODE_THEME = "monokai_sublime" +CKEDITOR_FILE_UPLOADER = "upload" +# app.config['CKEDITOR_ENABLE_CSRF'] = True # if you want to enable CSRF protect, uncomment this line +UPLOADED_PATH = os.path.join(basedir, "uploads") diff --git a/pyproger/templates/admin/edit.html b/pyproger/templates/admin/edit.html index b2dad4e..c740297 100644 --- a/pyproger/templates/admin/edit.html +++ b/pyproger/templates/admin/edit.html @@ -1,6 +1,12 @@ {% extends 'admin/model/edit.html' %} +{% block head %} +{{ super()}} +{{ ckeditor.load_code_theme() }} +{%endblock%} + {% block tail %} {{ super() }} {{ ckeditor.load() }} + {{ ckeditor.config(name='text')}} {% endblock %} diff --git a/pyproger/uploads/.gitkeep b/pyproger/uploads/.gitkeep new file mode 100644 index 0000000..e69de29