diff --git a/.gitignore b/.gitignore index 1ee5e1a..262322d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ __pycache__ venv -db.sqlite \ No newline at end of file +db.sqlite + +config.json diff --git a/dyxless/__init__.py b/dyxless/__init__.py index 347e6ae..09bb763 100644 --- a/dyxless/__init__.py +++ b/dyxless/__init__.py @@ -1,19 +1,31 @@ +import json + from flask import Flask +from flask_mail import Mail from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager db = SQLAlchemy() +mail = Mail() + +with open("dyxless/config.json") as config_file: + config_data = json.load(config_file) def create_app(): app = Flask("__name__", template_folder="dyxless/templates") - app.config["SECRET_KEY"] = "monastyrka-says-helloworld" - app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db.sqlite" + main_settings = config_data["main_settings"] + app.config.update(main_settings) - app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False + db_settings = config_data["db_settings"] + app.config.update(db_settings) + + mail_settings = config_data["mail_settings"] + app.config.update(mail_settings) db.init_app(app) + mail.init_app(app) login_manager = LoginManager() login_manager.login_view = "auth.login" @@ -33,4 +45,8 @@ def create_app(): app.register_blueprint(main_blueprint) + from .mails import mails as mails_blueprint + + app.register_blueprint(mails_blueprint) + return app diff --git a/dyxless/config.json_template b/dyxless/config.json_template new file mode 100644 index 0000000..32f7a4f --- /dev/null +++ b/dyxless/config.json_template @@ -0,0 +1,17 @@ +{ + "main_settings": { + "SECRET_KEY": "some-very-secret-key", + "SQLALCHEMY_TRACK_MODIFICATIONS": false + }, + "db_settings": { + "SQLALCHEMY_DATABASE_URI": "sqlite:///db.sqlite" + }, + "mail_settings": { + "MAIL_SERVER": "smtp.gmail.com", + "MAIL_PORT": 465, + "MAIL_USE_TLS": false, + "MAIL_USE_SSL": true, + "MAIL_USERNAME": "yourmail@gmail.com", + "MAIL_PASSWORD": "yourpass" + } +} \ No newline at end of file diff --git a/dyxless/decorators.py b/dyxless/decorators.py new file mode 100644 index 0000000..f7f0160 --- /dev/null +++ b/dyxless/decorators.py @@ -0,0 +1,9 @@ +from threading import Thread + + +def async_work(f): + def wrapper(*args, **kwargs): + thr = Thread(target=f, args=args, kwargs=kwargs) + thr.start() + + return wrapper diff --git a/dyxless/mails.py b/dyxless/mails.py new file mode 100644 index 0000000..fc9bdbc --- /dev/null +++ b/dyxless/mails.py @@ -0,0 +1,25 @@ +from flask import Blueprint +from flask_mail import Message + +from . import mail +from .decorators import async_work + +mails = Blueprint("mails", __name__) + + +def prepare_msg(subject, sender, recipients, text_body, html_body): + msg = Message(subject, sender=sender, recipients=recipients) + msg.body = text_body + msg.html = html_body + return msg + + +@async_work +def send_async_email(subject, sender, recipients, text_body, html_body): + msg = prepare_msg(subject, sender, recipients, text_body, html_body) + mail.send(msg) + + +def send_mail(subject, sender, recipients, text_body, html_body): + msg = prepare_msg(subject, sender, recipients, text_body, html_body) + mail.send(msg) diff --git a/dyxless/templates/base.html b/dyxless/templates/base.html index 4c441d0..2931f86 100644 --- a/dyxless/templates/base.html +++ b/dyxless/templates/base.html @@ -5,7 +5,7 @@ -